- <?php
- namespace CoreBundle\EventSubscriber;
- use CoreBundle\Entity\Periode;
- use CoreBundle\Entity\Post;
- use CoreBundle\Provider\DefaultValuesProvider;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- use Symfony\Component\HttpKernel\Event\ControllerEvent;
- use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
- use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
- use Symfony\Component\Security\Core\Security;
- use Twig\Environment;
- class CountNewPostsSubscriber implements EventSubscriberInterface
- {
-     /** @var EntityManagerInterface  */
-     protected $em;
-     /** @var SessionInterface  */
-     protected $session;
-     /** @var Environment  */
-     protected  $twig;
-     /** @var DefaultValuesProvider */
-     private $defaultValuesProvider;
-     /** @var Security */
-     private $security;
-     public function __construct(
-         EntityManagerInterface $em,
-         SessionInterface $session,
-         DefaultValuesProvider $defaultValuesProvider,
-         Environment $twig,
-         Security $security
-     ) {
-         $this->em = $em;
-         $this->session = $session;
-         $this->twig = $twig;
-         $this->defaultValuesProvider = $defaultValuesProvider;
-         $this->security = $security;
-     }
-     /**
-       * {@inheritDoc}
-       * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents()
-       */
-     public static function getSubscribedEvents()
-     {
-         return [
-             KernelEvents::CONTROLLER => 'onCoreController'
-         ];
-     }
-     /**
-      * Update the user "lastActivity" on each request
-      * @param ControllerEvent $event
-      */
-     public function onCoreController(ControllerEvent $event)
-     {
-         if (null === $this->security->getToken()) {
-             return;
-         }
-         $lastActivity = $this->session->get('lastactivity');
-         if (null === $lastActivity) {
-             $lastActivity = new \DateTime();
-         }
-         $posts = $this->em->getRepository(Post::class)
-             ->getNewPosts(
-                 $lastActivity,
-                 $this->defaultValuesProvider->getPeriode()
-             );
-         $count = 0;
-         /** @var Post $post */
-         foreach ($posts as $post) {
-             if ($this->security->isGranted('view', $post->getCategory())) {
-                 $count++;
-             }
-         }
-         $this->twig->addGlobal('_count_new_post', $count);
-     }
- }