src/EventListener/UserSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\BuyersGroup;
  4. use App\Entity\PrivateUser;
  5. use App\Entity\ProUser;
  6. use App\Entity\User;
  7. use App\Live\SocketNotifier;
  8. use App\Mailer\UserMailer;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use FOS\UserBundle\Event\FilterUserResponseEvent;
  11. use FOS\UserBundle\FOSUserEvents;
  12. use FOS\UserBundle\Model\UserManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  21. use Symfony\Component\Security\Http\SecurityEvents;
  22. class UserSubscriber implements EventSubscriberInterface
  23. {
  24.     protected $mailer;
  25.     protected $userManager;
  26.     protected $session;
  27.     protected $router;
  28.     protected $tokenStorage;
  29.     public function __construct(
  30.         UserMailer $mailer,
  31.         UserManagerInterface $userManager,
  32.         private readonly EntityManagerInterface $em,
  33.         SessionInterface $session,
  34.         RouterInterface $router,
  35.         TokenStorageInterface $tokenStorage,
  36.         private readonly SocketNotifier $notifier
  37.     ) {
  38.         $this->mailer $mailer;
  39.         $this->userManager $userManager;
  40.         $this->session $session;
  41.         $this->router $router;
  42.         $this->tokenStorage $tokenStorage;
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  48.             FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
  49.             KernelEvents::RESPONSE => 'onKernelResponse',
  50.         ];
  51.     }
  52.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  53.     {
  54.         $this->notifier->disable();
  55.         $user $event->getAuthenticationToken()->getUser();
  56.         $selections $this->session->get('selections', []);
  57.         if (is_array($selections) && count($selections) > 0) {
  58.             $selectionVehicleIds $this->em->getRepository(\App\Entity\Selection::class)->findActiveVehicleIdsByUser($user);
  59.             $vehicles $this->em->getRepository(\App\Entity\Vehicle::class)->findById($selections);
  60.             foreach ($vehicles as $vehicle) {
  61.                 if (!in_array($vehicle->getId(), $selectionVehicleIds)) {
  62.                     $user->addSelection($vehicle);
  63.                 }
  64.             }
  65.         }
  66.         if ($user instanceof ProUser && !in_array(BuyersGroup::ONLINE_GROUP$user->getBuyersGroups()->toArray())) {
  67.             $proBuyersGroup $this->em->getRepository(BuyersGroup::class)->findOneByName(BuyersGroup::ONLINE_GROUP);
  68.             if (null !== $proBuyersGroup) {
  69.                 $proBuyersGroup->addUser($user);
  70.             }
  71.         }
  72.         if ($user instanceof PrivateUser && in_array(BuyersGroup::ONLINE_GROUP$user->getBuyersGroups()->toArray())) {
  73.             $proBuyersGroup $this->em->getRepository(BuyersGroup::class)->findOneByName(BuyersGroup::ONLINE_GROUP);
  74.             if (null !== $proBuyersGroup) {
  75.                 $proBuyersGroup->removeUser($user);
  76.             }
  77.         }
  78.         $this->em->flush();
  79.     }
  80.     public function onRegistrationCompleted(FilterUserResponseEvent $event): void
  81.     {
  82.         $user $event->getUser();
  83.         if (!$user instanceof ProUser) {
  84.             return;
  85.         }
  86.         $this->mailer->sendNewProRegistrationAlert($user);
  87.     }
  88.     public function onKernelResponse(ResponseEvent $event): void
  89.     {
  90.         if (null === $this->tokenStorage->getToken()) {
  91.             return;
  92.         }
  93.         /** @var User|null $user */
  94.         $user $this->tokenStorage->getToken()->getUser();
  95.         if (null === $user || 'anon.' === $user) {
  96.             return;
  97.         }
  98.         if (false === $user->isEnabled()) {
  99.             if (User::ORIGIN_SHOWVROOM === $user->getOrigin()) {
  100.                 $request $event->getRequest();
  101.                 // User has no confirmation token until they send documents on step 3 of registration form
  102.                 if ($event->isMainRequest()
  103.                     && (!$user->getConfirmationToken())
  104.                     && !in_array($request->get('_route'), [
  105.                         'fos_user_registration_register_pro',
  106.                         'fos_user_registration_register_pro_step',
  107.                         'frontend_cgv_validation',
  108.                     ], true)) {
  109.                     $event->setResponse(new RedirectResponse($this->router->generate('fos_user_registration_register_pro')));
  110.                 }
  111.                 return;
  112.             }
  113.             $event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_logout')));
  114.         }
  115.     }
  116. }