app/Plugin/SDream/EventSubscriber/ShoppingIndexSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.     ここは、Symfonyの「controller実行前」イベントすべてに反応する
  4.     ⇒ 大前提:shopping 以外はスルー。
  5. */
  6. namespace Plugin\SDream\EventSubscriber;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Eccube\Controller\ShoppingController;
  13. use Psr\Log\LoggerInterface;
  14. use Eccube\Service\CartService;
  15. use Symfony\Component\Security\Core\Security;
  16. use Plugin\SDream\Service\PaymentRestrictionService;
  17. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  18. class ShoppingIndexSubscriber implements EventSubscriberInterface
  19. {
  20.     private RouterInterface $router;
  21.     private LoggerInterface $logger;
  22.     private Security $security;
  23.     private CartService $cartService;
  24.     private PaymentRestrictionService $paymentRestrictionService;
  25.     private FlashBagInterface $flashBag;
  26.     public function __construct(
  27.         RouterInterface $router,
  28.         LoggerInterface $logger,
  29.         Security $security,
  30.         CartService $cartService,
  31.         PaymentRestrictionService $paymentRestrictionService,
  32.         FlashBagInterface $flashBag       
  33.         )
  34.     {
  35.         $this->router $router;
  36.         $this->logger $logger;
  37.         $this->security $security;
  38.         $this->cartService $cartService;
  39.         $this->paymentRestrictionService $paymentRestrictionService;
  40.         $this->flashBag $flashBag;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             KernelEvents::CONTROLLER => 'onController',
  46.         ];
  47.     }
  48.     public function onController(ControllerEvent $event)
  49.     {
  50.         // メインリクエスト以外は無視
  51.         if (!$event->isMainRequest()) {
  52.             return;
  53.         }
  54.         $route $event->getRequest()->attributes->get('_route');
  55.         // shopping 以外は何もしない
  56.         if ($route !== 'shopping') {
  57.             return; //大前提:shopping 以外はスルー。
  58.         }
  59.         $controller $event->getController();
  60.         //  ShoppingControllerのみ
  61.         // 通常、[ Controllerインスタンス, 'method名' ]の形で、kernel.controller の $event->getController() を扱っているから、違う形「!is_array($controller)」の場合はスルー
  62.         if (!is_array($controller) || !$controller[0] instanceof ShoppingController) {
  63.             return;
  64.         }
  65.         $this->logger->info('onController called (test log)');
  66.         $this->logger->info('route name: '.$event->getRequest()->attributes->get('_route'));
  67.         $Cart $this->cartService->getCart();
  68.         if (!$Cart) {
  69.             return;
  70.         }
  71.         $Customer $this->security->getUser();
  72.         $allowedPayments $this->paymentRestrictionService
  73.             ->resolveAllowedPayments($Cart$Customer);
  74.         $allowedPaymentIds array_map(
  75.             fn($p) => $p->getId(),
  76.             $allowedPayments
  77.         );
  78.         if (empty($allowedPaymentIds)) {
  79.             $this->flashBag->add(
  80.                 'eccube.front.shopping.error',
  81.                 '現在のご注文内容ではご利用いただける支払方法がありません。'
  82.             );
  83.         }
  84.         /*
  85.         $this->logger->info(
  86.             '[ShoppingIndex] allowedPaymentIds',
  87.             ['allowedPaymentIds' => $allowedPaymentIds]
  88.         );
  89.         */
  90.         $event->getRequest()->attributes->set(
  91.             'allowedPaymentIds',
  92.             $allowedPaymentIds
  93.         );
  94.     }
  95. }