src/EventListener/BeforeLogoutListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Exception\RefreshTokenError;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. /**
  11.  * Class BeforeLogout.
  12.  */
  13. class BeforeLogoutListener implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var UrlGeneratorInterface
  17.      */
  18.     protected UrlGeneratorInterface $urlGenerator;
  19.     public function __construct(UrlGeneratorInterface $urlGenerator)
  20.     {
  21.         $this->urlGenerator $urlGenerator;
  22.     }
  23.     /**
  24.      * @param RequestEvent $event
  25.      */
  26.     public function onKernelRequest(RequestEvent $event): void
  27.     {
  28.         if ('/logout' === $event->getRequest()->getPathInfo() && !isset($_SESSION['samlUserdata'])) {
  29.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('app.logout.classic')));
  30.         }
  31.     }
  32.     /**
  33.      * @param ExceptionEvent $event
  34.      */
  35.     public function onKernelException(ExceptionEvent $event): void
  36.     {
  37.         if ($event->getThrowable() instanceof RefreshTokenError) {
  38.             $event->setResponse(new RedirectResponse($this->urlGenerator->generate('app.logout.classic')));
  39.         }
  40.     }
  41.     /**
  42.      * @return array
  43.      */
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             KernelEvents::REQUEST => [
  48.                 ['onKernelRequest'15],
  49.             ],
  50.             KernelEvents::EXCEPTION => [
  51.                 ['onKernelException'15],
  52.             ],
  53.         ];
  54.     }
  55. }