src/EventListener/RegistrationListener.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use FOS\UserBundle\FOSUserEvents;
  4. use FOS\UserBundle\Event\FormEvent;
  5. use FOS\UserBundle\Event\GetResponseUserEvent;
  6. // use Symfony\Component\EventDispatcher\Event;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. // use Doctrine\Common\Persistence\ObjectManager;
  11. // use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Twig\Environment as Twig_Environment;
  14. /**
  15.  * Listener responsible to change datas on register user
  16.  */
  17. class RegistrationListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ContainerInterface
  21.      */
  22.     protected $container;
  23.     /**
  24.      * @var Twig_Environment
  25.      */
  26.     protected $twig;
  27.     /**
  28.      * @var UrlGeneratorInterface
  29.      */
  30.     protected $router;
  31.     /**
  32.      * RegistrationListener constructor.
  33.      *
  34.      * @param ContainerInterface $container
  35.      * @param \Twig_Environment $twig
  36.      * @param UrlGeneratorInterface $router
  37.      */
  38.     public function __construct(ContainerInterface $containerTwig_Environment $twigUrlGeneratorInterface $router)
  39.     {
  40.         $this->container $container;
  41.         $this->twig $twig;
  42.         $this->router $router;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return array(
  50.             FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInitialize',
  51.             FOSUserEvents::REGISTRATION_FAILURE    => 'onRegistrationFailure',
  52.             FOSUserEvents::REGISTRATION_SUCCESS => ['onRegistrationSuccess', -10],
  53.         );
  54.     }
  55.     public function onRegistrationInitialize(GetResponseUserEvent $event)
  56.     {
  57.         $currentRoute $event->getRequest()->attributes->get('_route');
  58.         // dump($currentRoute);
  59.         if ($currentRoute === "alumni_registration_register") {
  60.             $user $event->getUser();
  61.             
  62.            $user->addRole('ROLE_ALUMNI');
  63.            //dump($user);
  64.            // $this->twig->addGlobal('form_errors', []);
  65.          //  dump("alumni");
  66.         } else if ($currentRoute === "annonce_registration_register") {
  67.             $user $event->getUser();
  68.             $user->addRole('ROLE_ANNO');
  69.         }
  70.         
  71.         //die;
  72.     }
  73.     public function onRegistrationFailure(FormEvent $event)
  74.     {
  75.         $currentRoute $event->getRequest()->attributes->get('_route');
  76.         if ($this->isRoutesAvailable($currentRoute)) {
  77.             $this->twig->addGlobal('form_errors'$event->getForm()->getErrors(true));
  78.         }
  79.     }
  80.     public function onRegistrationSuccess(FormEvent $event)
  81.     {
  82.         $currentRoute $event->getRequest()->attributes->get('_route');
  83.         if ($this->isRoutesAvailable($currentRoute)) {
  84.             $url $this->router->generate('alumni_profile');
  85.             $event->setResponse(new RedirectResponse($url));
  86.         } else if ($currentRoute === "annonce_registration_register") {
  87.             $url $this->router->generate('annonce_index');
  88.             $event->setResponse(new RedirectResponse($url));
  89.         }
  90.     }
  91.     
  92.     private function isRoutesAvailable($currentRoute)
  93.     {
  94.         return in_array(
  95.             $currentRoute,
  96.             ['alumni_registration_register']
  97.         );
  98.     }
  99. }