<?php
namespace App\EventListener;
use App\Event\UserConnectWithoutADIdEvent;
use App\Security\ADTokenException;
use App\Security\UnknownUserAccessException;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Routing\RouterInterface;
class UserListener
{
public function __construct(private readonly EntityManager $em, private readonly RouterInterface $router)
{
}
public function onUserConnectWithoutADId(UserConnectWithoutADIdEvent $event): void
{
$user = $event->getUser();
$payload = $event->getPayload();
$user->setAdId($payload['adId']);
$user->setCompanyId($payload['assignedEntityId']);
$this->em->flush($user);
}
public function onUnknownUserAccess(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!($exception instanceof UnknownUserAccessException)) {
return;
}
$response = new RedirectResponse($this->router->generate('fos_user_registration_register_pro'));
$event->setResponse($response);
}
public function onADTokenExpired(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!($exception instanceof ADTokenException)) {
return;
}
$response = new RedirectResponse($this->router->generate('homepage_pro'));
$event->setResponse($response);
}
}