diff --git a/mailpoet/lib/Automation/Engine/Data/Workflow.php b/mailpoet/lib/Automation/Engine/Data/Workflow.php index 43eb7b552a..2e7d166110 100644 --- a/mailpoet/lib/Automation/Engine/Data/Workflow.php +++ b/mailpoet/lib/Automation/Engine/Data/Workflow.php @@ -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; - } + $this->id = $id; + $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; } diff --git a/mailpoet/lib/Automation/Engine/Validation/WorkflowStepsValidator.php b/mailpoet/lib/Automation/Engine/Validation/WorkflowStepsValidator.php index a0c33db179..ed837f91b3 100644 --- a/mailpoet/lib/Automation/Engine/Validation/WorkflowStepsValidator.php +++ b/mailpoet/lib/Automation/Engine/Validation/WorkflowStepsValidator.php @@ -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,9 +57,15 @@ class WorkflowStepsValidator { } } - private function getCurrentWorkflow(int $id): ?Workflow { - if ($this->cachedExistingWorkflow === false) { - $this->cachedExistingWorkflow = $this->workflowStorage->getWorkflow($id); + 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; }