Add storage service for workflows & create workflow method
[MAILPOET-4136]
This commit is contained in:
@ -7,6 +7,7 @@ use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
|||||||
|
|
||||||
class Exceptions {
|
class Exceptions {
|
||||||
private const MIGRATION_FAILED = 'mailpoet_automation_migration_failed';
|
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_METHOD_NOT_ALLOWED = 'mailpoet_automation_api_method_not_allowed';
|
||||||
private const API_NO_JSON_BODY = 'mailpoet_automation_api_no_json_body';
|
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'));
|
->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 {
|
public static function apiMethodNotAllowed(): UnexpectedValueException {
|
||||||
return UnexpectedValueException::create()
|
return UnexpectedValueException::create()
|
||||||
->withStatusCode(405)
|
->withStatusCode(405)
|
||||||
|
29
mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php
Normal file
29
mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Engine\Storage;
|
||||||
|
|
||||||
|
use MailPoet\Automation\Engine\Exceptions;
|
||||||
|
use MailPoet\Automation\Engine\Workflows\Workflow;
|
||||||
|
use wpdb;
|
||||||
|
|
||||||
|
class WorkflowStorage {
|
||||||
|
/** @var string */
|
||||||
|
private $table;
|
||||||
|
|
||||||
|
/** @var wpdb */
|
||||||
|
private $wpdb;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
global $wpdb;
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
@ -109,6 +109,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Automation\Engine\API\Endpoints\WorkflowsEndpoint::class)->setPublic(true);
|
$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\Engine::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Migrations\Migrator::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);
|
$container->autowire(\MailPoet\Automation\Engine\WordPress::class)->setPublic(true);
|
||||||
// Config
|
// Config
|
||||||
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
||||||
|
Reference in New Issue
Block a user