src/EventListener/EmailConfirmationListener.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\PrivateUser;
  4. use App\Entity\ProUser;
  5. use App\Entity\User;
  6. use App\Twig\Extension\CountryExtension;
  7. use FOS\UserBundle\Event\FormEvent;
  8. use FOS\UserBundle\Event\UserEvent;
  9. use FOS\UserBundle\FOSUserEvents;
  10. use FOS\UserBundle\Mailer\MailerInterface;
  11. use FOS\UserBundle\Util\TokenGeneratorInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. class EmailConfirmationListener implements EventSubscriberInterface
  19. {
  20.     public function __construct(private readonly MailerInterface $mailer, private readonly TokenGeneratorInterface $tokenGenerator, private readonly UrlGeneratorInterface $router, private readonly SessionInterface $session, private readonly TokenStorageInterface $tokenStorage)
  21.     {
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  27.             FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm',
  28.         ];
  29.     }
  30.     public function onRegistrationConfirm($event): void
  31.     {
  32.         $user null;
  33.         if ($event instanceof FormEvent) {
  34.             /** @var $user User */
  35.             $user $event->getForm()->getData();
  36.         } elseif ($event instanceof UserEvent) {
  37.             $user $event->getUser();
  38.         }
  39.         if ($user && CountryExtension::COUNTRY_PORTUGAL == strtolower((string) $user->getCountry())) {
  40.             $this->mailer->sendRegisterConfirmEmailMessage($user);
  41.         }
  42.     }
  43.     public function onRegistrationSuccess(FormEvent $event): void
  44.     {
  45.         /** @var $user User */
  46.         $user $event->getForm()->getData();
  47.         $user->setEnabled(false);
  48.         if (null === $user->getConfirmationToken()) {
  49.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  50.         }
  51.         if (in_array(
  52.             strtolower((string) $user->getCountry()),
  53.             [CountryExtension::COUNTRY_PORTUGAL]
  54.         )) {
  55.             $this->mailer->sendRegisterRequestEmailMessage($user);
  56.         } else {
  57.             $this->mailer->sendConfirmationEmailMessage($user);
  58.         }
  59.         $cookie false;
  60.         if ($user instanceof PrivateUser && in_array(
  61.             strtolower((string) $user->getCountry()),
  62.             [CountryExtension::COUNTRY_PORTUGAL]
  63.         )) {
  64.             $url $this->router->generate('frontend_user_registration_waiting_verification');
  65.             $this->session->set('registration-datalayer''particulier');
  66.         } elseif ($user instanceof PrivateUser) {
  67.             $this->session->set('fos_user_send_confirmation_email/email'$user->getEmail());
  68.             $this->session->set('registration-datalayer''particulier');
  69.             $this->session->getFlashBag()->add('popup'$this->router->generate(
  70.                 'registration_step',
  71.                 ['step' => 3],
  72.                 UrlGeneratorInterface::ABSOLUTE_URL
  73.             ));
  74.             $url $this->router->generate('homepage');
  75.         } else {
  76.             $parameters = [];
  77.             // Set locale only if it's available
  78.             if (!empty($user->getLanguage())) {
  79.                 $parameters['_locale'] = strtolower((string) $user->getLanguage());
  80.             }
  81.             /** @var ProUser $user */
  82.             if (ProUser::ORIGIN_SHOWVROOM === $user->getOrigin()) {
  83.                 // Disconnect the user
  84.                 $this->tokenStorage->setToken(null);
  85.                 $this->session->invalidate();
  86.                 // Don't redirect users with account which is disabled, let them see the website and the popup
  87.                 $this->session->set('showvroom_account_disabled'true);
  88.             } else {
  89.                 $this->session->getFlashBag()->add('popup'$this->router->generate(
  90.                     'frontend_user_registration_waiting',
  91.                     $parameters,
  92.                     UrlGeneratorInterface::ABSOLUTE_URL
  93.                 ));
  94.             }
  95.             $this->session->set('registration-datalayer''pro');
  96.             // This will hide registration popup that is unnecessary after registration
  97.             $cookie = new Cookie('registration-pro-popup'1time() + 60);
  98.             $url $this->router->generate('homepage_pro');
  99.         }
  100.         $response = new RedirectResponse($url);
  101.         if ($cookie) {
  102.             $response->headers->setCookie($cookie);
  103.         }
  104.         $event->setResponse($response);
  105.     }
  106. }