app/Plugin/Schedule/EventSubscriber/KernelEventSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2018 SYSTEM_KD
  4.  * Date: 2018/12/02
  5.  */
  6. namespace Plugin\Schedule\EventSubscriber;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Eccube\Request\Context;
  9. use Plugin\Schedule\Service\ScheduleService;
  10. use Plugin\Schedule\Utils\ScheduleUtility;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  14. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. /**
  17.  * Class KernelEventSubscriber
  18.  * @package Plugin\Schedule\EventSubscriber
  19.  */
  20. class KernelEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
  21. {
  22.     /** @var Context */
  23.     private $context;
  24.     /** @var EntityManagerInterface */
  25.     private $em;
  26.     /** @var ScheduleService */
  27.     private $scheduleService;
  28.     /** @var ScheduleUtility */
  29.     private $scheduleUtility;
  30.     public function __construct(
  31.         Context $context,
  32.         EntityManagerInterface $em,
  33.         ScheduleService $scheduleService,
  34.         ScheduleUtility $scheduleUtility,
  35.         SessionInterface $session)
  36.     {
  37.         parent::__construct($session);
  38.         $this->context $context;
  39.         $this->em $em;
  40.         $this->scheduleService $scheduleService;
  41.         $this->scheduleUtility $scheduleUtility;
  42.     }
  43.     /**
  44.      * @param FilterControllerEvent $event
  45.      * @throws \Exception
  46.      */
  47.     public function onKernelController(FilterControllerEvent $event)
  48.     {
  49.         if ($this->context->isAdmin() || $this->isAdminUser()) {
  50.             // 管理画面へのアクセスの場合
  51.             return;
  52.         }
  53.         $nowTime $this->scheduleUtility->getNowUTCTime();
  54.         // Frontのみ実施
  55.         $filter $this->em->getFilters()->enable('category_schedule');
  56.         $filter->setParameter('now_time'$nowTime);
  57.     }
  58.     /**
  59.      * @param GetResponseEvent $event
  60.      * @throws \Exception
  61.      */
  62.     public function onKernelRequest(GetResponseEvent $event)
  63.     {
  64.         if ($this->isAdminUser()) {
  65.             return;
  66.         }
  67.         $nowTime $this->scheduleUtility->getNowUTCTime();
  68.         // Frontのみ実施
  69.         $filter $this->em->getFilters()->enable('block_schedule');
  70.         $filter->setParameter('now_time'$nowTime);
  71.     }
  72.     /**
  73.      * Returns an array of event names this subscriber wants to listen to.
  74.      *
  75.      * The array keys are event names and the value can be:
  76.      *
  77.      * * The method name to call (priority defaults to 0)
  78.      * * An array composed of the method name to call and the priority
  79.      * * An array of arrays composed of the method names to call and respective
  80.      *   priorities, or 0 if unset
  81.      *
  82.      * For instance:
  83.      *
  84.      * * array('eventName' => 'methodName')
  85.      * * array('eventName' => array('methodName', $priority))
  86.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  87.      *
  88.      * @return array The event names to listen to
  89.      */
  90.     public static function getSubscribedEvents()
  91.     {
  92.         return [
  93.             KernelEvents::CONTROLLER => 'onKernelController',
  94.             // TwigInitializeListener より先に実行
  95.             KernelEvents::REQUEST => ['onKernelRequest'7]
  96.         ];
  97.     }
  98. }