src/EventListener/RegistrationListener.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\PrivateUser;
  4. use App\Event\UserEvents;
  5. use FOS\UserBundle\FOSUserEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Http\SecurityEvents;
  15. class RegistrationListener implements EventSubscriberInterface
  16. {
  17.     public const VEHICLE_ID 'registration_vehicle_id';
  18.     public const REDIRECT_VEHICLE_ID 'registration_redirect_id';
  19.     public function __construct(private readonly SessionInterface $session, private readonly TokenStorageInterface $tokenStorage, private readonly RouterInterface $router)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             FOSUserEvents::REGISTRATION_CONFIRMED => 'onUserRegistrationConfirmed',
  26.         ];
  27.     }
  28.     public function onKernelRequest(RequestEvent $event)
  29.     {
  30.         if ($event->isMainRequest()) {
  31.             // Get value from URL in auth popin and store it in the session.
  32.             if ($origin $event->getRequest()->query->get(self::VEHICLE_ID)) {
  33.                 $this->session->set(self::VEHICLE_ID$origin);
  34.             }
  35.             // Get value from URL sent in the email and store it in the session.
  36.             if ($vehicleId $event->getRequest()->query->get(self::REDIRECT_VEHICLE_ID)) {
  37.                 $this->session->set(self::REDIRECT_VEHICLE_ID$vehicleId);
  38.             }
  39.             // Once the user is logged in, redirect it to the vehicle
  40.             if (
  41.                 null !== $this->tokenStorage->getToken()
  42.                 && $this->tokenStorage->getToken()->getUser() instanceof PrivateUser
  43.                 && $vehicleId $this->session->get(self::REDIRECT_VEHICLE_IDfalse)) {
  44.                 $response = new RedirectResponse($this->router->generate(
  45.                     'frontend_vehicle_view',
  46.                     ['id' => $vehicleId]
  47.                 ));
  48.                 $event->setResponse($response);
  49.                 $this->session->remove(self::REDIRECT_VEHICLE_ID);
  50.                 return $response;
  51.             }
  52.         }
  53.     }
  54.     public function onUserRegistrationConfirmed($event): void
  55.     {
  56.         $this->session->getFlashBag()->add('popup'$this->router->generate(
  57.             'registration_step',
  58.             ['step' => 4],
  59.             UrlGeneratorInterface::ABSOLUTE_URL
  60.         ));
  61.     }
  62. }