<?php
namespace App\EventListener;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserLastActivity
{
/** @var TokenStorageInterface */
private $tokenStorage;
/** @var UserRepository */
private $userRepository;
public function __construct(
TokenStorageInterface $tokenStorage,
UserRepository $userRepository)
{
$this->tokenStorage = $tokenStorage;
$this->userRepository = $userRepository;
}
public function onKernelRequest(): void
{
if (null !== $this->tokenStorage->getToken() && ($user = $this->tokenStorage->getToken()->getUser()) instanceof User) {
/* @var User $user */
$this->userRepository->updateLastActivity($user);
}
}
}