Last active
July 2, 2019 13:31
-
-
Save imiroslavov/4b8d0f5006b8a6ae45013dc06e960d25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EventListener; | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | |
use Symfony\Component\HttpKernel\HttpKernel; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
/** | |
* Class ActivityListener. | |
*/ | |
class ActivityListener | |
{ | |
/** | |
* @var TokenStorage | |
*/ | |
private $tokenStorage; | |
/** | |
* @var EntityManagerInterface | |
*/ | |
private $entityManager; | |
/** | |
* ActivityListener constructor. | |
* | |
* @param TokenStorageInterface $tokenStorage | |
* @param EntityManagerInterface $entityManager | |
*/ | |
public function __construct(TokenStorageInterface $tokenStorage, EntityManagerInterface $entityManager) | |
{ | |
$this->tokenStorage = $tokenStorage; | |
$this->entityManager = $entityManager; | |
} | |
/** | |
* @param FilterControllerEvent $event | |
*/ | |
public function onKernelController(FilterControllerEvent $event) | |
{ | |
if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) { | |
return; | |
} | |
if ($this->tokenStorage->getToken()) { | |
/* @var User $user */ | |
$user = $this->tokenStorage->getToken()->getUser(); | |
if (($user instanceof UserInterface) && (!$user->isOnline())) { | |
$user->setLastActivityAt(new \DateTime()); | |
$this->entityManager->flush($user); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment