Add basic workflow step saving to workflow update API
[MAILPOET-4420]
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
@ -82,6 +82,11 @@ class Workflow {
|
||||
return $this->steps;
|
||||
}
|
||||
|
||||
/** @param array<string, Step> $steps */
|
||||
public function setSteps(array $steps): void {
|
||||
$this->steps = $steps;
|
||||
}
|
||||
|
||||
public function getStep(string $id): ?Step {
|
||||
return $this->steps[$id] ?? null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user