Make API endpoint a simple single-action class
[MAILPOET-4207]
This commit is contained in:
@@ -2,16 +2,14 @@
|
||||
|
||||
namespace MailPoet\Automation\Engine\API;
|
||||
|
||||
use MailPoet\Automation\Engine\API\Endpoints\SystemDatabaseEndpoint;
|
||||
use MailPoet\Automation\Engine\API\Endpoints\WorkflowsEndpoint;
|
||||
use MailPoet\Automation\Engine\Exceptions\Exception;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use ReflectionClass;
|
||||
use Throwable;
|
||||
use WP_REST_Request;
|
||||
|
||||
class API {
|
||||
private const PREFIX = 'mailpoet/v1/automation';
|
||||
private const METHODS = ['get', 'post', 'put', 'delete'];
|
||||
private const WP_REST_API_INIT_ACTION = 'rest_api_init';
|
||||
|
||||
/** @var EndpointFactory */
|
||||
@@ -20,12 +18,6 @@ class API {
|
||||
/** @var WordPress */
|
||||
private $wordPress;
|
||||
|
||||
/** @var array<string, class-string<Endpoint>> */
|
||||
private $routes = [
|
||||
'system/database' => SystemDatabaseEndpoint::class,
|
||||
'workflows' => WorkflowsEndpoint::class,
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
EndpointFactory $endpointFactory,
|
||||
WordPress $wordPress
|
||||
@@ -36,30 +28,45 @@ class API {
|
||||
|
||||
public function initialize(): void {
|
||||
$this->wordPress->addAction(self::WP_REST_API_INIT_ACTION, function () {
|
||||
foreach ($this->routes as $route => $endpoint) {
|
||||
$reflection = new ReflectionClass($endpoint);
|
||||
foreach (self::METHODS as $method) {
|
||||
if ($reflection->hasMethod($method) && $reflection->getMethod($method)->class === $endpoint) {
|
||||
$this->registerRoute($route, $endpoint, $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->wordPress->doAction(Hooks::API_INITIALIZE, [$this]);
|
||||
});
|
||||
}
|
||||
|
||||
private function registerRoute(string $route, string $endpoint, string $method): void {
|
||||
public function registerGetRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'GET');
|
||||
}
|
||||
|
||||
public function registerPostRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'POST');
|
||||
}
|
||||
|
||||
public function registerPutRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PUT');
|
||||
}
|
||||
|
||||
public function registerPatchRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'PATCH');
|
||||
}
|
||||
|
||||
public function registerDeleteRoute(string $route, string $endpoint): void {
|
||||
$this->registerRoute($route, $endpoint, 'DELETE');
|
||||
}
|
||||
|
||||
private function registerRoute(string $route, string $endpointClass, string $method): void {
|
||||
$this->wordPress->registerRestRoute(self::PREFIX, $route, [
|
||||
'methods' => strtoupper($method),
|
||||
'callback' => function ($wpRequest) use ($endpoint, $method) {
|
||||
'methods' => $method,
|
||||
'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass) {
|
||||
try {
|
||||
$endpoint = $this->endpointFactory->createEndpoint($endpointClass);
|
||||
$request = new Request($wpRequest);
|
||||
return $this->endpointFactory->createEndpoint($endpoint)->$method($request);
|
||||
return $endpoint->handle($request);
|
||||
} catch (Throwable $e) {
|
||||
return $this->convertToErrorResponse($e);
|
||||
}
|
||||
},
|
||||
'permission_callback' => function () {
|
||||
return $this->wordPress->currentUserCan('administrator');
|
||||
'permission_callback' => function () use ($endpointClass) {
|
||||
$endpoint = $this->endpointFactory->createEndpoint($endpointClass);
|
||||
return $endpoint->checkPermissions();
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user