app/Plugin/ScheduleDx/EventSubscriber/UserPageEventSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2018 SYSTEM_KD
  4.  * Date: 2018/12/09
  5.  */
  6. namespace Plugin\ScheduleDx\EventSubscriber;
  7. use Eccube\Entity\Page;
  8. use Eccube\Repository\PageRepository;
  9. use Plugin\ScheduleDx\Service\ScheduleService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class UserPageEventSubscriber extends EventSubscriberBase implements EventSubscriberInterface
  16. {
  17.     private $pageRepository;
  18.     private $scheduleService;
  19.     public function __construct(
  20.         PageRepository $pageRepository,
  21.         ScheduleService $scheduleService,
  22.         RequestStack $requestStack
  23.     )
  24.     {
  25.         parent::__construct($requestStack);
  26.         $this->pageRepository $pageRepository;
  27.         $this->scheduleService $scheduleService;
  28.     }
  29.     /**
  30.      * ユーザ作成ページコントロール前
  31.      *
  32.      * @param ControllerEvent $event
  33.      * @throws \Exception
  34.      */
  35.     public function onUserDataController(ControllerEvent $event)
  36.     {
  37.         if ($this->isAdminUser()) {
  38.             return;
  39.         }
  40.         $request $event->getRequest();
  41.         $route $request->attributes->get('_route');
  42.         if ($route == "user_data") {
  43.             $detail $request->get('route');
  44.             /** @var Page $page */
  45.             $page $this->pageRepository->findOneBy(
  46.                 [
  47.                     'url' => $detail,
  48.                     'edit_type' => Page::EDIT_TYPE_USER,
  49.                 ]
  50.             );
  51.             if (null !== $page) {
  52.                 // 公開日時チェック
  53.                 if (!$this->scheduleService->isActiveTimePage($page)) {
  54.                     throw new NotFoundHttpException();
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     /**
  60.      * Returns an array of event names this subscriber wants to listen to.
  61.      *
  62.      * The array keys are event names and the value can be:
  63.      *
  64.      * * The method name to call (priority defaults to 0)
  65.      * * An array composed of the method name to call and the priority
  66.      * * An array of arrays composed of the method names to call and respective
  67.      *   priorities, or 0 if unset
  68.      *
  69.      * For instance:
  70.      *
  71.      * * array('eventName' => 'methodName')
  72.      * * array('eventName' => array('methodName', $priority))
  73.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  74.      *
  75.      * @return array The event names to listen to
  76.      */
  77.     public static function getSubscribedEvents()
  78.     {
  79.         return [
  80.             KernelEvents::CONTROLLER => [
  81.                 'onUserDataController'
  82.             ],
  83.         ];
  84.     }
  85. }