<?php
namespace App\Component\Subscriber;
use App\Component\Event\AuthenticationEvent;
use App\Component\User\UserService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AuthenticationSubscriber implements EventSubscriberInterface
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private UserService $userService
)
{
}
public static function getSubscribedEvents(): array
{
return [
AuthenticationEvent::class => 'onAuthenticate'
];
}
public function onAuthenticate(AuthenticationEvent $event): void
{
$user = $event->getUser();
$route = $this->userService->getDashboardRoute($user);
$url = $this->urlGenerator->generate($route);
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}