<?php
namespace App\EventSubscribers\Logout;
use App\Entity\User;
use App\Services\TimetableService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Throwable;
class FinishTimetableOnLogoutSubscriber implements EventSubscriberInterface
{
/** @var TimetableService */
private TimetableService $timetableService;
public function __construct(TimetableService $timetableService)
{
$this->timetableService = $timetableService;
}
public function onLogout(LogoutEvent $event)
{
try {
$user = $event->getToken()->getUser();
if ($user instanceof User) {
$this->timetableService->finish($user);
}
} catch (Throwable $t) {
throw $t;
}
}
public static function getSubscribedEvents(): array
{
return [
LogoutEvent::class => 'onLogout'
];
}
}