Extract automation API to MailPoet REST API
[MAILPOET-4523]
This commit is contained in:
committed by
John Oleksowicz
parent
1b5d6bd974
commit
ba35ddf6e6
@ -2,101 +2,50 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\Automation\Engine\Exceptions\Exception;
|
||||
use MailPoet\API\REST\API as MailPoetApi;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use MailPoet\Validator\Schema;
|
||||
use Throwable;
|
||||
use WP_REST_Request;
|
||||
|
||||
class API {
|
||||
private const PREFIX = 'mailpoet/v1/automation';
|
||||
private const WP_REST_API_INIT_ACTION = 'rest_api_init';
|
||||
private const PREFIX = 'automation/';
|
||||
|
||||
/** @var EndpointContainer */
|
||||
private $endpointContainer;
|
||||
/** @var MailPoetApi */
|
||||
private $api;
|
||||
|
||||
/** @var WordPress */
|
||||
private $wordPress;
|
||||
|
||||
public function __construct(
|
||||
EndpointContainer $endpointContainer,
|
||||
MailPoetApi $api,
|
||||
WordPress $wordPress
|
||||
) {
|
||||
$this->endpointContainer = $endpointContainer;
|
||||
$this->api = $api;
|
||||
$this->wordPress = $wordPress;
|
||||
}
|
||||
|
||||
public function initialize(): void {
|
||||
$this->wordPress->addAction(self::WP_REST_API_INIT_ACTION, function () {
|
||||
$this->wordPress->addAction(MailPoetApi::REST_API_INIT_ACTION, function () {
|
||||
$this->wordPress->doAction(Hooks::API_INITIALIZE, [$this]);
|
||||
});
|
||||
}
|
||||
|
||||
public function registerGetRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'GET');
|
||||
$this->api->registerGetRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPostRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'POST');
|
||||
$this->api->registerPostRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPutRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PUT');
|
||||
$this->api->registerPutRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerPatchRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PATCH');
|
||||
$this->api->registerPatchRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
|
||||
public function registerDeleteRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'DELETE');
|
||||
}
|
||||
|
||||
private function registerRoute(string $route, string $endpointClass, string $method): void {
|
||||
$schema = array_map(function (Schema $field) {
|
||||
return $field->toArray();
|
||||
}, $endpointClass::getRequestSchema());
|
||||
|
||||
$this->wordPress->registerRestRoute(self::PREFIX, $route, [
|
||||
'methods' => $method,
|
||||
'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass, $schema) {
|
||||
try {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
$wpRequest = $this->sanitizeUnknownParams($wpRequest, $schema);
|
||||
$request = new Request($wpRequest);
|
||||
return $endpoint->handle($request);
|
||||
} catch (Throwable $e) {
|
||||
return $this->convertToErrorResponse($e);
|
||||
}
|
||||
},
|
||||
'permission_callback' => function () use ($endpointClass) {
|
||||
$endpoint = $this->endpointContainer->get($endpointClass);
|
||||
return $endpoint->checkPermissions();
|
||||
},
|
||||
'args' => $schema,
|
||||
]);
|
||||
}
|
||||
|
||||
private function convertToErrorResponse(Throwable $e): ErrorResponse {
|
||||
$response = $e instanceof Exception
|
||||
? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode())
|
||||
: new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error');
|
||||
|
||||
if ($response->get_status() >= 500) {
|
||||
error_log((string)$e); // phpcs:ignore Squiz.PHP.DiscouragedFunctions
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function sanitizeUnknownParams(WP_REST_Request $wpRequest, array $args): WP_REST_Request {
|
||||
// Remove all params that are not declared in the schema, so we use just the validated ones.
|
||||
// Note that this doesn't work recursively for object properties as it is harder to solve
|
||||
// with features like oneOf, anyOf, additional properties, or pattern properties.
|
||||
$extraParams = array_diff(array_keys($wpRequest->get_params()), array_keys($args));
|
||||
foreach ($extraParams as $extraParam) {
|
||||
unset($wpRequest[(string)$extraParam]);
|
||||
}
|
||||
return $wpRequest;
|
||||
$this->api->registerDeleteRoute(self::PREFIX . $route, $endpoint);
|
||||
}
|
||||
}
|
||||
|
@ -2,20 +2,11 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\API\REST\Endpoint as MailPoetEndpoint;
|
||||
use MailPoet\Automation\Engine\Engine;
|
||||
use MailPoet\Validator\Schema;
|
||||
|
||||
use function current_user_can;
|
||||
|
||||
abstract class Endpoint {
|
||||
abstract public function handle(Request $request): Response;
|
||||
|
||||
abstract class Endpoint extends MailPoetEndpoint {
|
||||
public function checkPermissions(): bool {
|
||||
return current_user_can(Engine::CAPABILITY_MANAGE_AUTOMATIONS);
|
||||
}
|
||||
|
||||
/** @return array<string, Schema> */
|
||||
public static function getRequestSchema(): array {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\InvalidStateException;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
||||
|
||||
class EndpointContainer {
|
||||
/** @var ContainerInterface */
|
||||
private $container;
|
||||
|
||||
public function __construct(
|
||||
ContainerInterface $container
|
||||
) {
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function get(string $class): Endpoint {
|
||||
$endpoint = $this->container->get($class);
|
||||
if (!$endpoint instanceof Endpoint) {
|
||||
throw new InvalidStateException(sprintf("Class '%s' doesn't implement '%s'", $class, Endpoint::class));
|
||||
}
|
||||
return $endpoint;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
class ErrorResponse extends Response {
|
||||
public function __construct(
|
||||
int $status,
|
||||
string $message,
|
||||
string $code
|
||||
) {
|
||||
parent::__construct(null, $status);
|
||||
$this->set_data([
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => ['status' => $status],
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use WP_REST_Request;
|
||||
|
||||
class Request {
|
||||
/** @var WP_REST_Request */
|
||||
private $wpRequest;
|
||||
|
||||
public function __construct(
|
||||
WP_REST_Request $wpRequest
|
||||
) {
|
||||
$this->wpRequest = $wpRequest;
|
||||
}
|
||||
|
||||
public function getHeader(string $key): ?string {
|
||||
return $this->wpRequest->get_header($key);
|
||||
}
|
||||
|
||||
public function getParams(): array {
|
||||
return $this->wpRequest->get_params();
|
||||
}
|
||||
|
||||
/** @return mixed */
|
||||
public function getParam(string $name) {
|
||||
return $this->wpRequest->get_param($name);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use WP_REST_Response;
|
||||
|
||||
class Response extends WP_REST_Response {
|
||||
public function __construct(
|
||||
array $data = null,
|
||||
int $status = 200
|
||||
) {
|
||||
parent::__construct(['data' => $data], $status);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user