app/Plugin/QuantityDiscountDx/EventSubscriber/ShoppingEventSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/03/28
  5.  */
  6. namespace Plugin\QuantityDiscountDx\EventSubscriber;
  7. use Eccube\Event\TemplateEvent;
  8. use Plugin\QuantityDiscountDx\Service\ConfigService;
  9. use Plugin\QuantityDiscountDx\Service\TwigRenderService\TwigRenderService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ShoppingEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var TwigRenderService */
  14.     protected $twigRenderService;
  15.     /** @var ConfigService */
  16.     protected $configService;
  17.     public function __construct(
  18.         TwigRenderService $twigRenderService,
  19.         ConfigService $configService
  20.     )
  21.     {
  22.         $this->twigRenderService $twigRenderService;
  23.         $this->configService $configService;
  24.     }
  25.     public function onTemplateShopping(TemplateEvent $event)
  26.     {
  27.         if ($this->configService->isKeyBool(ConfigService::SETTING_KEY_SHOPPING_VIEW)) {
  28.             $this->viewQuantityDiscount($event);
  29.         }
  30.     }
  31.     public function onTemplateShoppingConfirm(TemplateEvent $event)
  32.     {
  33.         if ($this->configService->isKeyBool(ConfigService::SETTING_KEY_CONFIRM_VIEW)) {
  34.             $this->viewQuantityDiscount($event);
  35.         }
  36.     }
  37.     private function viewQuantityDiscount(TemplateEvent $event)
  38.     {
  39.         $this->twigRenderService->initRenderService($event);
  40.         $child $this->twigRenderService
  41.             ->eachChildBuilder()
  42.             ->findThis()
  43.             ->targetFindAndIndexKey('#quantity_discount_''qdIndex')
  44.             ->setInsertModeAppend();
  45.         $this->twigRenderService
  46.             ->eachBuilder()
  47.             ->find('.ec-orderDelivery__item')
  48.             ->find('.ec-imageGrid')
  49.             ->find('.ec-imageGrid__content')
  50.             ->setEachIndexKey('qdIndex')
  51.             ->each($child->build());
  52.         $this->twigRenderService->addSupportSnippet('@QuantityDiscountDx/default/Shopping/index_ex.twig');
  53.     }
  54.     /**
  55.      * Returns an array of event names this subscriber wants to listen to.
  56.      *
  57.      * The array keys are event names and the value can be:
  58.      *
  59.      * * The method name to call (priority defaults to 0)
  60.      * * An array composed of the method name to call and the priority
  61.      * * An array of arrays composed of the method names to call and respective
  62.      *   priorities, or 0 if unset
  63.      *
  64.      * For instance:
  65.      *
  66.      * * array('eventName' => 'methodName')
  67.      * * array('eventName' => array('methodName', $priority))
  68.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  69.      *
  70.      * @return array The event names to listen to
  71.      */
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             'Shopping/index.twig' => ['onTemplateShopping'],
  76.             'Shopping/confirm.twig' => ['onTemplateShoppingConfirm'],
  77.         ];
  78.     }
  79. }