Add ability to inject step runners

[PREMIUM-180]
This commit is contained in:
alex-mailpoet
2022-05-24 10:18:40 +03:00
committed by Veljko V
parent 3e76aa672a
commit a1ddef0c99
6 changed files with 25 additions and 6 deletions

View File

@@ -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();
}

View File

@@ -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;

View File

@@ -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';
}

View File

@@ -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,

View 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;
}