src/CoreBundle/EventSubscriber/CountNewPostsSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\EventSubscriber;
  3. use CoreBundle\Entity\Periode;
  4. use CoreBundle\Entity\Post;
  5. use CoreBundle\Provider\DefaultValuesProvider;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Twig\Environment;
  15. class CountNewPostsSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var EntityManagerInterface  */
  18.     protected $em;
  19.     /** @var SessionInterface  */
  20.     protected $session;
  21.     /** @var Environment  */
  22.     protected  $twig;
  23.     /** @var DefaultValuesProvider */
  24.     private $defaultValuesProvider;
  25.     /** @var Security */
  26.     private $security;
  27.     public function __construct(
  28.         EntityManagerInterface $em,
  29.         SessionInterface $session,
  30.         DefaultValuesProvider $defaultValuesProvider,
  31.         Environment $twig,
  32.         Security $security
  33.     ) {
  34.         $this->em $em;
  35.         $this->session $session;
  36.         $this->twig $twig;
  37.         $this->defaultValuesProvider $defaultValuesProvider;
  38.         $this->security $security;
  39.     }
  40.     /**
  41.       * {@inheritDoc}
  42.       * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
  43.       */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER => 'onCoreController'
  48.         ];
  49.     }
  50.     /**
  51.      * Update the user "lastActivity" on each request
  52.      * @param ControllerEvent $event
  53.      */
  54.     public function onCoreController(ControllerEvent $event)
  55.     {
  56.         if (null === $this->security->getToken()) {
  57.             return;
  58.         }
  59.         $lastActivity $this->session->get('lastactivity');
  60.         if (null === $lastActivity) {
  61.             $lastActivity = new \DateTime();
  62.         }
  63.         $posts $this->em->getRepository(Post::class)
  64.             ->getNewPosts(
  65.                 $lastActivity,
  66.                 $this->defaultValuesProvider->getPeriode()
  67.             );
  68.         $count 0;
  69.         /** @var Post $post */
  70.         foreach ($posts as $post) {
  71.             if ($this->security->isGranted('view'$post->getCategory())) {
  72.                 $count++;
  73.             }
  74.         }
  75.         $this->twig->addGlobal('_count_new_post'$count);
  76.     }
  77. }