<?php
namespace App\Application\EventSubscriber;
use Athome\UserBundle\Model\UserInterface;
use Athome\UserBundle\Model\UserManagerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
class LastLoginSubscriber implements EventSubscriberInterface
{
/**
* @param EntityManagerInterface $entityManager
*/
public function __construct(private EntityManagerInterface $entityManager)
{
}
public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
];
}
/**
* @param InteractiveLoginEvent $event
* @throws \Exception
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
{
$user = $event->getAuthenticationToken()->getUser();
if ($user instanceof UserInterface) {
$user->setLastLogin(new \DateTime());
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
}