Explicitly handle optional workflow ID and version ID, fix missing ID for new workflows
[MAILPOET-4629]
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Automation\Engine\Data;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
|
||||
use MailPoet\Automation\Engine\Utils\Json;
|
||||
|
||||
class Workflow {
|
||||
@ -17,10 +18,10 @@ class Workflow {
|
||||
self::STATUS_TRASH,
|
||||
];
|
||||
|
||||
/** @var int */
|
||||
/** @var int|null */
|
||||
private $id;
|
||||
|
||||
/** @var int */
|
||||
/** @var int|null */
|
||||
private $versionId;
|
||||
|
||||
/** @var string */
|
||||
@ -55,13 +56,8 @@ class Workflow {
|
||||
$this->name = $name;
|
||||
$this->steps = $steps;
|
||||
$this->author = $author;
|
||||
|
||||
if ($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
if ($versionId) {
|
||||
$this->versionId = $versionId;
|
||||
}
|
||||
|
||||
$now = new DateTimeImmutable();
|
||||
$this->createdAt = $now;
|
||||
@ -69,10 +65,16 @@ class Workflow {
|
||||
}
|
||||
|
||||
public function getId(): int {
|
||||
if (!$this->id) {
|
||||
throw InvalidStateException::create()->withMessage('No workflow ID was set');
|
||||
}
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getVersionId(): int {
|
||||
if (!$this->versionId) {
|
||||
throw InvalidStateException::create()->withMessage('No workflow version ID was set');
|
||||
}
|
||||
return $this->versionId;
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine\Validation;
|
||||
use MailPoet\Automation\Engine\Data\Step;
|
||||
use MailPoet\Automation\Engine\Data\Workflow;
|
||||
use MailPoet\Automation\Engine\Exceptions;
|
||||
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
|
||||
use MailPoet\Automation\Engine\Registry;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||
use MailPoet\Validator\Validator;
|
||||
@ -42,7 +43,7 @@ class WorkflowStepsValidator {
|
||||
$registryStep = $this->registry->getStep($step->getKey());
|
||||
if (!$registryStep) {
|
||||
// step not registered (e.g. plugin was deactivated) - allow saving it only if it hasn't changed
|
||||
$currentWorkflow = $this->getCurrentWorkflow($workflow->getId());
|
||||
$currentWorkflow = $this->getCurrentWorkflow($workflow);
|
||||
$currentStep = $currentWorkflow ? ($currentWorkflow->getSteps()[$step->getId()] ?? null) : null;
|
||||
if (!$currentStep || $step->toArray() !== $currentStep->toArray()) {
|
||||
throw Exceptions::workflowStepModifiedWhenUnknown($step);
|
||||
@ -56,10 +57,16 @@ class WorkflowStepsValidator {
|
||||
}
|
||||
}
|
||||
|
||||
private function getCurrentWorkflow(int $id): ?Workflow {
|
||||
private function getCurrentWorkflow(Workflow $workflow): ?Workflow {
|
||||
try {
|
||||
$id = $workflow->getId();
|
||||
if ($this->cachedExistingWorkflow === false) {
|
||||
$this->cachedExistingWorkflow = $this->workflowStorage->getWorkflow($id);
|
||||
}
|
||||
} catch (InvalidStateException $e) {
|
||||
// for new workflows, no workflow ID is set
|
||||
$this->cachedExistingWorkflow = null;
|
||||
}
|
||||
return $this->cachedExistingWorkflow;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user