<?php
declare(strict_types=1);
namespace App\Infrastructure\EventSubscriber;
use App\Database\Domain\Entity\User\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class UserActivityTrackerSubscriber implements EventSubscriberInterface
{
private Security $security;
private EntityManagerInterface $em;
public function __construct(Security $security, EntityManagerInterface $em)
{
$this->security = $security;
$this->em = $em;
}
public function onKernelResponse(ResponseEvent $event): void
{
$user = $this->security->getUser();
if ($user instanceof User) {
$user->setLastActivity(new \DateTimeImmutable());
$this->em->flush();
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => ['onKernelResponse', 2],
];
}
}