src/Subscriber/RequestSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\GalleryPic;
  4. use App\Entity\Message;
  5. use App\Entity\Report;
  6. use App\Entity\User;
  7. use App\Entity\UserIpBanned;
  8. use App\EntityManager\UserIpBannedManager;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. use Twig\Environment;
  18. class RequestSubscriber implements EventSubscriberInterface
  19. {
  20.     protected $tokenStorage;
  21.     protected $entityManager;
  22.     protected $twigEnvironment;
  23.     protected $security;
  24.     protected $router;
  25.     protected $userIpBannedManager;
  26.     public function __construct(
  27.         TokenStorageInterface $tokenStorage,
  28.         EntityManagerInterface $entityManager,
  29.         Environment $twigEnvironment,
  30.         Security $security,
  31.         RouterInterface $router,
  32.         UserIpBannedManager $userIpBannedManager
  33.     ) {
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->entityManager $entityManager;
  36.         $this->twigEnvironment $twigEnvironment;
  37.         $this->security $security;
  38.         $this->router $router;
  39.         $this->userIpBannedManager $userIpBannedManager;
  40.     }
  41.     public function onKernelRequest(RequestEvent $event)
  42.     {
  43.         $this->updateUserOnlineStatus();
  44.         $this->loadUnreadMessages();
  45.         $this->loadAdminData();
  46.     }
  47.     protected function updateUserOnlineStatus()
  48.     {
  49.         $user $this->getUser();
  50.         if (!$user instanceof User || $this->security->isGranted(User::ROLE_IMPERSONATING)) {
  51.             return;
  52.         }
  53.         $delay 300;
  54.         if (!$user->isOnline($delay)) {
  55.             $user->setDateLastOnline(new \DateTime());
  56.             $this->entityManager->persist($user);
  57.             $this->entityManager->flush();
  58.         }
  59.     }
  60.     protected function loadUnreadMessages()
  61.     {
  62.         $user $this->getUser();
  63.         if (!$user instanceof User) {
  64.             return;
  65.         }
  66.         $this->twigEnvironment->addGlobal(
  67.             'unreadMessagesCounter',
  68.             $this->entityManager->getRepository(Message::class)->getUnreadMessagesCounterForUser($user)
  69.         );
  70.     }
  71.     protected function loadAdminData()
  72.     {
  73.         $user $this->getUser();
  74.         if (!$user instanceof User || !$user->hasRole(User::ROLE_SUPER_ADMIN)) {
  75.             return;
  76.         }
  77.         $data = [
  78.             'report' => $this->entityManager->getRepository(Report::class)->getOpenCount(),
  79.             'pictureWaitingForValidation' => $this->entityManager
  80.                 ->getRepository(GalleryPic::class)
  81.                 ->getCountPicturesWaitingForModeration(),
  82.         ];
  83.         $this->twigEnvironment->addGlobal(
  84.             'adminCounter',
  85.             $data
  86.         );
  87.     }
  88.     /**
  89.      * @return mixed|null
  90.      */
  91.     protected function getUser()
  92.     {
  93.         if (!is_null($this->tokenStorage->getToken())) {
  94.             return $this->tokenStorage->getToken()->getUser();
  95.         }
  96.         return null;
  97.     }
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return [
  101.             KernelEvents::REQUEST => 'onKernelRequest',
  102.         ];
  103.     }
  104. }