src/App/Subscriber/LoginReturnSubscriber.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  9. class LoginReturnSubscriber implements EventSubscriberInterface
  10. {
  11.     use TargetPathTrait;
  12.     private RequestStack $requestStack;
  13.     private TokenStorageInterface $tokenStorage;
  14.     public function __construct(RequestStack $requestStackTokenStorageInterface $tokenStorage)
  15.     {
  16.         $this->requestStack $requestStack;
  17.         $this->tokenStorage $tokenStorage;
  18.     }
  19.     public function onKernelRequest(RequestEvent $event): void
  20.     {
  21.         $request $event->getRequest();
  22.         $route $request->attributes->get('_route');
  23.         if (
  24.             !in_array($route, ['proposal_details''proposal_list'], true)
  25.             || !$event->isMainRequest()
  26.             || $request->isXmlHttpRequest()
  27.         ) {
  28.             return;
  29.         }
  30.         if (null === $this->tokenStorage->getToken()) {
  31.             return;
  32.         }
  33.         if (is_object($this->tokenStorage->getToken()->getUser())) {
  34.             return;
  35.         }
  36.         $this->saveTargetPath($this->requestStack->getSession(), 'main'$request->getUri());
  37.     }
  38.     /**
  39.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             KernelEvents::REQUEST => ['onKernelRequest'],
  45.         ];
  46.     }
  47. }