1<?php
2
3namespace App\EventSubscriber;
4
5use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6use Symfony\Component\HttpKernel\KernelEvents;
7use Doctrine\ORM\EntityManagerInterface;
8use Symfony\Component\Security\Core\Security;
9
10class ActivitySubscriber implements EventSubscriberInterface {
11
12 private $em;
13 private $security;
14
15 public function __construct(
16 EntityManagerInterface $em, Security $security) {
17 $this->em = $em;
18 $this->security = $security;
19 }
20
21 public function onTerminate() {
22 $user = $this->security->getUser();
23
24 if (!$user->isActiveNow()) {
25 $user->setLastActivityAt(new \DateTime());
26 $this->em->flush($user);
27 }
28 }
29
30 public static function getSubscribedEvents() {
31 return [
32 // must be registered before (i.e. with a higher priority than) the default Locale listener
33 KernelEvents::TERMINATE => [['onTerminate', 20]],
34 ];
35 }
36
37}