app/Customize/Event/ReceptionStopEvent.php line 117

Open in your IDE?
  1. <?php
  2. namespace Customize\Event;
  3. use Customize\Service\CustomOrderHelper;
  4. use Customize\Service\ReceptionStopService;
  5. use Eccube\Request\Context;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. /**
  12.  * 受付一時停止中は新規受付ルートを /info へリダイレクトする.
  13.  */
  14. class ReceptionStopEvent implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * 新規受付として遮断するルート名.
  18.      *
  19.      * @var string[]
  20.      */
  21.     private const DENIED_ROUTES = [
  22.         // プリント見積フォーム
  23.         'product_estimate_print_sticker',
  24.         'product_estimate_print_sticker_edit',
  25.         'print_sticker_add_estimate_cart',
  26.         'print_sticker_edit_estimate_cart_item',
  27.         'estimate_print_sticker_get_quantity_select',
  28.         'estimate_print_sticker_auto_calc',
  29.         'estimate_print_sticker_get_material_size_range',
  30.         'estimate_print_sticker_resolution_judgment',
  31.         'print_sticker_preview_slider',
  32.         // カッティング見積フォーム
  33.         'product_estimate_cutting_sticker',
  34.         'product_estimate_cutting_sticker_edit',
  35.         'cutting_sticker_add_estimate_cart',
  36.         'cutting_sticker_edit_estimate_cart_item',
  37.         'estimate_cutting_sticker_get_colors',
  38.         'estimate_cutting_sticker_auto_calc',
  39.         'estimate_cutting_sticker_get_basic_processing_fee',
  40.         'cutting_sticker_preview_slider',
  41.         // 見積カート
  42.         'estimate_cart',
  43.         'estimate_cart_item_remove',
  44.         'estimate_cart_buystep',
  45.         'estimate_cart_item_copy',
  46.         'estimate_cart_add_equipment',
  47.         // 見積申込
  48.         'estimate_request',
  49.         'estimate_request_redirect_to',
  50.         'estimate_request_login',
  51.         'estimate_request_error',
  52.         'estimate_request_shipping',
  53.         'estimate_request_shipping_edit',
  54.         'estimate_request_confirm',
  55.         'estimate_request_checkout',
  56.         'estimate_request_complete',
  57.         'estimate_request_nonmember',
  58.         'estimate_request_customer',
  59.         // 新規お問い合わせ(返信ルートは含めない)
  60.         'contact',
  61.         'contact_confirm',
  62.         'contact_complete',
  63.         'contact_search_estimate_and_order_by_email',
  64.         // 再注文入口
  65.         'mypage_reorder_item',
  66.         'mypage_order_item',
  67.         'mypage_order',
  68.     ];
  69.     /**
  70.      * 見積→注文は許可し、再注文セッション時のみ遮断する shopping 系ルート.
  71.      *
  72.      * @var string[]
  73.      */
  74.     private const SHOPPING_ROUTES = [
  75.         'shopping',
  76.         'shopping_redirect_to',
  77.         'shopping_confirm',
  78.         'shopping_checkout',
  79.         'shopping_complete',
  80.         'shopping_shipping',
  81.         'shopping_shipping_edit',
  82.         'shopping_login',
  83.         'shopping_error',
  84.         'shopping_nonmember',
  85.         'shopping_customer',
  86.     ];
  87.     /**
  88.      * @var ReceptionStopService
  89.      */
  90.     private $receptionStopService;
  91.     /**
  92.      * @var Context
  93.      */
  94.     private $requestContext;
  95.     /**
  96.      * @var UrlGeneratorInterface
  97.      */
  98.     private $urlGenerator;
  99.     public function __construct(
  100.         ReceptionStopService $receptionStopService,
  101.         Context $requestContext,
  102.         UrlGeneratorInterface $urlGenerator
  103.     ) {
  104.         $this->receptionStopService $receptionStopService;
  105.         $this->requestContext $requestContext;
  106.         $this->urlGenerator $urlGenerator;
  107.     }
  108.     public function onKernelRequest(RequestEvent $event): void
  109.     {
  110.         if (!$event->isMainRequest()) {
  111.             return;
  112.         }
  113.         if ($this->requestContext->isAdmin()) {
  114.             return;
  115.         }
  116.         if (!$this->receptionStopService->isNewReceptionStopped()) {
  117.             return;
  118.         }
  119.         $request $event->getRequest();
  120.         $route $request->attributes->get('_route');
  121.         if (!$route || $route === 'info') {
  122.             return;
  123.         }
  124.         if (in_array($routeself::DENIED_ROUTEStrue)) {
  125.             $event->setResponse($this->createInfoRedirect());
  126.             return;
  127.         }
  128.         if (in_array($routeself::SHOPPING_ROUTEStrue) && $this->isReorderShoppingSession($request)) {
  129.             $event->setResponse($this->createInfoRedirect());
  130.         }
  131.     }
  132.     /**
  133.      * 再注文フローの shopping か(見積セッションがあれば見積注文として許可).
  134.      */
  135.     private function isReorderShoppingSession($request): bool
  136.     {
  137.         if (!$request->hasSession()) {
  138.             return false;
  139.         }
  140.         $session $request->getSession();
  141.         // 見積→注文セッションがある場合は許可
  142.         if ($session->get(CustomOrderHelper::SESSION_MEMBER_ESTIMATE)
  143.             || $session->get(CustomOrderHelper::SESSION_NON_MEMBER_ESTIMATE)
  144.         ) {
  145.             return false;
  146.         }
  147.         return (bool) (
  148.             $session->get(CustomOrderHelper::SESSION_REORDER_ORDER_ITEM_ID)
  149.             || $session->get(CustomOrderHelper::SESSION_FAVORITE_ID)
  150.             || $session->get(CustomOrderHelper::SESSION_REORDER_PRE_ORDER_ID)
  151.         );
  152.     }
  153.     private function createInfoRedirect(): RedirectResponse
  154.     {
  155.         return new RedirectResponse($this->urlGenerator->generate('info'));
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public static function getSubscribedEvents()
  161.     {
  162.         // RouterListener(32) の後に _route を参照する
  163.         return [
  164.             KernelEvents::REQUEST => ['onKernelRequest'16],
  165.         ];
  166.     }
  167. }