app/Plugin/ApexSetupFee/Event.php line 134

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApexSetupFee;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Entity\Order;
  5. use Eccube\Event\TemplateEvent;
  6. use Eccube\Repository\OrderRepository;
  7. use Plugin\ApexSetupFee\Form\Type\SetupFeeOrderType;
  8. use Plugin\ApexSetupFee\Repository\SetupFeeOrderRepository;
  9. use Plugin\ApexSetupFee\Repository\SetupFeeRepository;
  10. use Plugin\ApexSetupFee\Service\SetupFeeService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\FormFactoryInterface;
  13. class Event implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SetupFeeOrderRepository
  17.      */
  18.     private $setupFeeOrderRepository;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $entityManager;
  23.     /**
  24.      * @var SetupFeeRepository
  25.      */
  26.     private $setupFeeRepository;
  27.     /**
  28.      * @var SetupFeeService
  29.      */
  30.     private $SetupFeeService;
  31.     /**
  32.      * @var OrderRepository
  33.      */
  34.     private $orderRepository;
  35.     /**
  36.      * @var \Twig_Environment
  37.      */
  38.     private $twig;
  39.     /**
  40.      * @var FormFactoryInterface
  41.      */
  42.     private $formFactory;
  43.     /**
  44.      * Event constructor.
  45.      *
  46.      * @param SetupFeeOrderRepository $setupFeeOrderRepository
  47.      * @param EntityManagerInterface $entityManager
  48.      * @param SetupFeeRepository $setupFeeRepository
  49.      * @param OrderRepository $orderRepository
  50.      * @param \Twig_Environment $twig
  51.      */
  52.     public function __construct(SetupFeeOrderRepository $setupFeeOrderRepository,
  53.                                 EntityManagerInterface $entityManager,
  54.                                 SetupFeeRepository $setupFeeRepository,
  55.                                 SetupFeeService $setupFeeService,
  56.                                 OrderRepository $orderRepository,
  57.                                 \Twig_Environment $twig,
  58.                                 FormFactoryInterface $formFactory
  59.     ) {
  60.         $this->setupFeeOrderRepository $setupFeeOrderRepository;
  61.         $this->entityManager $entityManager;
  62.         $this->setupFeeRepository $setupFeeRepository;
  63.         $this->setupFeeService $setupFeeService;
  64.         $this->orderRepository $orderRepository;
  65.         $this->twig $twig;
  66.         $this->formFactory $formFactory;
  67.     }
  68.     /**
  69.      * @return array
  70.      */
  71.     public static function getSubscribedEvents()
  72.     {
  73.         return [
  74.             'Shopping/index.twig' => 'index',
  75.             'Shopping/confirm.twig' => 'index',
  76.             'Mypage/history.twig' => 'onRenderMypageHistory',
  77.         ];
  78.     }
  79.     /**
  80.      * @param TemplateEvent $event
  81.      */
  82.     public function index(TemplateEvent $event)
  83.     {
  84.         $parameters $event->getParameters();
  85.         /** @var Order $Order */
  86.         $Order $parameters['Order'];
  87.         // クーポンが未入力でクーポン情報が存在すればクーポン情報を削除
  88.         $SetupFeeOrder $this->setupFeeOrderRepository->getSetupFeeOrder($Order->getPreOrderId());
  89.         if(is_null($SetupFeeOrder)){
  90.             $SetupFee $this->setupFeeRepository->find(1);
  91.             $this->setupFeeService->saveSetupFeeOrder($Order$SetupFee);
  92.             $SetupFeeOrder $this->setupFeeOrderRepository->getSetupFeeOrder($Order->getPreOrderId());
  93.         }
  94.         $parameters['SetupFeeOrder'] = $SetupFeeOrder;
  95.         $event->setParameters($parameters);
  96.         $SetupFees_arr $this->setupFeeRepository->findBy(
  97.             ['visible' => true],
  98.             ['id' => 'ASC']
  99.         );
  100.         $SetupFees = [];
  101.         foreach ($SetupFees_arr as $v){
  102.             $SetupFees[$v->getId()] = $v;
  103.         }
  104.         $parameters['SetupFees'] = $SetupFees;
  105.         $SetupFeeForm $this->formFactory->createBuilder(SetupFeeOrderType::class, null, ['setup_fee_id' => $SetupFeeOrder->getSetupFeeId()])->getForm();
  106.         $parameters['SetupFeeForm'] = $SetupFeeForm->createView();
  107.         $event->setParameters($parameters);
  108.         if (strpos($event->getView(), 'index.twig') !== false) {
  109.             $event->addSnippet('@ApexSetupFee/default/shopping_index.twig');
  110.         } else {
  111.             $event->addSnippet('@ApexSetupFee/default/shopping_confirm.twig');
  112.         }
  113.     }
  114.     /**
  115.      * Hook point add coupon information to mypage history.
  116.      *
  117.      * @param TemplateEvent $event
  118.      */
  119.     public function onRenderMypageHistory(TemplateEvent $event)
  120.     {
  121.         log_info('SetupFee trigger onRenderMypageHistory start');
  122.         $parameters $event->getParameters();
  123.         if (is_null($parameters['Order'])) {
  124.             return;
  125.         }
  126.         $Order $parameters['Order'];
  127.         // クーポン受注情報を取得する
  128.         $SetupFeeOrder $this->setupFeeOrderRepository->findOneBy([
  129.             'order_id' => $Order->getId(),
  130.         ]);
  131.         if (is_null($SetupFeeOrder)) {
  132.             return;
  133.         }
  134.         // set parameter for twig files
  135.         $parameters['setup_fee_name'] = $SetupFeeOrder->getSetupFeeName();
  136.         $event->setParameters($parameters);
  137.         $event->addSnippet('@ApexSetupFee/default/mypage_history.twig');
  138.         log_info('Coupon trigger onRenderMypageHistory finish');
  139.     }
  140. }