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

@@ -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;
}