src/Infrastructure/EventSubscriber/UserActivityTrackerSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Infrastructure\EventSubscriber;
  4. use App\Database\Domain\Entity\User\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Security;
  10. class UserActivityTrackerSubscriber implements EventSubscriberInterface
  11. {
  12.     private Security $security;
  13.     private EntityManagerInterface $em;
  14.     public function __construct(Security $securityEntityManagerInterface $em)
  15.     {
  16.         $this->security $security;
  17.         $this->em $em;
  18.     }
  19.     public function onKernelResponse(ResponseEvent $event): void
  20.     {
  21.         $user $this->security->getUser();
  22.         if ($user instanceof User) {
  23.             $user->setLastActivity(new \DateTimeImmutable());
  24.             $this->em->flush();
  25.         }
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             KernelEvents::RESPONSE => ['onKernelResponse'2],
  31.         ];
  32.     }
  33. }