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\Storage\WorkflowStorage;
use MailPoet\Automation\Engine\WordPress; use MailPoet\Automation\Engine\WordPress;
use MailPoet\Automation\Engine\Workflows\Step; use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\StepRunner as StepRunnerInterface;
use MailPoet\Automation\Engine\Workflows\WorkflowRun; use MailPoet\Automation\Engine\Workflows\WorkflowRun;
use Throwable; use Throwable;
@@ -30,6 +31,9 @@ class StepRunner {
/** @var WorkflowStorage */ /** @var WorkflowStorage */
private $workflowStorage; private $workflowStorage;
/** @var array<string, StepRunnerInterface> */
private $stepRunners;
public function __construct( public function __construct(
ActionScheduler $actionScheduler, ActionScheduler $actionScheduler,
ActionStepRunner $actionStepRunner, ActionStepRunner $actionStepRunner,
@@ -46,6 +50,12 @@ class StepRunner {
public function initialize(): void { public function initialize(): void {
$this->wordPress->addAction(Hooks::WORKFLOW_STEP, [$this, 'run']); $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 */ /** @param mixed $args */
@@ -98,8 +108,8 @@ class StepRunner {
} }
$stepType = $step->getType(); $stepType = $step->getType();
if ($stepType === Step::TYPE_ACTION) { if (isset($this->stepRunners[$stepType])) {
$this->actionStepRunner->run($step, $workflow, $workflowRun); $this->stepRunners[$stepType]->run($step, $workflow, $workflowRun);
} else { } else {
throw new InvalidStateException(); 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\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Registry; use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\Workflows\Step; use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\StepRunner;
use MailPoet\Automation\Engine\Workflows\Workflow; use MailPoet\Automation\Engine\Workflows\Workflow;
use MailPoet\Automation\Engine\Workflows\WorkflowRun; use MailPoet\Automation\Engine\Workflows\WorkflowRun;
class ActionStepRunner { class ActionStepRunner implements StepRunner {
/** @var Registry */ /** @var Registry */
private $registry; private $registry;

View File

@@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine;
class Hooks { class Hooks {
public const INITIALIZE = 'mailpoet/automation/initialize'; public const INITIALIZE = 'mailpoet/automation/initialize';
public const API_INITIALIZE = 'mailpoet/automation/api/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 TRIGGER = 'mailpoet/automation/trigger';
public const WORKFLOW_STEP = 'mailpoet/automation/workflow/step'; public const WORKFLOW_STEP = 'mailpoet/automation/workflow/step';
} }

View File

@@ -16,10 +16,10 @@ class Step {
private $key; private $key;
/** @var string|null */ /** @var string|null */
private $nextStepId; protected $nextStepId;
/** @var array */ /** @var array */
private $args; protected $args;
public function __construct( public function __construct(
string $id, 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;
}

View File

@@ -33,7 +33,7 @@ class SubscriberSubject implements Subject {
return $this->getSubscriber()->getId(); return $this->getSubscriber()->getId();
} }
), ),
'email' => new Field( 'email' => new Field(
'mailpoet:subscriber:email', 'mailpoet:subscriber:email',
Field::TYPE_STRING, Field::TYPE_STRING,