<?php
declare(strict_types=1);
namespace App\EventListener;
use App\Entity\System\Customer;
use App\Entity\System\Employee;
use App\Manager\System\CustomerManager;
use App\Manager\System\EmployeeManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
class AuthenticationListener implements EventSubscriberInterface
{
private EmployeeManager $employeeManager;
private CustomerManager $customerManager;
public function __construct(
EmployeeManager $employeeManager,
CustomerManager $customerManager
) {
$this->employeeManager = $employeeManager;
$this->customerManager = $customerManager;
}
public static function getSubscribedEvents(): array
{
return [
AuthenticationSuccessEvent::class => 'onSuccess',
];
}
public function onSuccess(AuthenticationSuccessEvent $event): void
{
/** @var Customer|Employee $user */
$user = $event->getAuthenticationToken()->getUser();
$user->setLastAccess(new \DateTime());
if ($user instanceof Employee) {
$this->employeeManager->save($user);
return;
}
if ($user instanceof Customer) {
$this->customerManager->save($user);
}
}
}