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;
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\Workflow; use MailPoet\Automation\Engine\Workflows\Workflow;
class UpdateWorkflowController { class UpdateWorkflowController {
@ -40,6 +41,22 @@ class UpdateWorkflowController {
$changed = true; $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) { if ($changed) {
$this->storage->updateWorkflow($workflow); $this->storage->updateWorkflow($workflow);
} }
@ -56,4 +73,24 @@ class UpdateWorkflowController {
throw UnexpectedValueException::create()->withMessage(__(sprintf('Invalid status: %s', $status), 'mailpoet')); 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();
}
}
}
} }

View File

@ -29,10 +29,19 @@ class WorkflowsPutEndpoint extends Endpoint {
} }
public static function getRequestSchema(): array { 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 [ return [
'id' => Builder::integer()->required(), 'id' => Builder::integer()->required(),
'name' => Builder::string()->minLength(1), 'name' => Builder::string()->minLength(1),
'status' => Builder::string(), 'status' => Builder::string(),
'steps' => Builder::object()->additionalProperties($step),
]; ];
} }

View File

@ -21,6 +21,7 @@ class Exceptions {
private const SUBJECT_NOT_FOUND = 'mailpoet_automation_subject_not_found'; private const SUBJECT_NOT_FOUND = 'mailpoet_automation_subject_not_found';
private const SUBJECT_LOAD_FAILED = 'mailpoet_automation_workflow_subject_load_failed'; private const SUBJECT_LOAD_FAILED = 'mailpoet_automation_workflow_subject_load_failed';
private const MULTIPLE_SUBJECTS_FOUND = 'mailpoet_automation_multiple_subjects_found'; 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() { public function __construct() {
throw new InvalidStateException( throw new InvalidStateException(
@ -112,4 +113,10 @@ class Exceptions {
->withErrorCode(self::MULTIPLE_SUBJECTS_FOUND) ->withErrorCode(self::MULTIPLE_SUBJECTS_FOUND)
->withMessage(__(sprintf("Multiple subjects with key '%s' found, only one expected.", $key), 'mailpoet')); ->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'));
}
} }

View File

@ -82,6 +82,11 @@ class Workflow {
return $this->steps; return $this->steps;
} }
/** @param array<string, Step> $steps */
public function setSteps(array $steps): void {
$this->steps = $steps;
}
public function getStep(string $id): ?Step { public function getStep(string $id): ?Step {
return $this->steps[$id] ?? null; return $this->steps[$id] ?? null;
} }