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

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.      * @param MailerInterface         $mailer
  29.      * @param TokenGeneratorInterface $tokenGenerator
  30.      * @param UrlGeneratorInterface   $router
  31.      * @param SessionInterface        $session
  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.     /**
  41.      * @return array
  42.      */
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return array(
  46.             FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
  47.         );
  48.     }
  49.     /**
  50.      * @param FormEvent $event
  51.      */
  52.     public function onRegistrationSuccess(FormEvent $event)
  53.     {
  54.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  55.         $user $event->getForm()->getData();
  56.         $user->setEnabled(false);
  57.         if (null === $user->getConfirmationToken()) {
  58.             $user->setConfirmationToken($this->tokenGenerator->generateToken());
  59.         }
  60.         $this->mailer->sendConfirmationEmailMessage($user);
  61.         $this->session->set('fos_user_send_confirmation_email/email'$user->getEmail());
  62.         $url $this->router->generate('fos_user_registration_check_email');
  63.         $event->setResponse(new RedirectResponse($url));
  64.     }
  65. }