Add ability to inject step runners
[PREMIUM-180]
This commit is contained in:
@@ -11,6 +11,7 @@ use MailPoet\Automation\Engine\Storage\WorkflowRunStorage;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use MailPoet\Automation\Engine\Workflows\Step;
|
||||
use MailPoet\Automation\Engine\Workflows\StepRunner as StepRunnerInterface;
|
||||
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
||||
use Throwable;
|
||||
|
||||
@@ -30,6 +31,9 @@ class StepRunner {
|
||||
/** @var WorkflowStorage */
|
||||
private $workflowStorage;
|
||||
|
||||
/** @var array<string, StepRunnerInterface> */
|
||||
private $stepRunners;
|
||||
|
||||
public function __construct(
|
||||
ActionScheduler $actionScheduler,
|
||||
ActionStepRunner $actionStepRunner,
|
||||
@@ -46,6 +50,12 @@ class StepRunner {
|
||||
|
||||
public function initialize(): void {
|
||||
$this->wordPress->addAction(Hooks::WORKFLOW_STEP, [$this, 'run']);
|
||||
$this->addStepRunner(Step::TYPE_ACTION, $this->actionStepRunner);
|
||||
$this->wordPress->doAction(Hooks::STEP_RUNNER_INITIALIZE, [$this]);
|
||||
}
|
||||
|
||||
public function addStepRunner(string $stepType, StepRunnerInterface $stepRunner): void {
|
||||
$this->stepRunners[$stepType] = $stepRunner;
|
||||
}
|
||||
|
||||
/** @param mixed $args */
|
||||
@@ -98,8 +108,8 @@ class StepRunner {
|
||||
}
|
||||
|
||||
$stepType = $step->getType();
|
||||
if ($stepType === Step::TYPE_ACTION) {
|
||||
$this->actionStepRunner->run($step, $workflow, $workflowRun);
|
||||
if (isset($this->stepRunners[$stepType])) {
|
||||
$this->stepRunners[$stepType]->run($step, $workflow, $workflowRun);
|
||||
} else {
|
||||
throw new InvalidStateException();
|
||||
}
|
||||
|
@@ -5,10 +5,11 @@ namespace MailPoet\Automation\Engine\Control\Steps;
|
||||
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
|
||||
use MailPoet\Automation\Engine\Registry;
|
||||
use MailPoet\Automation\Engine\Workflows\Step;
|
||||
use MailPoet\Automation\Engine\Workflows\StepRunner;
|
||||
use MailPoet\Automation\Engine\Workflows\Workflow;
|
||||
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
||||
|
||||
class ActionStepRunner {
|
||||
class ActionStepRunner implements StepRunner {
|
||||
/** @var Registry */
|
||||
private $registry;
|
||||
|
||||
|
@@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine;
|
||||
class Hooks {
|
||||
public const INITIALIZE = 'mailpoet/automation/initialize';
|
||||
public const API_INITIALIZE = 'mailpoet/automation/api/initialize';
|
||||
public const STEP_RUNNER_INITIALIZE = 'mailpoet/automation/step_runner/initialize';
|
||||
public const TRIGGER = 'mailpoet/automation/trigger';
|
||||
public const WORKFLOW_STEP = 'mailpoet/automation/workflow/step';
|
||||
}
|
||||
|
@@ -16,10 +16,10 @@ class Step {
|
||||
private $key;
|
||||
|
||||
/** @var string|null */
|
||||
private $nextStepId;
|
||||
protected $nextStepId;
|
||||
|
||||
/** @var array */
|
||||
private $args;
|
||||
protected $args;
|
||||
|
||||
public function __construct(
|
||||
string $id,
|
||||
|
7
mailpoet/lib/Automation/Engine/Workflows/StepRunner.php
Normal file
7
mailpoet/lib/Automation/Engine/Workflows/StepRunner.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Workflows;
|
||||
|
||||
interface StepRunner {
|
||||
public function run(Step $step, Workflow $workflow, WorkflowRun $workflowRun): void;
|
||||
}
|
Reference in New Issue
Block a user