<?php
namespace App\EventListener;
use App\Exception\RefreshTokenError;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Class BeforeLogout.
*/
class BeforeLogoutListener implements EventSubscriberInterface
{
/**
* @var UrlGeneratorInterface
*/
protected UrlGeneratorInterface $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event): void
{
if ('/logout' === $event->getRequest()->getPathInfo() && !isset($_SESSION['samlUserdata'])) {
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('app.logout.classic')));
}
}
/**
* @param ExceptionEvent $event
*/
public function onKernelException(ExceptionEvent $event): void
{
if ($event->getThrowable() instanceof RefreshTokenError) {
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('app.logout.classic')));
}
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['onKernelRequest', 15],
],
KernelEvents::EXCEPTION => [
['onKernelException', 15],
],
];
}
}