Explicitly handle optional workflow ID and version ID, fix missing ID for new workflows

[MAILPOET-4629]
This commit is contained in:
Jan Jakes
2022-09-23 11:17:07 +02:00
committed by David Remer
parent 02aaba1ded
commit f4b3f9bf86
2 changed files with 22 additions and 13 deletions

View File

@ -3,6 +3,7 @@
namespace MailPoet\Automation\Engine\Data; namespace MailPoet\Automation\Engine\Data;
use DateTimeImmutable; use DateTimeImmutable;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Utils\Json; use MailPoet\Automation\Engine\Utils\Json;
class Workflow { class Workflow {
@ -17,10 +18,10 @@ class Workflow {
self::STATUS_TRASH, self::STATUS_TRASH,
]; ];
/** @var int */ /** @var int|null */
private $id; private $id;
/** @var int */ /** @var int|null */
private $versionId; private $versionId;
/** @var string */ /** @var string */
@ -55,13 +56,8 @@ class Workflow {
$this->name = $name; $this->name = $name;
$this->steps = $steps; $this->steps = $steps;
$this->author = $author; $this->author = $author;
$this->id = $id;
if ($id) { $this->versionId = $versionId;
$this->id = $id;
}
if ($versionId) {
$this->versionId = $versionId;
}
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
$this->createdAt = $now; $this->createdAt = $now;
@ -69,10 +65,16 @@ class Workflow {
} }
public function getId(): int { public function getId(): int {
if (!$this->id) {
throw InvalidStateException::create()->withMessage('No workflow ID was set');
}
return $this->id; return $this->id;
} }
public function getVersionId(): int { public function getVersionId(): int {
if (!$this->versionId) {
throw InvalidStateException::create()->withMessage('No workflow version ID was set');
}
return $this->versionId; return $this->versionId;
} }

View File

@ -5,6 +5,7 @@ namespace MailPoet\Automation\Engine\Validation;
use MailPoet\Automation\Engine\Data\Step; use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Data\Workflow; use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Registry; use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Validator\Validator; use MailPoet\Validator\Validator;
@ -42,7 +43,7 @@ class WorkflowStepsValidator {
$registryStep = $this->registry->getStep($step->getKey()); $registryStep = $this->registry->getStep($step->getKey());
if (!$registryStep) { if (!$registryStep) {
// step not registered (e.g. plugin was deactivated) - allow saving it only if it hasn't changed // 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; $currentStep = $currentWorkflow ? ($currentWorkflow->getSteps()[$step->getId()] ?? null) : null;
if (!$currentStep || $step->toArray() !== $currentStep->toArray()) { if (!$currentStep || $step->toArray() !== $currentStep->toArray()) {
throw Exceptions::workflowStepModifiedWhenUnknown($step); throw Exceptions::workflowStepModifiedWhenUnknown($step);
@ -56,9 +57,15 @@ class WorkflowStepsValidator {
} }
} }
private function getCurrentWorkflow(int $id): ?Workflow { private function getCurrentWorkflow(Workflow $workflow): ?Workflow {
if ($this->cachedExistingWorkflow === false) { try {
$this->cachedExistingWorkflow = $this->workflowStorage->getWorkflow($id); $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; return $this->cachedExistingWorkflow;
} }