src/EventListener/SwitchUserListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\System\Customer;
  4. use App\Entity\System\Employee;
  5. use App\Manager\Logs\ImpersonationLogManager;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Http\Event\SwitchUserEvent;
  8. use Symfony\Component\Security\Http\SecurityEvents;
  9. class SwitchUserListener implements EventSubscriberInterface
  10. {
  11.     private ImpersonationLogManager $impersonationLogManager;
  12.     public function __construct(
  13.         ImpersonationLogManager $impersonationLogManager
  14.     ) {
  15.         $this->impersonationLogManager $impersonationLogManager;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             SecurityEvents::SWITCH_USER => 'onSwitchUser',
  21.         ];
  22.     }
  23.     public function onSwitchUser(SwitchUserEvent $event): void
  24.     {
  25.         /** @var Employee $employee */
  26.         $employee $event->getToken()->getUser();
  27.         /** @var Customer $customer */
  28.         $customer $event->getTargetUser();
  29.         $impersonationLog $this->impersonationLogManager->create();
  30.         $impersonationLog->setCustomerId($customer->getId());
  31.         $impersonationLog->setEmployeeId($employee->getId());
  32.         $this->impersonationLogManager->save($impersonationLog);
  33.     }
  34. }