src/ForumBundle/EventSubscriber/ActivitySubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace ForumBundle\EventSubscriber;
  3. use CoreBundle\Entity\Category;
  4. use CoreBundle\Entity\Post;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\HttpKernel;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use CoreBundle\Entity\Student;
  10. use CoreBundle\Entity\Tutor;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Core\Security;
  14. class ActivitySubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var Security */
  17.     protected $security;
  18.     /** @var EntityManagerInterface  */
  19.     protected $entityManager;
  20.     protected $requestStack;
  21.     public function __construct(Security $securityEntityManagerInterface $entityManagerRequestStack $requestStack)
  22.     {
  23.         $this->security $security;
  24.         $this->entityManager $entityManager;
  25.         $this->requestStack $requestStack;
  26.     }
  27.     /**
  28.       * {@inheritDoc}
  29.       * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
  30.       */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             KernelEvents::CONTROLLER => 'onCoreController'
  35.         ];
  36.     }
  37.     /**
  38.      * Update the user "lastActivity" on each request
  39.      */
  40.     public function onCoreController(ControllerEvent $event)
  41.     {
  42.         // Check that the current request is a "MASTER_REQUEST"
  43.         // Ignore any sub-request
  44.         if ($event->getRequestType() !== HttpKernel::MAIN_REQUEST) {
  45.             return;
  46.         }
  47.         // Check token authentication availability
  48.         if (null !== $this->security->getToken()) {
  49.             $user $this->security->getToken()->getUser();
  50.             if ( (($user instanceof Student) || ($user instanceof Tutor)) /*&& !($user->isActiveNow())*/ ) {
  51.                 $user->setLastActivity(new \DateTime('now'));
  52.                 $category_id $this->requestStack->getCurrentRequest()->get('category_id');
  53.                 $post_id $this->requestStack->getCurrentRequest()->get('post_id');
  54.                 if (null !== $category_id) {
  55.                     $category $this->entityManager->getRepository(Category::class)->find($category_id);
  56.                     if (null !== $category) {
  57.                         $user->setLastCategory($category);
  58.                     }
  59.                 } elseif (null !== $post_id) {
  60.                     $post $this->entityManager->getRepository(Post::class)->find($post_id);
  61.                     if (null !== $post) {
  62.                         $user->setLastCategory($post->getCategory());
  63.                     }
  64.                 }
  65.                 $this->entityManager->persist($user);
  66.                 $this->entityManager->flush();
  67.             }
  68.         }
  69.     }
  70. }