<?php
namespace Customize\Event;
use Customize\Service\CustomOrderHelper;
use Customize\Service\ReceptionStopService;
use Eccube\Request\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* 受付一時停止中は新規受付ルートを /info へリダイレクトする.
*/
class ReceptionStopEvent implements EventSubscriberInterface
{
/**
* 新規受付として遮断するルート名.
*
* @var string[]
*/
private const DENIED_ROUTES = [
// プリント見積フォーム
'product_estimate_print_sticker',
'product_estimate_print_sticker_edit',
'print_sticker_add_estimate_cart',
'print_sticker_edit_estimate_cart_item',
'estimate_print_sticker_get_quantity_select',
'estimate_print_sticker_auto_calc',
'estimate_print_sticker_get_material_size_range',
'estimate_print_sticker_resolution_judgment',
'print_sticker_preview_slider',
// カッティング見積フォーム
'product_estimate_cutting_sticker',
'product_estimate_cutting_sticker_edit',
'cutting_sticker_add_estimate_cart',
'cutting_sticker_edit_estimate_cart_item',
'estimate_cutting_sticker_get_colors',
'estimate_cutting_sticker_auto_calc',
'estimate_cutting_sticker_get_basic_processing_fee',
'cutting_sticker_preview_slider',
// 見積カート
'estimate_cart',
'estimate_cart_item_remove',
'estimate_cart_buystep',
'estimate_cart_item_copy',
'estimate_cart_add_equipment',
// 見積申込
'estimate_request',
'estimate_request_redirect_to',
'estimate_request_login',
'estimate_request_error',
'estimate_request_shipping',
'estimate_request_shipping_edit',
'estimate_request_confirm',
'estimate_request_checkout',
'estimate_request_complete',
'estimate_request_nonmember',
'estimate_request_customer',
// 新規お問い合わせ(返信ルートは含めない)
'contact',
'contact_confirm',
'contact_complete',
'contact_search_estimate_and_order_by_email',
// 再注文入口
'mypage_reorder_item',
'mypage_order_item',
'mypage_order',
];
/**
* 見積→注文は許可し、再注文セッション時のみ遮断する shopping 系ルート.
*
* @var string[]
*/
private const SHOPPING_ROUTES = [
'shopping',
'shopping_redirect_to',
'shopping_confirm',
'shopping_checkout',
'shopping_complete',
'shopping_shipping',
'shopping_shipping_edit',
'shopping_login',
'shopping_error',
'shopping_nonmember',
'shopping_customer',
];
/**
* @var ReceptionStopService
*/
private $receptionStopService;
/**
* @var Context
*/
private $requestContext;
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
public function __construct(
ReceptionStopService $receptionStopService,
Context $requestContext,
UrlGeneratorInterface $urlGenerator
) {
$this->receptionStopService = $receptionStopService;
$this->requestContext = $requestContext;
$this->urlGenerator = $urlGenerator;
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
if ($this->requestContext->isAdmin()) {
return;
}
if (!$this->receptionStopService->isNewReceptionStopped()) {
return;
}
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (!$route || $route === 'info') {
return;
}
if (in_array($route, self::DENIED_ROUTES, true)) {
$event->setResponse($this->createInfoRedirect());
return;
}
if (in_array($route, self::SHOPPING_ROUTES, true) && $this->isReorderShoppingSession($request)) {
$event->setResponse($this->createInfoRedirect());
}
}
/**
* 再注文フローの shopping か(見積セッションがあれば見積注文として許可).
*/
private function isReorderShoppingSession($request): bool
{
if (!$request->hasSession()) {
return false;
}
$session = $request->getSession();
// 見積→注文セッションがある場合は許可
if ($session->get(CustomOrderHelper::SESSION_MEMBER_ESTIMATE)
|| $session->get(CustomOrderHelper::SESSION_NON_MEMBER_ESTIMATE)
) {
return false;
}
return (bool) (
$session->get(CustomOrderHelper::SESSION_REORDER_ORDER_ITEM_ID)
|| $session->get(CustomOrderHelper::SESSION_FAVORITE_ID)
|| $session->get(CustomOrderHelper::SESSION_REORDER_PRE_ORDER_ID)
);
}
private function createInfoRedirect(): RedirectResponse
{
return new RedirectResponse($this->urlGenerator->generate('info'));
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
// RouterListener(32) の後に _route を参照する
return [
KernelEvents::REQUEST => ['onKernelRequest', 16],
];
}
}