src/EventListener/ActivityListener.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  * src : Zabulu\DatabaseBundle\EventListener\ActivityListener.php
  4.  */
  5. namespace App\EventListener;
  6. use App\Entity\AdminUser;
  7. use Doctrine\Common\Persistence\ObjectManager;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  9. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  10. use Symfony\Component\HttpKernel\HttpKernel;
  11. /**
  12.  * Listener that updates the last activity of the authenticated user
  13.  */
  14. class ActivityListener
  15. {
  16.     protected $om;
  17.     protected $tokenStorage;
  18.     function __construct(ObjectManager $omTokenStorage $tokenStorage)
  19.     {
  20.         $this->om $om;
  21.         $this->tokenStorage $tokenStorage;
  22.     }
  23.     /**
  24.     * Update the user "lastActivity" on each request
  25.     * @param FilterControllerEvent $event
  26.     */
  27.     public function onCoreController(FilterControllerEvent $event)
  28.     {
  29.         // Check that the current request is a "MASTER_REQUEST"
  30.         // Ignore any sub-request
  31.         if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
  32.             return;
  33.         }
  34.         // Check token authentication availability
  35.         if ($this->tokenStorage->getToken()) {
  36.             $user $this->tokenStorage->getToken()->getUser();
  37.             if ( $user instanceof AdminUser) {
  38.                 $user->setActiveTime(new \DateTime());
  39.                 $this->om->persist($user);
  40.                 $this->om->flush();
  41.             }
  42.         }
  43.     }
  44. }