src/FMT/Application/Listener/UserToFOSListener.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3.  * Author: Anton Orlov
  4.  * Date: 23.03.2018
  5.  * Time: 13:45
  6.  */
  7. namespace FMT\Application\Listener;
  8. use FMT\Data\Entity\User;
  9. use FMT\Domain\Event\UserEvent;
  10. use FMT\Application\FormType\Security\RegistrationDonorType;
  11. use FMT\Application\FormType\Security\RegistrationStudentType;
  12. use FMT\Application\FormType\Security\UserPasswordType;
  13. use FOS\UserBundle\Event\FilterUserResponseEvent;
  14. use FOS\UserBundle\Event\FormEvent;
  15. use FOS\UserBundle\Event\GetResponseUserEvent;
  16. use FOS\UserBundle\FOSUserEvents;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\Form\FormFactoryInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Response;
  21. /**
  22.  * Class UserToFOSListener
  23.  * @package FMT\Application\Listener
  24.  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25.  * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  26.  */
  27. class UserToFOSListener
  28. {
  29.     /** @var EventDispatcherInterface */
  30.     private $dispatcher;
  31.     /** @var FormFactoryInterface */
  32.     private $factory;
  33.     /** @var RequestStack */
  34.     private $stack;
  35.     /**
  36.      * UserToFOSListener constructor.
  37.      * @param EventDispatcherInterface $dispatcher
  38.      * @param FormFactoryInterface $factory
  39.      * @param RequestStack $stack
  40.      */
  41.     public function __construct(
  42.         EventDispatcherInterface $dispatcher,
  43.         FormFactoryInterface $factory,
  44.         RequestStack $stack
  45.     ) {
  46.         $this->dispatcher $dispatcher;
  47.         $this->factory $factory;
  48.         $this->stack $stack;
  49.     }
  50.     /**
  51.      * @param UserEvent $event
  52.      */
  53.     public function onUserSubmitted(UserEvent $event)
  54.     {
  55.         $user $event->getUser();
  56.         if (!$user->isAnyStudent() && !$user->isAnyDonor() && !$user->isInternal()) {
  57.             return;
  58.         }
  59.         $event = new GetResponseUserEvent($user$this->stack->getCurrentRequest());
  60.         $this->dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_INITIALIZE);
  61.         $event = new FormEvent($this->getUserForm($user), $this->stack->getCurrentRequest());
  62.         $this->dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_SUCCESS);
  63.     }
  64.     /**
  65.      * @param UserEvent $event
  66.      */
  67.     public function onUserCreated(UserEvent $event)
  68.     {
  69.         $user $event->getUser();
  70.         if (!$user->isAnyStudent() && !$user->isAnyDonor() || !$user->isInternal()) {
  71.             return;
  72.         }
  73.         $event = new FilterUserResponseEvent($user$this->stack->getCurrentRequest(), new Response());
  74.         $this->dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_COMPLETED);
  75.     }
  76.     /**
  77.      * @param UserEvent $event
  78.      */
  79.     public function onUserFailed(UserEvent $event)
  80.     {
  81.         $user $event->getUser();
  82.         if (!$user->isAnyStudent() && !$user->isAnyDonor() && !$user->isInternal()) {
  83.             return;
  84.         }
  85.         $event = new FormEvent($this->getUserForm($user), $this->stack->getCurrentRequest());
  86.         $this->dispatcher->dispatch($eventFOSUserEvents::REGISTRATION_FAILURE);
  87.     }
  88.     /**
  89.      * @param UserEvent $event
  90.      */
  91.     public function onResetSuccess(UserEvent $event)
  92.     {
  93.         $user $event->getUser();
  94.         if (!$user->isAnyStudent() && !$user->isAnyDonor() && !$user->isInternal()) {
  95.             return;
  96.         }
  97.         $form $this->factory->create(UserPasswordType::class, $user);
  98.         $formEvent = new FormEvent($form$this->stack->getCurrentRequest());
  99.         $this->dispatcher->dispatch($formEventFOSUserEvents::RESETTING_RESET_SUCCESS);
  100.     }
  101.     /**
  102.      * @param UserEvent $event
  103.      */
  104.     public function onConfirmationSuccess(UserEvent $event)
  105.     {
  106.         $user $event->getUser();
  107.         if (!$user->isAnyStudent() && !$user->isAnyDonor() && !$user->isInternal()) {
  108.             return;
  109.         }
  110.         $fosEvent = new GetResponseUserEvent($user$this->stack->getCurrentRequest());
  111.         $this->dispatcher->dispatch($fosEventFOSUserEvents::REGISTRATION_CONFIRM);
  112.     }
  113.     /**
  114.      * @param User $user
  115.      * @return \Symfony\Component\Form\FormInterface
  116.      */
  117.     private function getUserForm(User $user)
  118.     {
  119.         $class $user->isAnyStudent() ? RegistrationStudentType::class : RegistrationDonorType::class;
  120.         return $this->factory->create($class$user);
  121.     }
  122. }