<?php
namespace Stimactiv\CoreBundle\EventSubscriber;
use Stimactiv\CoreBundle\Entity\BaseUser;
use Stimactiv\CoreBundle\Model\BaseUserManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class ActivitySubscriber implements EventSubscriberInterface
{
private BaseUserManager $userManager;
private Security $security;
public function __construct(BaseUserManager $userManager, Security $security)
{
$this->userManager = $userManager;
$this->security = $security;
}
public function onTerminate(): void
{
$user = $this->security->getUser();
if ($user instanceof BaseUser && !$user->isActiveNow()) {
$user->setLastActivityAt(new \DateTime());
$this->userManager->update($user);
}
}
public static function getSubscribedEvents(): array
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::TERMINATE => [['onTerminate', 20]],
];
}
}