<?php
declare(strict_types=1);
namespace App\Form\EventListener\User;
use App\Entity\Security\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ValidateUserFieldsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::POST_SUBMIT => 'postSubmit'];
}
public function postSubmit(FormEvent $event): void
{
/** @var User $user */
$user = $event->getData();
if (null !== $user->getId()) {
return;
}
// Hash user's password
if (null === $user->getPassword() || 'random' === $user->getPassword()) {
$user->setPassword(time() . random_int(0, getrandmax()));
}
if (null === $user->getEmail()) {
$user->setEmail(time() . '-random@foxorder.com');
}
}
}