Run validation on workflow create and update

[MAILPOET-4629]
This commit is contained in:
Jan Jakes
2022-09-14 16:30:59 +02:00
committed by David Remer
parent e220b70c78
commit d7b65bd980
2 changed files with 21 additions and 10 deletions

View File

@ -6,6 +6,7 @@ use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Automation\Engine\Storage\WorkflowTemplateStorage;
use MailPoet\Automation\Engine\Validation\WorkflowValidator;
use MailPoet\UnexpectedValueException;
class CreateWorkflowFromTemplateController {
@ -15,26 +16,32 @@ class CreateWorkflowFromTemplateController {
/** @var WorkflowTemplateStorage */
private $templateStorage;
/** @var WorkflowValidator */
private $workflowValidator;
public function __construct(
WorkflowStorage $storage,
WorkflowTemplateStorage $templateStorage
WorkflowTemplateStorage $templateStorage,
WorkflowValidator $workflowValidator
) {
$this->storage = $storage;
$this->templateStorage = $templateStorage;
$this->workflowValidator = $workflowValidator;
}
public function createWorkflow(string $slug): Workflow {
$template = $this->templateStorage->getTemplateBySlug($slug);
if (!$template) {
throw UnexpectedValueException::create()->withMessage('Template not found.');
}
$workflowId = $this->storage->createWorkflow($template->getWorkflow());
$workflow = $this->storage->getWorkflow($workflowId);
if (!$workflow) {
$workflow = $template->getWorkflow();
$this->workflowValidator->validate($workflow);
$workflowId = $this->storage->createWorkflow($workflow);
$savedWorkflow = $this->storage->getWorkflow($workflowId);
if (!$savedWorkflow) {
throw new InvalidStateException('Workflow not found.');
}
return $workflow;
return $savedWorkflow;
}
}