src/Eccube/Service/OrderStateMachine.php line 165

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 Eccube\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.pay' => ['updatePaymentDate'],
  93.             'workflow.order.transition.cancel' => [['rollbackStock'], ['rollbackUsePoint']],
  94.             'workflow.order.transition.back_to_in_progress' => [['commitStock'], ['commitUsePoint']],
  95.             'workflow.order.transition.ship' => [['commitAddPoint']],
  96.             'workflow.order.transition.return' => [['rollbackUsePoint'], ['rollbackAddPoint']],
  97.             'workflow.order.transition.cancel_return' => [['commitUsePoint'], ['commitAddPoint']],
  98.         ];
  99.     }
  100.     /*
  101.      * Event handlers.
  102.      */
  103.     /**
  104.      * 入金日を更新する.
  105.      *
  106.      * @param Event $event
  107.      */
  108.     public function updatePaymentDate(Event $event)
  109.     {
  110.         /* @var Order $Order */
  111.         $Order $event->getSubject()->getOrder();
  112.         // 入金日がない場合のみ更新する。
  113.         if (!$Order->getPaymentDate()) {
  114.             $Order->setPaymentDate(new \DateTime());
  115.         }
  116.     }
  117.     /**
  118.      * 会員の保有ポイントを減らす.
  119.      *
  120.      * @param Event $event
  121.      *
  122.      * @throws PurchaseFlow\PurchaseException
  123.      */
  124.     public function commitUsePoint(Event $event)
  125.     {
  126.         /* @var Order $Order */
  127.         $Order $event->getSubject()->getOrder();
  128.         $this->pointProcessor->prepare($Order, new PurchaseContext());
  129.     }
  130.     /**
  131.      * 利用ポイントを会員に戻す.
  132.      *
  133.      * @param Event $event
  134.      */
  135.     public function rollbackUsePoint(Event $event)
  136.     {
  137.         /* @var Order $Order */
  138.         $Order $event->getSubject()->getOrder();
  139.         $this->pointProcessor->rollback($Order, new PurchaseContext());
  140.     }
  141.     /**
  142.      * 在庫を減らす.
  143.      *
  144.      * @param Event $event
  145.      *
  146.      * @throws PurchaseFlow\PurchaseException
  147.      */
  148.     public function commitStock(Event $event)
  149.     {
  150.         /* @var Order $Order */
  151.         $Order $event->getSubject()->getOrder();
  152.         $this->stockReduceProcessor->prepare($Order, new PurchaseContext());
  153.     }
  154.     /**
  155.      * 在庫を戻す.
  156.      *
  157.      * @param Event $event
  158.      */
  159.     public function rollbackStock(Event $event)
  160.     {
  161.         /* @var Order $Order */
  162.         $Order $event->getSubject()->getOrder();
  163.         $this->stockReduceProcessor->rollback($Order, new PurchaseContext());
  164.     }
  165.     /**
  166.      * 会員に加算ポイントを付与する.
  167.      *
  168.      * @param Event $event
  169.      */
  170.     public function commitAddPoint(Event $event)
  171.     {
  172.         /* @var Order $Order */
  173.         $Order $event->getSubject()->getOrder();
  174.         $Customer $Order->getCustomer();
  175.         if ($Customer) {
  176.             $Customer->setPoint(intval($Customer->getPoint()) + intval($Order->getAddPoint()));
  177.         }
  178.     }
  179.     /**
  180.      * 会員に付与した加算ポイントを取り消す.
  181.      *
  182.      * @param Event $event
  183.      */
  184.     public function rollbackAddPoint(Event $event)
  185.     {
  186.         /* @var Order $Order */
  187.         $Order $event->getSubject()->getOrder();
  188.         $Customer $Order->getCustomer();
  189.         if ($Customer) {
  190.             $Customer->setPoint(intval($Customer->getPoint()) - intval($Order->getAddPoint()));
  191.         }
  192.     }
  193.     /**
  194.      * 受注ステータスを再設定.
  195.      * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す.
  196.      *
  197.      * @param Event $event
  198.      */
  199.     public function onCompleted(Event $event)
  200.     {
  201.         /** @var $context OrderStateMachineContext */
  202.         $context $event->getSubject();
  203.         $Order $context->getOrder();
  204.         $CompletedOrderStatus $this->orderStatusRepository->find($context->getStatus());
  205.         $Order->setOrderStatus($CompletedOrderStatus);
  206.     }
  207.     private function newContext(Order $Order)
  208.     {
  209.         return new OrderStateMachineContext((string) $Order->getOrderStatus()->getId(), $Order);
  210.     }
  211. }
  212. class OrderStateMachineContext
  213. {
  214.     /** @var string */
  215.     private $status;
  216.     /** @var Order */
  217.     private $Order;
  218.     /**
  219.      * OrderStateMachineContext constructor.
  220.      *
  221.      * @param string $status
  222.      * @param Order $Order
  223.      */
  224.     public function __construct($statusOrder $Order)
  225.     {
  226.         $this->status $status;
  227.         $this->Order $Order;
  228.     }
  229.     /**
  230.      * @return string
  231.      */
  232.     public function getStatus()
  233.     {
  234.         return $this->status;
  235.     }
  236.     /**
  237.      * @param string $status
  238.      */
  239.     public function setStatus($status)
  240.     {
  241.         $this->status $status;
  242.     }
  243.     /**
  244.      * @return Order
  245.      */
  246.     public function getOrder()
  247.     {
  248.         return $this->Order;
  249.     }
  250.     // order_state_machine.php の marking_store => property は、デフォルト値である marking を使用するよう強く推奨されている.
  251.     // EC-CUBE4.1 までは status を指定していたが、 Symfony5 よりエラーになるためエイリアスを作成して対応する.
  252.     /**
  253.      * Alias of getStatus()
  254.      */
  255.     public function getMarking(): string
  256.     {
  257.         return $this->getStatus();
  258.     }
  259.     /**
  260.      * Alias of setStatus()
  261.      */
  262.     public function setMarking(string $status): void
  263.     {
  264.         $this->setStatus($status);
  265.     }
  266. }