src/EventListener/ActivityListener.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\CommonData\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  6. use Symfony\Component\HttpKernel\HttpKernel;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. /**
  9.  * Class ActivityListener
  10.  *
  11.  * @package App\EventListener
  12.  */
  13. class ActivityListener
  14. {
  15.     /**
  16.      * @var EntityManagerInterface
  17.      */
  18.     private $em;
  19.     /**
  20.      * @var TokenStorageInterface
  21.      */
  22.     private $security;
  23.     /**
  24.      * ActivityListener constructor.
  25.      *
  26.      * @param EntityManagerInterface $em
  27.      * @param TokenStorageInterface  $security
  28.      */
  29.     public function __construct(EntityManagerInterface $emTokenStorageInterface $security)
  30.     {
  31.         $this->em       $em;
  32.         $this->security $security;
  33.     }
  34.     /**
  35.      * @param FilterControllerEvent $event
  36.      *
  37.      * @return null
  38.      * @throws \Exception
  39.      */
  40.     public function onKernelController(FilterControllerEvent $event)
  41.     {
  42.         if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
  43.             return null;
  44.         }
  45.         if ($this->security->getToken()) {
  46.             $user $this
  47.                 ->security
  48.                 ->getToken()
  49.                 ->getUser();
  50.             if (($user instanceof User) && !($user->isActiveNow())) {
  51.                 $this
  52.                     ->em
  53.                     ->flush($user->setLastActivityAt(new \DateTime()));
  54.             }
  55.         }
  56.     }
  57. }