app/Plugin/JoolenZR42/EventListener/EventListener.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3.  * Plugin Name: JoolenZR42
  4.  *
  5.  * Copyright(c) joolen inc. All Rights Reserved.
  6.  *
  7.  * https://www.joolen.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\JoolenZR42\EventListener;
  13. use Eccube\Entity\Order;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Event\TemplateEvent;
  18. use Eccube\Repository\OrderRepository;
  19. use Eccube\Repository\PluginRepository;
  20. use Eccube\Service\CartService;
  21. use Eccube\Service\OrderHelper;
  22. use Eccube\Entity\Master\OrderStatus;
  23. use Plugin\JoolenZR42\Repository\ConfigRepository;
  24. use Plugin\JoolenZR42\Service\MailService;
  25. class EventListener implements EventSubscriberInterface
  26. {
  27.     const SESSION_ZRCONNECT_ORDER_ID 'eccube.plugin.joolen_zr42.order.id';
  28.     /**
  29.      * ログ表示用
  30.      * @var string
  31.      */
  32.     protected $methodName '[Joolen ZR Plugin] ';
  33.     /**
  34.      * @var ConfigRepository
  35.      */
  36.     protected $configRepository;
  37.     /**
  38.      * @var OrderRepository
  39.      */
  40.     protected $orderRepository;
  41.     /**
  42.      * @var Session
  43.      */
  44.     protected $session;
  45.     /**
  46.      * @var CartService
  47.      */
  48.     protected $cartService;
  49.     /**
  50.      * @var OrderHelper
  51.      */
  52.     protected $orderHelper;
  53.     /**
  54.      * @var MailService
  55.      */
  56.     private $mailService;
  57.     public function __construct(
  58.         RequestStack $requestStack,
  59.         ConfigRepository $configRepository,
  60.         OrderRepository $orderRepository,
  61.         PluginRepository $PluginRepository,
  62.         CartService $cartService,
  63.         OrderHelper $orderHelper,
  64.         MailService $mailService
  65.     ) {
  66.         $this->session $requestStack->getSession();
  67.         $this->cartService $cartService;
  68.         $this->orderHelper $orderHelper;
  69.         $this->orderRepository $orderRepository;
  70.         $this->configRepository $configRepository;
  71.         $this->mailService $mailService;
  72.         $this->Plugin $PluginRepository->findOneBy([
  73.             'code' => 'JoolenZR42',
  74.             'enabled' => true,
  75.         ]);
  76.     }
  77.     /**
  78.      * @return array
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             // 受注IDをセッションに保管するタイミング: 注文確認画面
  84.             // 決済のプラグインによっては、注文完了画面でOrder.idが消されてしまい取得できないため、予め確保しておく
  85.             'Shopping/index.twig' => 'setOrderIdToSession',
  86.             // 購入完了時のメール送信のタイミング: 注文完了画面信
  87.             'front.shopping.complete.initialize' => 'onSendOrderMailToZR',
  88.         ];
  89.     }
  90.     /**
  91.      * セッションにOrder.idを保存
  92.      * @param TemplateEvent $event
  93.      * @return void
  94.      */
  95.     public function setOrderIdToSession(TemplateEvent $event)
  96.     {
  97.         if (!$this->Plugin) {
  98.             return;
  99.         }
  100.         $Order $this->getOrderFromCart();
  101.         if ($Order) {
  102.             log_info($this->methodName '受注IDをセッションに保存 Order.id[' $Order->getId() . ']');
  103.             $this->session->set(self::SESSION_ZRCONNECT_ORDER_ID$Order->getId());
  104.         }
  105.     }
  106.     /**
  107.      * カートからOrderを取得
  108.      * @return Order|null
  109.      */
  110.     private function getOrderFromCart()
  111.     {
  112.         // カートの取得
  113.         $Cart $this->cartService->getCart();
  114.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  115.             // カートが購入フローへ遷移できない状態はnullを返す
  116.             log_info($this->methodName 'カート不正のため CartからOrderを取得できず1');
  117.             return null;
  118.         }
  119.         // Order取得
  120.         if ($Order $this->orderHelper->getPurchaseProcessingOrder($Cart->getPreOrderId())) {
  121.             return $Order;
  122.         }
  123.         log_info($this->methodName 'カート不正のため CartからOrderを取得できず2');
  124.         return null;
  125.     }
  126.     /**
  127.      * 在庫ロボットへ受注メールを送信する
  128.      * @param EventArgs $event
  129.      * @return void
  130.      * @throws \Twig\Error\LoaderError
  131.      * @throws \Twig\Error\RuntimeError
  132.      * @throws \Twig\Error\SyntaxError
  133.      */
  134.     public function onSendOrderMailToZR(EventArgs $event)
  135.     {
  136.         if (!$this->Plugin) {
  137.             return;
  138.         }
  139.         $methodName $this->methodName '在庫ロボット受注メール [フロント購入時の送信] ';
  140.         $Order $this->getOrder($event$methodName);
  141.         if (!$Order) {
  142.             $errorMessage ' Orderを取得できないため、在庫ロボット受注メールを送信できませんでした';
  143.             log_info($methodName '[失敗]' $errorMessage);
  144.             return;
  145.         }
  146.         $checked $this->checkOrder($Order$methodName);
  147.         if (!$checked) {
  148.             return;
  149.         }
  150.         $Config $this->configRepository->get();
  151.         if (!$Config || is_null($Config->getEmail())) {
  152.             $errorMessage '送信情報の登録がないため、在庫ロボット受注メールを送信できませんでした';
  153.             log_info($methodName '[失敗] ' $errorMessage);
  154.             return;
  155.         }
  156.         log_info($methodName '送信[開始]');
  157.         $this->mailService->sendOrderMailToZRFront($Config->getEmail(), $Order);
  158.         log_info($methodName '送信[完了]');
  159.     }
  160.     /**
  161.      * @param EventArgs $event
  162.      * @param string $methodName
  163.      * @return Order|null
  164.      */
  165.     private function getOrder(EventArgs $eventstring $methodName)
  166.     {
  167.         $Order $event->getArgument('Order');
  168.         if ($Order) {
  169.             return $Order;
  170.         }
  171.         // 決済プラグインによってはOrderを取得できない時があるので、独自セッションから取得を試みる
  172.         $orderId $this->session->get(self::SESSION_ZRCONNECT_ORDER_ID);
  173.         $this->session->remove(self::SESSION_ZRCONNECT_ORDER_ID);
  174.         log_info($methodName '通常処理でOrderを取得できないため、独自セッションからorder_idを取得した。 order_id:[' $orderId ']');
  175.         return $this->orderRepository->find($orderId);
  176.     }
  177.     /**
  178.      * @param Order $Order
  179.      * @param string $methodName
  180.      * @return bool
  181.      */
  182.     private function checkOrder(Order $Orderstring $methodName): bool
  183.     {
  184.         // ログメッセージにOrder.id情報を追加
  185.         $methodName .= 'Order.id[' $Order->getId() . '] ';
  186.         // 決済が完了していない場合は終了
  187.         if (!$Order->getOrderStatus()) {
  188.             $errorMessage '受注ステータスが確認できないため、在庫ロボット受注メールを送信できませんでした';
  189.             log_info($methodName '[失敗] ' $errorMessage);
  190.             return false;
  191.         }
  192.         if ($Order->getOrderStatus()->getId() === OrderStatus::PENDING || // 決済処理中
  193.             $Order->getOrderStatus()->getId() === OrderStatus::PROCESSING // 購入処理中
  194.         ) {
  195.             $errorMessage '受注ステータスが不正なため、在庫ロボット受注メールを送信できませんでした。OrderStatus.id[' $Order->getOrderStatus()->getId() . ']';
  196.             log_info($methodName '[失敗] ' $errorMessage);
  197.             return false;
  198.         }
  199.         return true;
  200.     }
  201. }