From a7cd2b74b47f6ae1e160811f0fbb51c280484588 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Mon, 7 Mar 2022 12:18:47 +0100 Subject: [PATCH] Fetch workflow, workflow run, and step in workflow step runner [MAILPOET-4136] --- .../Automation/Engine/Control/StepRunner.php | 35 +++++++++++++++++-- mailpoet/lib/Automation/Engine/Exceptions.php | 21 +++++++++++ .../Engine/Storage/WorkflowRunStorage.php | 6 ++++ .../Engine/Storage/WorkflowStorage.php | 6 ++++ 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/mailpoet/lib/Automation/Engine/Control/StepRunner.php b/mailpoet/lib/Automation/Engine/Control/StepRunner.php index 2ed2905532..145b4a8708 100644 --- a/mailpoet/lib/Automation/Engine/Control/StepRunner.php +++ b/mailpoet/lib/Automation/Engine/Control/StepRunner.php @@ -3,8 +3,11 @@ namespace MailPoet\Automation\Engine\Control; use Exception; +use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Exceptions\InvalidStateException; use MailPoet\Automation\Engine\Hooks; +use MailPoet\Automation\Engine\Storage\WorkflowRunStorage; +use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Engine\WordPress; use Throwable; @@ -12,10 +15,20 @@ class StepRunner { /** @var WordPress */ private $wordPress; + /** @var WorkflowRunStorage */ + private $workflowRunStorage; + + /** @var WorkflowStorage */ + private $workflowStorage; + public function __construct( - WordPress $wordPress + WordPress $wordPress, + WorkflowRunStorage $workflowRunStorage, + WorkflowStorage $workflowStorage ) { $this->wordPress = $wordPress; + $this->workflowRunStorage = $workflowRunStorage; + $this->workflowStorage = $workflowStorage; } public function initialize(): void { @@ -43,6 +56,24 @@ class StepRunner { throw new InvalidStateException(); } - // TODO: process step + $workflowRunId = $args['workflow_run_id']; + $stepId = $args['step_id']; + + $workflowRun = $this->workflowRunStorage->getWorkflowRun($workflowRunId); + if (!$workflowRun) { + throw Exceptions::workflowRunNotFound($workflowRunId); + } + + $workflow = $this->workflowStorage->getWorkflow($workflowRun->getWorkflowId()); + if (!$workflow) { + throw Exceptions::workflowNotFound($workflowRun->getWorkflowId()); + } + + $step = $workflow->getStep($stepId); + if (!$step) { + throw Exceptions::workflowStepNotFound($stepId); + } + + // TODO: process step based on its type } } diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index d5d7148b55..33b48f3f48 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -11,6 +11,9 @@ class Exceptions { private const DATABASE_ERROR = 'mailpoet_automation_database_error'; private const API_METHOD_NOT_ALLOWED = 'mailpoet_automation_api_method_not_allowed'; private const API_NO_JSON_BODY = 'mailpoet_automation_api_no_json_body'; + private const WORKFLOW_NOT_FOUND = 'mailpoet_automation_workflow_not_found'; + private const WORKFLOW_RUN_NOT_FOUND = 'mailpoet_automation_workflow_run_not_found'; + private const WORKFLOW_STEP_NOT_FOUND = 'mailpoet_automation_workflow_step_not_found'; private const WORKFLOW_TRIGGER_NOT_FOUND = 'mailpoet_automation_workflow_trigger_not_found'; public function __construct() { @@ -44,6 +47,24 @@ class Exceptions { ->withMessage(__('No JSON body passed.', 'mailpoet')); } + public static function workflowNotFound(int $id): NotFoundException { + return NotFoundException::create() + ->withErrorCode(self::WORKFLOW_NOT_FOUND) + ->withMessage(__(sprintf("Workflow with ID '%s' not found.", $id), 'mailpoet')); + } + + public static function workflowRunNotFound(int $id): NotFoundException { + return NotFoundException::create() + ->withErrorCode(self::WORKFLOW_RUN_NOT_FOUND) + ->withMessage(__(sprintf("Workflow run with ID '%s' not found.", $id), 'mailpoet')); + } + + public static function workflowStepNotFound(string $id): NotFoundException { + return NotFoundException::create() + ->withErrorCode(self::WORKFLOW_STEP_NOT_FOUND) + ->withMessage(__(sprintf("Workflow step with ID '%s' not found.", $id), 'mailpoet')); + } + public static function workflowTriggerNotFound(int $workflowId, string $key): NotFoundException { return NotFoundException::create() ->withErrorCode(self::WORKFLOW_TRIGGER_NOT_FOUND) diff --git a/mailpoet/lib/Automation/Engine/Storage/WorkflowRunStorage.php b/mailpoet/lib/Automation/Engine/Storage/WorkflowRunStorage.php index 5e5c5df1d9..60ec7f9724 100644 --- a/mailpoet/lib/Automation/Engine/Storage/WorkflowRunStorage.php +++ b/mailpoet/lib/Automation/Engine/Storage/WorkflowRunStorage.php @@ -26,4 +26,10 @@ class WorkflowRunStorage { } return $this->wpdb->insert_id; } + + public function getWorkflowRun(int $id): ?WorkflowRun { + $query = strval($this->wpdb->prepare("SELECT * FROM $this->table WHERE id = %d", $id)); + $data = $this->wpdb->get_row($query, ARRAY_A); + return $data ? WorkflowRun::fromArray((array)$data) : null; + } } diff --git a/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php b/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php index 0605801e62..993ec8455e 100644 --- a/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php +++ b/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php @@ -28,6 +28,12 @@ class WorkflowStorage { return $this->wpdb->insert_id; } + public function getWorkflow(int $id): ?Workflow { + $query = strval($this->wpdb->prepare("SELECT * FROM $this->table WHERE id = %d", $id)); + $data = $this->wpdb->get_row($query, ARRAY_A); + return $data ? Workflow::fromArray((array)$data) : null; + } + /** @return string[] */ public function getActiveTriggerKeys(): array { $query = strval(