diff --git a/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php b/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php index 0ceced94d3..b34d79b63a 100644 --- a/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php +++ b/mailpoet/lib/Automation/Engine/Builder/UpdateWorkflowController.php @@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine\Builder; use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; use MailPoet\Automation\Engine\Storage\WorkflowStorage; +use MailPoet\Automation\Engine\Workflows\Step; use MailPoet\Automation\Engine\Workflows\Workflow; class UpdateWorkflowController { @@ -40,6 +41,22 @@ class UpdateWorkflowController { $changed = true; } + if (array_key_exists('steps', $data)) { + $this->validateWorkflowSteps($workflow, $data['steps']); + $steps = []; + foreach ($data['steps'] as $step) { + $steps[(string)$step['id']] = new Step( + $step['id'], + $step['type'], + $step['key'], + $step['next_step_id'] ?? null, + $step['args'] ?? null + ); + } + $workflow->setSteps($steps); + $changed = true; + } + if ($changed) { $this->storage->updateWorkflow($workflow); } @@ -56,4 +73,24 @@ class UpdateWorkflowController { throw UnexpectedValueException::create()->withMessage(__(sprintf('Invalid status: %s', $status), 'mailpoet')); } } + + private function validateWorkflowSteps(Workflow $workflow, array $steps): void { + $existingSteps = $workflow->getSteps(); + if (count($steps) !== count($existingSteps)) { + throw Exceptions::workflowStructureModificationNotSupported(); + } + + foreach ($steps as $id => $data) { + $existingStep = $existingSteps[$id] ?? null; + if ( + !$existingStep + || $data['id'] !== $existingStep->getId() + || $data['type'] !== $existingStep->getType() + || $data['key'] !== $existingStep->getKey() + || $data['next_step_id'] !== $existingStep->getNextStepId() + ) { + throw Exceptions::workflowStructureModificationNotSupported(); + } + } + } } diff --git a/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php index 1998c665a6..4847bbb5df 100644 --- a/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php +++ b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php @@ -29,10 +29,19 @@ class WorkflowsPutEndpoint extends Endpoint { } public static function getRequestSchema(): array { + $step = Builder::object([ + 'id' => Builder::string()->required(), + 'type' => Builder::string()->required(), + 'key' => Builder::string()->required(), + 'args' => Builder::object(), + 'next_step_id' => Builder::string()->nullable(), + ]); + return [ 'id' => Builder::integer()->required(), 'name' => Builder::string()->minLength(1), 'status' => Builder::string(), + 'steps' => Builder::object()->additionalProperties($step), ]; } diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index e904757cce..fb088f05a0 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -21,6 +21,7 @@ class Exceptions { private const SUBJECT_NOT_FOUND = 'mailpoet_automation_subject_not_found'; private const SUBJECT_LOAD_FAILED = 'mailpoet_automation_workflow_subject_load_failed'; private const MULTIPLE_SUBJECTS_FOUND = 'mailpoet_automation_multiple_subjects_found'; + private const WORKFLOW_STRUCTURE_MODIFICATION_NOT_SUPPORTED = 'mailpoet_automation_workflow_structure_modification_not_supported'; public function __construct() { throw new InvalidStateException( @@ -112,4 +113,10 @@ class Exceptions { ->withErrorCode(self::MULTIPLE_SUBJECTS_FOUND) ->withMessage(__(sprintf("Multiple subjects with key '%s' found, only one expected.", $key), 'mailpoet')); } + + public static function workflowStructureModificationNotSupported(): UnexpectedValueException { + return UnexpectedValueException::create() + ->withErrorCode(self::WORKFLOW_STRUCTURE_MODIFICATION_NOT_SUPPORTED) + ->withMessage(__("Workflow structure modification not supported.", 'mailpoet')); + } } diff --git a/mailpoet/lib/Automation/Engine/Workflows/Workflow.php b/mailpoet/lib/Automation/Engine/Workflows/Workflow.php index e839642f39..5dfe467311 100644 --- a/mailpoet/lib/Automation/Engine/Workflows/Workflow.php +++ b/mailpoet/lib/Automation/Engine/Workflows/Workflow.php @@ -82,6 +82,11 @@ class Workflow { return $this->steps; } + /** @param array $steps */ + public function setSteps(array $steps): void { + $this->steps = $steps; + } + public function getStep(string $id): ?Step { return $this->steps[$id] ?? null; }