<?php
namespace AppEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpKernelKernelEvents;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentSecurityCoreSecurity;
class ActivitySubscriber implements EventSubscriberInterface {
private $em;
private $security;
public function __construct(
EntityManagerInterface $em, Security $security) {
$this->em = $em;
$this->security = $security;
}
public function onTerminate() {
$user = $this->security->getUser();
if (!$user->isActiveNow()) {
$user->setLastActivityAt(new DateTime());
$this->em->flush($user);
}
}
public static function getSubscribedEvents() {
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::TERMINATE => [['onTerminate', 20]],
];
}
}