app/Customize/Service/OrderStateMachine.php line 154

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.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 Customize\Service;
  13. use Eccube\Entity\Master\OrderStatus;
  14. use Eccube\Entity\Order;
  15. use Eccube\Repository\Master\OrderStatusRepository;
  16. use Eccube\Service\PurchaseFlow\Processor\PointProcessor;
  17. use Eccube\Service\PurchaseFlow\Processor\StockReduceProcessor;
  18. use Eccube\Service\PurchaseFlow\PurchaseContext;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\Workflow\Event\Event;
  21. use Symfony\Component\Workflow\StateMachine;
  22. class OrderStateMachine implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var StateMachine
  26.      */
  27.     private $machine;
  28.     /**
  29.      * @var OrderStatusRepository
  30.      */
  31.     private $orderStatusRepository;
  32.     /**
  33.      * @var PointProcessor
  34.      */
  35.     private $pointProcessor;
  36.     /**
  37.      * @var StockReduceProcessor
  38.      */
  39.     private $stockReduceProcessor;
  40.     public function __construct(StateMachine $_orderStateMachineOrderStatusRepository $orderStatusRepositoryPointProcessor $pointProcessorStockReduceProcessor $stockReduceProcessor)
  41.     {
  42.         $this->machine $_orderStateMachine;
  43.         $this->orderStatusRepository $orderStatusRepository;
  44.         $this->pointProcessor $pointProcessor;
  45.         $this->stockReduceProcessor $stockReduceProcessor;
  46.     }
  47.     /**
  48.      * 指定ステータスに遷移.
  49.      *
  50.      * @param Order $Order 受注
  51.      * @param OrderStatus $OrderStatus 遷移先ステータス
  52.      */
  53.     public function apply(Order $OrderOrderStatus $OrderStatus)
  54.     {
  55.         $context $this->newContext($Order);
  56.         $transition $this->getTransition($context$OrderStatus);
  57.         if ($transition) {
  58.             $this->machine->apply($context$transition->getName());
  59.         } else {
  60.             throw new \InvalidArgumentException();
  61.         }
  62.     }
  63.     /**
  64.      * 指定ステータスに遷移できるかどうかを判定.
  65.      *
  66.      * @param Order $Order 受注
  67.      * @param OrderStatus $OrderStatus 遷移先ステータス
  68.      *
  69.      * @return boolean 指定ステータスに遷移できる場合はtrue
  70.      */
  71.     public function can(Order $OrderOrderStatus $OrderStatus)
  72.     {
  73.         return !is_null($this->getTransition($this->newContext($Order), $OrderStatus));
  74.     }
  75.     private function getTransition(OrderStateMachineContext $contextOrderStatus $OrderStatus)
  76.     {
  77.         $transitions $this->machine->getEnabledTransitions($context);
  78.         foreach ($transitions as $t) {
  79.             if (in_array($OrderStatus->getId(), $t->getTos())) {
  80.                 return $t;
  81.             }
  82.         }
  83.         return null;
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public static function getSubscribedEvents()
  89.     {
  90.         return [
  91.             'workflow.order.completed' => ['onCompleted'],
  92. //            'workflow.order.transition.paying' => [],
  93.             'workflow.order.transition.pay' => ['updatePaymentDate'],
  94. //            'workflow.order.transition.data_create' => [],
  95. //            'workflow.order.transition.data_send' => [],
  96. //            'workflow.order.transition.approve' => [],
  97. //            'workflow.order.transition.sticker_create' => [],
  98. //            'workflow.order.transition.under_inspection' => [],
  99.             'workflow.order.transition.ship' => [['commitAddPoint']],
  100.             'workflow.order.transition.cancel' => [['rollbackStock'], ['rollbackUsePoint']],
  101.             'workflow.order.transition.return_cancel' => [['commitStock'], ['commitUsePoint']],
  102.             'workflow.order.transition.return' => [['rollbackUsePoint'], ['rollbackAddPoint']],
  103.             'workflow.order.transition.cancel_return' => [['commitUsePoint'], ['commitAddPoint']],
  104.         ];
  105.     }
  106.     /*
  107.      * Event handlers.
  108.      */
  109.     /**
  110.      * 入金日を更新する.
  111.      *
  112.      * @param Event $event
  113.      */
  114.     public function updatePaymentDate(Event $event)
  115.     {
  116.         /* @var Order $Order */
  117.         $Order $event->getSubject()->getOrder();
  118.         $Order->setPaymentDate(new \DateTime());
  119.     }
  120.     /**
  121.      * 会員の保有ポイントを減らす.
  122.      *
  123.      * @param Event $event
  124.      *
  125.      * @throws PurchaseFlow\PurchaseException
  126.      */
  127.     public function commitUsePoint(Event $event)
  128.     {
  129.         /* @var Order $Order */
  130.         $Order $event->getSubject()->getOrder();
  131.         $this->pointProcessor->prepare($Order, new PurchaseContext());
  132.     }
  133.     /**
  134.      * 利用ポイントを会員に戻す.
  135.      *
  136.      * @param Event $event
  137.      */
  138.     public function rollbackUsePoint(Event $event)
  139.     {
  140.         /* @var Order $Order */
  141.         $Order $event->getSubject()->getOrder();
  142.         $this->pointProcessor->rollback($Order, new PurchaseContext());
  143.     }
  144.     /**
  145.      * 在庫を減らす.
  146.      *
  147.      * @param Event $event
  148.      *
  149.      * @throws PurchaseFlow\PurchaseException
  150.      */
  151.     public function commitStock(Event $event)
  152.     {
  153.         /* @var Order $Order */
  154.         $Order $event->getSubject()->getOrder();
  155.         $this->stockReduceProcessor->prepare($Order, new PurchaseContext());
  156.     }
  157.     /**
  158.      * 在庫を戻す.
  159.      *
  160.      * @param Event $event
  161.      */
  162.     public function rollbackStock(Event $event)
  163.     {
  164.         /* @var Order $Order */
  165.         $Order $event->getSubject()->getOrder();
  166.         $this->stockReduceProcessor->rollback($Order, new PurchaseContext());
  167.     }
  168.     /**
  169.      * 会員に加算ポイントを付与する.
  170.      *
  171.      * @param Event $event
  172.      */
  173.     public function commitAddPoint(Event $event)
  174.     {
  175.         /* @var Order $Order */
  176.         $Order $event->getSubject()->getOrder();
  177.         $Customer $Order->getCustomer();
  178.         if ($Customer) {
  179.             $Customer->setPoint(intval($Customer->getPoint()) + intval($Order->getAddPoint()));
  180.         }
  181.     }
  182.     /**
  183.      * 会員に付与した加算ポイントを取り消す.
  184.      *
  185.      * @param Event $event
  186.      */
  187.     public function rollbackAddPoint(Event $event)
  188.     {
  189.         /* @var Order $Order */
  190.         $Order $event->getSubject()->getOrder();
  191.         $Customer $Order->getCustomer();
  192.         if ($Customer) {
  193.             $Customer->setPoint(intval($Customer->getPoint()) - intval($Order->getAddPoint()));
  194.         }
  195.     }
  196.     /**
  197.      * 受注ステータスを再設定.
  198.      * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す.
  199.      *
  200.      * @param Event $event
  201.      */
  202.     public function onCompleted(Event $event)
  203.     {
  204.         /** @var $context OrderStateMachineContext */
  205.         $context $event->getSubject();
  206.         $Order $context->getOrder();
  207.         $CompletedOrderStatus $this->orderStatusRepository->find($context->getStatus());
  208.         $Order->setOrderStatus($CompletedOrderStatus);
  209.     }
  210.     private function newContext(Order $Order)
  211.     {
  212.         return new OrderStateMachineContext((string) $Order->getOrderStatus()->getId(), $Order);
  213.     }
  214. }
  215. class OrderStateMachineContext
  216. {
  217.     /** @var string */
  218.     private $status;
  219.     /** @var Order */
  220.     private $Order;
  221.     /**
  222.      * OrderStateMachineContext constructor.
  223.      *
  224.      * @param string $status
  225.      * @param Order $Order
  226.      */
  227.     public function __construct($statusOrder $Order)
  228.     {
  229.         $this->status $status;
  230.         $this->Order $Order;
  231.     }
  232.     /**
  233.      * @return string
  234.      */
  235.     public function getStatus()
  236.     {
  237.         return $this->status;
  238.     }
  239.     /**
  240.      * @param string $status
  241.      */
  242.     public function setStatus($status)
  243.     {
  244.         $this->status $status;
  245.     }
  246.     /**
  247.      * @return Order
  248.      */
  249.     public function getOrder()
  250.     {
  251.         return $this->Order;
  252.     }
  253.     // order_state_machine.php の marking_store => property は、デフォルト値である marking を使用するよう強く推奨されている.
  254.     // EC-CUBE4.1 までは status を指定していたが、 Symfony5 よりエラーになるためエイリアスを作成して対応する.
  255.     /**
  256.      * Alias of getStatus()
  257.      */
  258.     public function getMarking(): string
  259.     {
  260.         return $this->getStatus();
  261.     }
  262.     /**
  263.      * Alias of setStatus()
  264.      */
  265.     public function setMarking(string $status): void
  266.     {
  267.         $this->setStatus($status);
  268.     }
  269. }