Fetch workflow, workflow run, and step in workflow step runner

[MAILPOET-4136]
This commit is contained in:
Jan Jakes
2022-03-07 12:18:47 +01:00
committed by Veljko V
parent 66eaf555b5
commit a7cd2b74b4
4 changed files with 66 additions and 2 deletions

View File

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

View File

@@ -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)

View File

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

View File

@@ -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(