Run validation on workflow create and update
[MAILPOET-4629]
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user