vendor/friendsofsymfony/user-bundle/EventListener/EmailConfirmationListener.php line 53

  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\EventListener;
  11. use FOS\UserBundle\Event\FormEvent;
  12. use FOS\UserBundle\FOSUserEvents;
  13. use FOS\UserBundle\Mailer\MailerInterface;
  14. use FOS\UserBundle\Util\TokenGeneratorInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. /**
  20.  * @internal
  21.  *
  22.  * @final
  23.  */
  24. class EmailConfirmationListener implements EventSubscriberInterface
  25. {
  26.     private $mailer;
  27.     private $tokenGenerator;
  28.     private $router;
  29.     private $session;
  30.     /**
  31.      * EmailConfirmationListener constructor.
  32.      */
  33.     public function __construct(MailerInterface $mailerTokenGeneratorInterface $tokenGeneratorUrlGeneratorInterface $routerSessionInterface $session)
  34.     {
  35.         $this->mailer $mailer;
  36.         $this->tokenGenerator $tokenGenerator;
  37.         $this->router $router;
  38.         $this->session $session;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  44.         ];
  45.     }
  46.     public function onRegistrationSuccess(FormEvent $event)
  47.     {
  48.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  49.         $user $event->getForm()->getData();
  50.         $user->setEnabled(false);
  51.         if (null === $user->getConfirmationToken()) {
  52.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  53.         }
  54.         $this->mailer->sendConfirmationEmailMessage($user);
  55.         $this->session->set('fos_user_send_confirmation_email/email'$user->getEmail());
  56.         $url $this->router->generate('fos_user_registration_check_email');
  57.         $event->setResponse(new RedirectResponse($url));
  58.     }
  59. }