<?php
/**
* Copyright(c) 2018 SYSTEM_KD
* Date: 2018/12/09
*/
namespace Plugin\Schedule\EventSubscriber;
use Eccube\Controller\Admin\Content\FileController;
use Eccube\Entity\Page;
use Eccube\Repository\PageRepository;
use Plugin\Schedule\Service\ScheduleService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class UserPageEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
{
private $pageRepository;
private $scheduleService;
public function __construct(
PageRepository $pageRepository,
ScheduleService $scheduleService,
SessionInterface $session
)
{
parent::__construct($session);
$this->pageRepository = $pageRepository;
$this->scheduleService = $scheduleService;
$this->session = $session;
}
/**
* ユーザ作成ページコントロール前
*
* @param FilterControllerEvent $event
* @throws \Exception
*/
public function onUserDataController(FilterControllerEvent $event)
{
if ($this->isAdminUser()) {
return;
}
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if ($route == "user_data") {
$detail = $request->get('route');
/** @var Page $page */
$page = $this->pageRepository->findOneBy(
[
'url' => $detail,
'edit_type' => Page::EDIT_TYPE_USER,
]
);
if (null !== $page) {
// 公開日時チェック
if (!$this->scheduleService->isActiveTimePage($page)) {
throw new NotFoundHttpException();
}
}
}
}
/**
* 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 => [
'onUserDataController'
],
];
}
}