<?php
namespace App\EventListener;
use App\Entity\CommonData\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Class ActivityListener
*
* @package App\EventListener
*/
class ActivityListener
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var TokenStorageInterface
*/
private $security;
/**
* ActivityListener constructor.
*
* @param EntityManagerInterface $em
* @param TokenStorageInterface $security
*/
public function __construct(EntityManagerInterface $em, TokenStorageInterface $security)
{
$this->em = $em;
$this->security = $security;
}
/**
* @param FilterControllerEvent $event
*
* @return null
* @throws \Exception
*/
public function onKernelController(FilterControllerEvent $event)
{
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return null;
}
if ($this->security->getToken()) {
$user = $this
->security
->getToken()
->getUser();
if (($user instanceof User) && !($user->isActiveNow())) {
$this
->em
->flush($user->setLastActivityAt(new \DateTime()));
}
}
}
}