From 72ce5fb569f299b2bcdfefd56fbf16e4b1223da4 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Thu, 14 Sep 2023 14:55:00 +0200 Subject: [PATCH] Add a possibility to schedule next step by index [MAILPOET-5586] --- .../Engine/Control/StepRunController.php | 4 +++ .../Engine/Control/StepScheduler.php | 31 ++++++++++++++++--- mailpoet/lib/Automation/Engine/Exceptions.php | 8 +++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/mailpoet/lib/Automation/Engine/Control/StepRunController.php b/mailpoet/lib/Automation/Engine/Control/StepRunController.php index f63e6538e6..51b491cbf3 100644 --- a/mailpoet/lib/Automation/Engine/Control/StepRunController.php +++ b/mailpoet/lib/Automation/Engine/Control/StepRunController.php @@ -27,6 +27,10 @@ class StepRunController { return $this->stepScheduler->scheduleNextStep($this->stepRunArgs, $timestamp); } + public function scheduleNextStepByIndex(int $nextStepIndex, int $timestamp = null): int { + return $this->stepScheduler->scheduleNextStepByIndex($this->stepRunArgs, $nextStepIndex, $timestamp); + } + public function hasScheduledNextStep(): bool { return $this->stepScheduler->hasScheduledNextStep($this->stepRunArgs); } diff --git a/mailpoet/lib/Automation/Engine/Control/StepScheduler.php b/mailpoet/lib/Automation/Engine/Control/StepScheduler.php index 3f21f36605..859f3b27fd 100644 --- a/mailpoet/lib/Automation/Engine/Control/StepScheduler.php +++ b/mailpoet/lib/Automation/Engine/Control/StepScheduler.php @@ -4,6 +4,7 @@ namespace MailPoet\Automation\Engine\Control; use MailPoet\Automation\Engine\Data\AutomationRun; use MailPoet\Automation\Engine\Data\StepRunArgs; +use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Hooks; use MailPoet\Automation\Engine\Storage\AutomationRunStorage; @@ -29,16 +30,30 @@ class StepScheduler { } public function scheduleNextStep(StepRunArgs $args, int $timestamp = null): int { - $runId = $args->getAutomationRun()->getId(); + $nextSteps = $args->getStep()->getNextSteps(); // complete the automation run if there are no more steps - if (count($args->getStep()->getNextSteps()) === 0) { - $this->automationRunStorage->updateNextStep($runId, null); - $this->automationRunStorage->updateStatus($runId, AutomationRun::STATUS_COMPLETE); + if (count($nextSteps) === 0) { + $this->completeAutomationRun($args); + return 0; + } + return $this->scheduleNextStepByIndex($args, 0, $timestamp); + } + + public function scheduleNextStepByIndex(StepRunArgs $args, int $nextStepIndex, int $timestamp = null): int { + $step = $args->getStep(); + $nextStep = $step->getNextSteps()[$nextStepIndex] ?? null; + if (!$nextStep) { + throw Exceptions::nextStepNotFound($step->getId(), $nextStepIndex); + } + + $runId = $args->getAutomationRun()->getId(); + $nextStepId = $nextStep->getId(); + if (!$nextStepId) { + $this->completeAutomationRun($args); return 0; } - $nextStepId = $args->getStep()->getNextSteps()[0]->getId(); $data = $this->getActionData($runId, $nextStepId); $id = $this->scheduleStepAction($data, $timestamp); $this->automationRunStorage->updateNextStep($runId, $nextStepId); @@ -80,6 +95,12 @@ class StepScheduler { : $this->actionScheduler->schedule($timestamp, Hooks::AUTOMATION_STEP, $data); } + private function completeAutomationRun(StepRunArgs $args): void { + $runId = $args->getAutomationRun()->getId(); + $this->automationRunStorage->updateNextStep($runId, null); + $this->automationRunStorage->updateStatus($runId, AutomationRun::STATUS_COMPLETE); + } + private function getActionData(int $runId, string $stepId, int $runNumber = 1): array { return [ [ diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index 57c77d2070..75dc19b932 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -27,6 +27,7 @@ class Exceptions { private const FIELD_NOT_FOUND = 'mailpoet_automation_field_not_found'; private const FIELD_LOAD_FAILED = 'mailpoet_automation_field_load_failed'; private const FILTER_NOT_FOUND = 'mailpoet_automation_filter_not_found'; + private const NEXT_STEP_NOT_FOUND = 'mailpoet_next_step_not_found'; private const AUTOMATION_STRUCTURE_MODIFICATION_NOT_SUPPORTED = 'mailpoet_automation_structure_modification_not_supported'; private const AUTOMATION_STRUCTURE_NOT_VALID = 'mailpoet_automation_structure_not_valid'; private const AUTOMATION_STEP_MODIFIED_WHEN_UNKNOWN = 'mailpoet_automation_step_modified_when_unknown'; @@ -183,6 +184,13 @@ class Exceptions { ->withMessage(sprintf(__("Filter for field of type '%s' not found.", 'mailpoet'), $fieldType)); } + public static function nextStepNotFound(string $stepId, int $nextStepId): InvalidStateException { + return InvalidStateException::create() + ->withErrorCode(self::NEXT_STEP_NOT_FOUND) + // translators: %1$d is the ID of the automation step, %2$s is the ID of the next step. + ->withMessage(sprintf(__("Automation step with ID '%1\$s' doesn't have a next step with index '%2\$d'.", 'mailpoet'), $stepId, $nextStepId)); + } + public static function automationStructureModificationNotSupported(): UnexpectedValueException { return UnexpectedValueException::create() ->withErrorCode(self::AUTOMATION_STRUCTURE_MODIFICATION_NOT_SUPPORTED)