<?php
namespace App\EventListener;
use App\Entity\System\Customer;
use App\Entity\System\Employee;
use App\Manager\Logs\ImpersonationLogManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;
class SwitchUserListener implements EventSubscriberInterface
{
private ImpersonationLogManager $impersonationLogManager;
public function __construct(
ImpersonationLogManager $impersonationLogManager
) {
$this->impersonationLogManager = $impersonationLogManager;
}
public static function getSubscribedEvents(): array
{
return [SecurityEvents::SWITCH_USER => 'onSwitchUser'];
}
public function onSwitchUser(SwitchUserEvent $event): void
{
/** @var Employee $employee */
$employee = $event->getToken()->getUser();
/** @var Customer $customer */
$customer = $event->getTargetUser();
$impersonationLog = $this->impersonationLogManager->create();
$impersonationLog->setCustomerId($customer->getId());
$impersonationLog->setEmployeeId($employee->getId());
$this->impersonationLogManager->save($impersonationLog);
}
}