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

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\HttpFoundation\Session\SessionInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. class EmailConfirmationListener implements EventSubscriberInterface
  20. {
  21.     private $mailer;
  22.     private $tokenGenerator;
  23.     private $router;
  24.     private $session;
  25.     /**
  26.      * EmailConfirmationListener constructor.
  27.      */
  28.     public function __construct(MailerInterface $mailerTokenGeneratorInterface $tokenGeneratorUrlGeneratorInterface $routerSessionInterface $session)
  29.     {
  30.         $this->mailer $mailer;
  31.         $this->tokenGenerator $tokenGenerator;
  32.         $this->router $router;
  33.         $this->session $session;
  34.     }
  35.     /**
  36.      * @return array
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  42.         ];
  43.     }
  44.     public function onRegistrationSuccess(FormEvent $event)
  45.     {
  46.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  47.         $user $event->getForm()->getData();
  48.         $user->setEnabled(false);
  49.         if (null === $user->getConfirmationToken()) {
  50.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  51.         }
  52.         $this->mailer->sendConfirmationEmailMessage($user);
  53.         $this->session->set('fos_user_send_confirmation_email/email'$user->getEmail());
  54.         $url $this->router->generate('fos_user_registration_check_email');
  55.         $event->setResponse(new RedirectResponse($url));
  56.     }
  57. }