diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index d36192716c..c05bd91e92 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -27,6 +27,7 @@ class Exceptions { private const WORKFLOW_STRUCTURE_MODIFICATION_NOT_SUPPORTED = 'mailpoet_automation_workflow_structure_modification_not_supported'; private const WORKFLOW_STRUCTURE_NOT_VALID = 'mailpoet_automation_workflow_structure_not_valid'; private const WORKFLOW_STEP_MODIFIED_WHEN_UNKNOWN = 'mailpoet_automation_workflow_step_modified_when_unknown'; + private const WORKFLOW_NOT_VALID = 'mailpoet_automation_workflow_not_valid'; public function __construct() { throw new InvalidStateException( @@ -180,4 +181,12 @@ class Exceptions { ) ); } + + public static function workflowNotValid(string $detail, array $errors): UnexpectedValueException { + return UnexpectedValueException::create() + ->withErrorCode(self::WORKFLOW_NOT_VALID) + // translators: %s is a detailed information + ->withMessage(sprintf(__("Workflow validation failed: %s", 'mailpoet'), $detail)) + ->withErrors($errors); + } } diff --git a/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRule.php b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRule.php index ae45745a39..1edf26455f 100644 --- a/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRule.php +++ b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRule.php @@ -3,6 +3,7 @@ namespace MailPoet\Automation\Engine\Validation\WorkflowRules; use MailPoet\Automation\Engine\Data\Workflow; +use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Registry; use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode; use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor; @@ -15,6 +16,9 @@ class ValidStepArgsRule implements WorkflowNodeVisitor { /** @var Validator */ private $validator; + /** @var array */ + private $errors = []; + public function __construct( Registry $registry, Validator $validator @@ -24,6 +28,7 @@ class ValidStepArgsRule implements WorkflowNodeVisitor { } public function initialize(Workflow $workflow): void { + $this->errors = []; } public function visitNode(Workflow $workflow, WorkflowNode $node): void { @@ -35,10 +40,24 @@ class ValidStepArgsRule implements WorkflowNodeVisitor { // validate args schema only for active workflows if ($workflow->getStatus() !== Workflow::STATUS_ACTIVE) { + return; + } + + try { $this->validator->validate($registryStep->getArgsSchema(), $step->getArgs()); + } catch (Throwable $e) { + $this->errors[$step->getId()] = [ + 'step_id' => $step->getId(), + 'message' => $e instanceof ValidationException + ? $e->getWpError()->get_error_message() + : __('Unknown error.', 'mailpoet'), + ]; } } public function complete(Workflow $workflow): void { + if ($this->errors) { + throw Exceptions::workflowNotValid(__('Some steps have invalid arguments', 'mailpoet'), $this->errors); + } } }