vendor/shopware/core/Content/Flow/Dispatching/FlowExecutor.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching;
  3. use Shopware\Core\Content\Flow\Dispatching\Struct\ActionSequence;
  4. use Shopware\Core\Content\Flow\Dispatching\Struct\Flow;
  5. use Shopware\Core\Content\Flow\Dispatching\Struct\IfSequence;
  6. use Shopware\Core\Content\Flow\Dispatching\Struct\Sequence;
  7. use Shopware\Core\Content\Flow\Exception\ExecuteSequenceException;
  8. use Shopware\Core\Framework\App\Event\AppFlowActionEvent;
  9. use Shopware\Core\Framework\App\FlowAction\AppFlowActionProvider;
  10. use Shopware\Core\Framework\Event\FlowEvent;
  11. use Shopware\Core\Framework\Event\FlowEventAware;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. /**
  14.  * @internal not intended for decoration or replacement
  15.  */
  16. class FlowExecutor
  17. {
  18.     private EventDispatcherInterface $dispatcher;
  19.     private AppFlowActionProvider $appFlowActionProvider;
  20.     public function __construct(EventDispatcherInterface $dispatcherAppFlowActionProvider $appFlowActionProvider)
  21.     {
  22.         $this->dispatcher $dispatcher;
  23.         $this->appFlowActionProvider $appFlowActionProvider;
  24.     }
  25.     public function execute(Flow $flowFlowEventAware $event): void
  26.     {
  27.         $state = new FlowState($event);
  28.         $state->flowId $flow->getId();
  29.         foreach ($flow->getSequences() as $sequence) {
  30.             $state->sequenceId $sequence->sequenceId;
  31.             $state->delayed false;
  32.             try {
  33.                 $this->executeSequence($sequence$state);
  34.             } catch (\Exception $e) {
  35.                 throw new ExecuteSequenceException($sequence->flowId$sequence->sequenceId$e->getMessage(), $e->getCode(), $e);
  36.             }
  37.             if ($state->stop) {
  38.                 return;
  39.             }
  40.         }
  41.     }
  42.     public function executeSequence(?Sequence $sequenceFlowState $state): void
  43.     {
  44.         if ($sequence === null) {
  45.             return;
  46.         }
  47.         $state->currentSequence $sequence;
  48.         if ($sequence instanceof IfSequence) {
  49.             $this->executeIf($sequence$state);
  50.             return;
  51.         }
  52.         if ($sequence instanceof ActionSequence) {
  53.             $this->executeAction($sequence$state);
  54.         }
  55.     }
  56.     public function executeAction(ActionSequence $sequenceFlowState $state): void
  57.     {
  58.         $actionName $sequence->action;
  59.         if (!$actionName) {
  60.             return;
  61.         }
  62.         if ($state->stop) {
  63.             return;
  64.         }
  65.         $globalEvent = new FlowEvent($actionName$state$sequence->config);
  66.         if ($sequence->appFlowActionId) {
  67.             $eventData $this->appFlowActionProvider->getWebhookData($globalEvent$sequence->appFlowActionId);
  68.             $globalEvent = new AppFlowActionEvent(
  69.                 $actionName,
  70.                 $eventData['headers'],
  71.                 $eventData['payload']
  72.             );
  73.         }
  74.         $this->dispatcher->dispatch($globalEvent$actionName);
  75.         if ($state->delayed) {
  76.             return;
  77.         }
  78.         $state->currentSequence $sequence;
  79.         /** @var ActionSequence $nextAction */
  80.         $nextAction $sequence->nextAction;
  81.         if ($nextAction !== null) {
  82.             $this->executeAction($nextAction$state);
  83.         }
  84.     }
  85.     public function executeIf(IfSequence $sequenceFlowState $state): void
  86.     {
  87.         if (\in_array($sequence->ruleId$state->event->getContext()->getRuleIds(), true)) {
  88.             $this->executeSequence($sequence->trueCase$state);
  89.             return;
  90.         }
  91.         $this->executeSequence($sequence->falseCase$state);
  92.     }
  93. }