<?php
/**
* Copyright(c) 2018 SYSTEM_KD
* Date: 2018/12/02
*/
namespace Plugin\Schedule\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Request\Context;
use Plugin\Schedule\Service\ScheduleService;
use Plugin\Schedule\Utils\ScheduleUtility;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class KernelEventSubscriber
* @package Plugin\Schedule\EventSubscriber
*/
class KernelEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
{
/** @var Context */
private $context;
/** @var EntityManagerInterface */
private $em;
/** @var ScheduleService */
private $scheduleService;
/** @var ScheduleUtility */
private $scheduleUtility;
public function __construct(
Context $context,
EntityManagerInterface $em,
ScheduleService $scheduleService,
ScheduleUtility $scheduleUtility,
SessionInterface $session)
{
parent::__construct($session);
$this->context = $context;
$this->em = $em;
$this->scheduleService = $scheduleService;
$this->scheduleUtility = $scheduleUtility;
}
/**
* @param FilterControllerEvent $event
* @throws \Exception
*/
public function onKernelController(FilterControllerEvent $event)
{
if ($this->context->isAdmin() || $this->isAdminUser()) {
// 管理画面へのアクセスの場合
return;
}
$nowTime = $this->scheduleUtility->getNowUTCTime();
// Frontのみ実施
$filter = $this->em->getFilters()->enable('category_schedule');
$filter->setParameter('now_time', $nowTime);
}
/**
* @param GetResponseEvent $event
* @throws \Exception
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($this->isAdminUser()) {
return;
}
$nowTime = $this->scheduleUtility->getNowUTCTime();
// Frontのみ実施
$filter = $this->em->getFilters()->enable('block_schedule');
$filter->setParameter('now_time', $nowTime);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
// TwigInitializeListener より先に実行
KernelEvents::REQUEST => ['onKernelRequest', 7]
];
}
}