app/Plugin/ScheduleDx/EventSubscriber/KernelEventSubscriber.php line 59

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