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

Open in your IDE?
  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\Routing\Generator\UrlGeneratorInterface;
  18. /**
  19.  * @internal
  20.  *
  21.  * @final
  22.  */
  23. class EmailConfirmationListener implements EventSubscriberInterface
  24. {
  25.     private $mailer;
  26.     private $tokenGenerator;
  27.     private $router;
  28.     /**
  29.      * EmailConfirmationListener constructor.
  30.      */
  31.     public function __construct(MailerInterface $mailerTokenGeneratorInterface $tokenGeneratorUrlGeneratorInterface $router)
  32.     {
  33.         $this->mailer $mailer;
  34.         $this->tokenGenerator $tokenGenerator;
  35.         $this->router $router;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  41.         ];
  42.     }
  43.     /**
  44.      * @return void
  45.      */
  46.     public function onRegistrationSuccess(FormEvent $event)
  47.     {
  48.         /** @var \FOS\UserBundle\Model\UserInterface $user */
  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.         $event->getRequest()->getSession()->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. }