src/CoreBundle/Component/Error/LogoutExceptionSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\Component\Error;
  4. use App\CoreBundle\Component\FlashMessage\FlashMessage;
  5. use App\CoreBundle\Model\Localization\Localization;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  9. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\Security\Core\Exception\LogoutException;
  13. class LogoutExceptionSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
  17.      */
  18.     protected $flashBag;
  19.     /**
  20.      * @var \Symfony\Component\Routing\RouterInterface
  21.      */
  22.     protected $router;
  23.     /**
  24.      * @var \App\CoreBundle\Model\Localization\Localization
  25.      */
  26.     protected $localization;
  27.     /**
  28.      * @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flashBag
  29.      * @param \Symfony\Component\Routing\RouterInterface $router
  30.      * @param \App\CoreBundle\Model\Localization\Localization $localization
  31.      */
  32.     public function __construct(
  33.         FlashBagInterface $flashBag,
  34.         RouterInterface $router,
  35.         Localization $localization
  36.     ) {
  37.         $this->flashBag $flashBag;
  38.         $this->router $router;
  39.         $this->localization $localization;
  40.     }
  41.     /**
  42.      * {@inheritDoc}
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::EXCEPTION => ['onKernelException'],
  48.         ];
  49.     }
  50.     /**
  51.      * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
  52.      */
  53.     public function onKernelException(ExceptionEvent $event): void
  54.     {
  55.         if ($event->getThrowable() instanceof LogoutException || $event->getThrowable()->getPrevious() instanceof LogoutException) {
  56.             $redirectUrl $this->getSafeUrlToRedirect($event->getRequest()->headers->get('referer'));
  57.             $event->setResponse(new RedirectResponse($redirectUrl));
  58.         }
  59.     }
  60.     /**
  61.      * @param string|null $url
  62.      * @return string
  63.      */
  64.     protected function getSafeUrlToRedirect(?string $url): string
  65.     {
  66.         /*if ($url !== null) {
  67.             $urlParse = parse_url($url);
  68.             $domainUrl = $this->localization->getUrl();
  69.             $domainUrlParse = parse_url($domainUrl);
  70.             $parsedUrl = $urlParse['scheme'] . $urlParse['host'];
  71.             $parsedDomainUrl = $domainUrlParse['scheme'] . $domainUrlParse['host'];
  72.             if ($parsedUrl === $parsedDomainUrl) {
  73.                 return $url;
  74.             }
  75.         }*/
  76.         return $this->router->generate('front_home');
  77.     }
  78. }