app/Customize/Service/EstimateStateMachine.php line 158

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 Customize\Entity\Estimate;
  14. use Customize\Entity\Master\EstimateStatus;
  15. use Customize\Repository\Master\EstimateStatusRepository;
  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 EstimateStateMachine implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var StateMachine
  26.      */
  27.     private $machine;
  28.     /**
  29.      * @var EstimateStatusRepository
  30.      */
  31.     private $estimateStatusRepository;
  32.     /**
  33.      * @var PointProcessor
  34.      */
  35.     private $pointProcessor;
  36.     /**
  37.      * @var StockReduceProcessor
  38.      */
  39.     private $stockReduceProcessor;
  40.     public function __construct(StateMachine $_estimateStateMachineEstimateStatusRepository $estimateStatusRepositoryPointProcessor $pointProcessorStockReduceProcessor $stockReduceProcessor)
  41.     {
  42.         $this->machine $_estimateStateMachine;
  43.         $this->estimateStatusRepository $estimateStatusRepository;
  44.         $this->pointProcessor $pointProcessor;
  45.         $this->stockReduceProcessor $stockReduceProcessor;
  46.     }
  47.     /**
  48.      * 指定ステータスに遷移.
  49.      *
  50.      * @param Estimate $Estimate 受注
  51.      * @param EstimateStatus $EstimateStatus 遷移先ステータス
  52.      */
  53.     public function apply(Estimate $EstimateEstimateStatus $EstimateStatus)
  54.     {
  55.         $context $this->newContext($Estimate);
  56.         $transition $this->getTransition($context$EstimateStatus);
  57.         if ($transition) {
  58.             $this->machine->apply($context$transition->getName());
  59.         } else {
  60.             throw new \InvalidArgumentException();
  61.         }
  62.     }
  63.     /**
  64.      * 指定ステータスに遷移できるかどうかを判定.
  65.      *
  66.      * @param Estimate $Estimate 受注
  67.      * @param EstimateStatus $EstimateStatus 遷移先ステータス
  68.      *
  69.      * @return boolean 指定ステータスに遷移できる場合はtrue
  70.      */
  71.     public function can(Estimate $EstimateEstimateStatus $EstimateStatus)
  72.     {
  73.         return !is_null($this->getTransition($this->newContext($Estimate), $EstimateStatus));
  74.     }
  75.     private function getTransition(EstimateStateMachineContext $contextEstimateStatus $EstimateStatus)
  76.     {
  77.         $transitions $this->machine->getEnabledTransitions($context);
  78.         foreach ($transitions as $t) {
  79.             if (in_array($EstimateStatus->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.estimate.completed' => ['onCompleted'],
  92.             'workflow.estimate.transition.return_fixed' => ['returnFixed'],
  93.             'workflow.estimate.transition.return_new' => ['returnNewAndInProgress'],
  94.             'workflow.estimate.transition.return_in_progress' => ['returnNewAndInProgress'],
  95. //            'workflow.estimate.transition.in_progress' => [],
  96. //            'workflow.estimate.transition.approval_pending' => [],
  97. //            'workflow.estimate.transition.fixed' => [],
  98. //            'workflow.estimate.transition.ordered' => [],
  99. //            'workflow.estimate.transition.cancel' => [],
  100.         ];
  101.     }
  102.     /*
  103.      * Event handlers.
  104.      */
  105.     // @TODO ここにイベント処理を記述する。このファイルを参照 `app/Customize/Service/OrderStateMachine.php`
  106.     /**
  107.      * 見積ステータスを再設定.
  108.      * {@link StateMachine}によって遷移が終了したときには{@link Estimate#EstimateStatus}のidが変更されるだけなのでEstimateStatusを設定し直す.
  109.      *
  110.      * @param Event $event
  111.      */
  112.     public function onCompleted(Event $event)
  113.     {
  114.         /** @var $context EstimateStateMachineContext */
  115.         $context $event->getSubject();
  116.         $Estimate $context->getEstimate();
  117.         $CompletedEstimateStatus $this->estimateStatusRepository->find($context->getStatus());
  118.         $Estimate->setEstimateStatus($CompletedEstimateStatus);
  119.     }
  120.     /**
  121.      * 見積確定から見積対応中へステータスの変更があった場合、Estimateのデータを更新する
  122.      *
  123.      * @param Event $event
  124.      */
  125.     public function returnFixed(Event $event)
  126.     {
  127.         /** @var $context EstimateStateMachineContext */
  128.         $context $event->getSubject();
  129.         $Estimate $context->getEstimate();
  130.         $Estimate->setDesignConfirmFlg(0);
  131.         $Estimate->setEstimateFix(0);
  132.         $this->returnNewAndInProgress($event);
  133.     }
  134.     /**
  135.      * 見積対応中以前のステップへステータスが戻った場合、
  136.      * EstimateShipping.shipping_method_select_conditions(配送方法の選択条件)を更新する
  137.      *
  138.      * @param Event $event
  139.      */
  140.     public function returnNewAndInProgress(Event $event)
  141.     {
  142.         /** @var $context EstimateStateMachineContext */
  143.         $context $event->getSubject();
  144.         $Estimate $context->getEstimate();
  145.         $EstimateShipping $Estimate->getShippings()[0];
  146.         if ( $EstimateShipping ) {
  147.             $EstimateShipping->setShippingMethodSelectConditions(null);
  148.         }
  149.     }
  150.     private function newContext(Estimate $Estimate)
  151.     {
  152.         return new EstimateStateMachineContext((string) $Estimate->getEstimateStatus()->getId(), $Estimate);
  153.     }
  154. }
  155. class EstimateStateMachineContext
  156. {
  157.     /** @var string */
  158.     private $status;
  159.     /** @var Estimate */
  160.     private $Estimate;
  161.     /**
  162.      * OrderStateMachineContext constructor.
  163.      *
  164.      * @param string $status
  165.      * @param Estimate $Estimate
  166.      */
  167.     public function __construct($statusEstimate $Estimate)
  168.     {
  169.         $this->status $status;
  170.         $this->Estimate $Estimate;
  171.     }
  172.     /**
  173.      * @return string
  174.      */
  175.     public function getStatus()
  176.     {
  177.         return $this->status;
  178.     }
  179.     /**
  180.      * @param string $status
  181.      */
  182.     public function setStatus($status)
  183.     {
  184.         $this->status $status;
  185.     }
  186.     /**
  187.      * @return Estimate
  188.      */
  189.     public function getEstimate()
  190.     {
  191.         return $this->Estimate;
  192.     }
  193.     // estimate_state_machine.php の marking_store => property は、デフォルト値である marking を使用するよう強く推奨されている.
  194.     // EC-CUBE4.1 までは status を指定していたが、 Symfony5 よりエラーになるためエイリアスを作成して対応する.
  195.     /**
  196.      * Alias of getStatus()
  197.      */
  198.     public function getMarking(): string
  199.     {
  200.         return $this->getStatus();
  201.     }
  202.     /**
  203.      * Alias of setStatus()
  204.      */
  205.     public function setMarking(string $status): void
  206.     {
  207.         $this->setStatus($status);
  208.     }
  209. }