Add basic workflow step saving to workflow update API

[MAILPOET-4420]
This commit is contained in:
Jan Jakes
2022-07-15 14:40:10 +02:00
committed by Veljko V
parent 55fb3f6c82
commit 7da2d2f7c8
4 changed files with 58 additions and 0 deletions

View File

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