src/Security/LogoutSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Security\Http\Event\LogoutEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use App\Entity\EventLog;
  7. use App\Entity\TypeEventLog;
  8. class LogoutSubscriber implements EventSubscriberInterface
  9. {
  10.     private $entityManager;
  11.     public function __construct(EntityManagerInterface $entityManager)
  12.     {
  13.         $this->entityManager $entityManager;
  14.     }
  15.     public function onLogoutEvent(LogoutEvent $event)
  16.     {
  17.         $user $event->getToken()->getUser();
  18.         $eventLog = new EventLog();
  19.         $eventLog->setCreatedAt();
  20.         $eventLog->setDescription($user->getUsername() . " Cerró Sesión");
  21.         $eventLog->setTypeEntity("Login");
  22.         $eventLog->setUser($user);
  23.         $eventLog->setEntityId($user->getUserIdentifier());
  24.         $eventLog->setIndView(1);
  25.         $eventLog->setTypeEventLog($this->entityManager->getRepository(TypeEventLog::class)->find(7));
  26.         $this->entityManager->persist($eventLog);
  27.         $this->entityManager->flush();
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             LogoutEvent::class => 'onLogoutEvent',
  33.         ];
  34.     }
  35. }