<?php
namespace App\Security;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\EventLog;
use App\Entity\TypeEventLog;
class LogoutSubscriber implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function onLogoutEvent(LogoutEvent $event)
{
$user = $event->getToken()->getUser();
$eventLog = new EventLog();
$eventLog->setCreatedAt();
$eventLog->setDescription($user->getUsername() . " Cerró Sesión");
$eventLog->setTypeEntity("Login");
$eventLog->setUser($user);
$eventLog->setEntityId($user->getUserIdentifier());
$eventLog->setIndView(1);
$eventLog->setTypeEventLog($this->entityManager->getRepository(TypeEventLog::class)->find(7));
$this->entityManager->persist($eventLog);
$this->entityManager->flush();
}
public static function getSubscribedEvents(): array
{
return [
LogoutEvent::class => 'onLogoutEvent',
];
}
}