src/EventSubscriber/LogoutExceptionSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Exception\LogoutException;
  10. use Symfony\Component\Security\Core\Security;
  11. class LogoutExceptionSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(private Security $security, private UrlGeneratorInterface $urlGenerator)
  14.     {
  15.     }
  16.     public function onKernelException(ExceptionEvent $event): void
  17.     {
  18.         $exception $event->getThrowable();
  19.         /** @var FlashBagInterface $flashes */
  20.         $flashes $event->getRequest()->getSession()->getBag('flashes');
  21.         if (!($exception instanceof LogoutException)) {
  22.             return;
  23.         }
  24.         if ($this->security->getUser()) {
  25.             $flashes->add('danger''Échec de la déconnexion. Merci de bien vouloir réessayer.');
  26.         }
  27.         $event->setResponse(new RedirectResponse($this->urlGenerator->generate('home')));
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             KernelEvents::EXCEPTION => [
  33.                 // set the priority higher than the Sentry subscriber (which is currently at 128)
  34.                 ['onKernelException'130],
  35.             ],
  36.         ];
  37.     }
  38. }