<?php
/**
* src : Zabulu\DatabaseBundle\EventListener\ActivityListener.php
*/
namespace App\EventListener;
use App\Entity\AdminUser;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $om;
protected $tokenStorage;
function __construct(ObjectManager $om, TokenStorage $tokenStorage)
{
$this->om = $om;
$this->tokenStorage = $tokenStorage;
}
/**
* Update the user "lastActivity" on each request
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
// Check that the current request is a "MASTER_REQUEST"
// Ignore any sub-request
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return;
}
// Check token authentication availability
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
if ( $user instanceof AdminUser) {
$user->setActiveTime(new \DateTime());
$this->om->persist($user);
$this->om->flush();
}
}
}
}