src/Service/User/LastActiveSubscriber.php line 56
<?php
/**
* Created by PhpStorm.
* User: Михаил
* Date: 28.01.2019
* Time: 17:31
*/
namespace App\Service\User;
use App\Entity\User\User;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class LastActiveSubscriber implements EventSubscriberInterface
{
public function __construct(
private Security $security,
private EntityManagerInterface $em,
private LoggerInterface $logger
)
{
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::FINISH_REQUEST => 'onTerminate',
];
}
public function onTerminate(\Symfony\Component\HttpKernel\Event\FinishRequestEvent $event)
{
try {
if (!$event->isMainRequest()) {
return;
}
if (false === $event->getRequest()->hasSession()) {
return;
}
$user = $this->security->getUser();
// if (!$this->storage->getToken()) return;
/** @var User $user */
// $user = $this->storage->getToken()?->getUser();
if (!$user instanceof User) {
return;
}
$this->em->getRepository(User::class)->updateLastActivity($user);
// dump($res);
} catch (\Exception $exception) {
$this->logger->critical('LastActiveSubscriber', [
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
]);
}
}
}