<?php
declare(strict_types=1);
namespace App\Bundles\UserBundle\EventListener\User;
use App\Bundles\AuditBundle\Service\SessionAuditEntry\SessionAuditEntryModifier;
use App\Platform\Service\SessionProvider;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Event\LogoutEvent;
#[AsEventListener(event: LogoutEvent::class, method: 'onLogout', priority: 8)]
class BeforeLogoutListener
{
public function __construct(
private readonly SessionAuditEntryModifier $sessionAuditEntryModifier,
private readonly SessionProvider $sessionProvider,
) {
}
public function onLogout(LogoutEvent $event): void
{
if (!$this->isLogoutRequest($event->getRequest())) {
return;
}
$session = $this->sessionProvider->provide($event->getRequest());
$this->sessionAuditEntryModifier->markLogoutTimeBySession($session);
$request = $event->getRequest();
if ($request->query->has('expiredSession')) {
$event->setResponse(new RedirectResponse('/login?expiredSession=true'));
}
}
private function isLogoutRequest(Request $request): bool
{
return $request->attributes->get('_route') === 'app_logout';
}
}