From 41cb2c40348a6bcc1cf3f5f558bd259a0ba87b4a Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Mon, 7 Mar 2022 11:34:04 +0100 Subject: [PATCH] Add storage service for workflows & create workflow method [MAILPOET-4136] --- mailpoet/lib/Automation/Engine/Exceptions.php | 7 +++++ .../Engine/Storage/WorkflowStorage.php | 29 +++++++++++++++++++ mailpoet/lib/DI/ContainerConfigurator.php | 1 + 3 files changed, 37 insertions(+) create mode 100644 mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index cd44f32ec7..869559e59c 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -7,6 +7,7 @@ use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; class Exceptions { private const MIGRATION_FAILED = 'mailpoet_automation_migration_failed'; + 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'; @@ -22,6 +23,12 @@ class Exceptions { ->withMessage(__(sprintf('Migration failed: %s', $error), 'mailpoet')); } + public static function databaseError(string $error): InvalidStateException { + return InvalidStateException::create() + ->withErrorCode(self::DATABASE_ERROR) + ->withMessage(__(sprintf('Database error: %s', $error), 'mailpoet')); + } + public static function apiMethodNotAllowed(): UnexpectedValueException { return UnexpectedValueException::create() ->withStatusCode(405) diff --git a/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php b/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php new file mode 100644 index 0000000000..8507d9570b --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php @@ -0,0 +1,29 @@ +table = $wpdb->prefix . 'mailpoet_automation_workflows'; + $this->wpdb = $wpdb; + } + + public function createWorkflow(Workflow $workflow): int { + $result = $this->wpdb->insert($this->table, $workflow->toArray()); + if (!$result) { + throw Exceptions::databaseError($this->wpdb->last_error); + } + return $this->wpdb->insert_id; + } +} diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index 072faef120..1958a72958 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -109,6 +109,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Automation\Engine\API\Endpoints\WorkflowsEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Engine::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Migrations\Migrator::class)->setPublic(true); + $container->autowire(\MailPoet\Automation\Engine\Storage\WorkflowStorage::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\WordPress::class)->setPublic(true); // Config $container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);