src/Eccube/Service/OrderStateMachine.php line 123

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