Rename endpoint "factory" to a more exact "container"

[MAILPOET-4207]
This commit is contained in:
Jan Jakes
2022-04-12 14:42:22 +02:00
committed by Veljko V
parent 36a3f34db8
commit 679f2200bd
4 changed files with 12 additions and 12 deletions

View File

@ -13,17 +13,17 @@ class API {
private const PREFIX = 'mailpoet/v1/automation'; private const PREFIX = 'mailpoet/v1/automation';
private const WP_REST_API_INIT_ACTION = 'rest_api_init'; private const WP_REST_API_INIT_ACTION = 'rest_api_init';
/** @var EndpointFactory */ /** @var EndpointContainer */
private $endpointFactory; private $endpointContainer;
/** @var WordPress */ /** @var WordPress */
private $wordPress; private $wordPress;
public function __construct( public function __construct(
EndpointFactory $endpointFactory, EndpointContainer $endpointContainer,
WordPress $wordPress WordPress $wordPress
) { ) {
$this->endpointFactory = $endpointFactory; $this->endpointContainer = $endpointContainer;
$this->wordPress = $wordPress; $this->wordPress = $wordPress;
} }
@ -62,7 +62,7 @@ class API {
'methods' => $method, 'methods' => $method,
'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass) { 'callback' => function (WP_REST_Request $wpRequest) use ($endpointClass) {
try { try {
$endpoint = $this->endpointFactory->createEndpoint($endpointClass); $endpoint = $this->endpointContainer->get($endpointClass);
$request = new Request($wpRequest); $request = new Request($wpRequest);
return $endpoint->handle($request); return $endpoint->handle($request);
} catch (Throwable $e) { } catch (Throwable $e) {
@ -70,7 +70,7 @@ class API {
} }
}, },
'permission_callback' => function () use ($endpointClass) { 'permission_callback' => function () use ($endpointClass) {
$endpoint = $this->endpointFactory->createEndpoint($endpointClass); $endpoint = $this->endpointContainer->get($endpointClass);
return $endpoint->checkPermissions(); return $endpoint->checkPermissions();
}, },
'args' => $schema, 'args' => $schema,

View File

@ -5,7 +5,7 @@ namespace MailPoet\Automation\Engine\API;
use MailPoet\InvalidStateException; use MailPoet\InvalidStateException;
use MailPoetVendor\Psr\Container\ContainerInterface; use MailPoetVendor\Psr\Container\ContainerInterface;
class EndpointFactory { class EndpointContainer {
/** @var ContainerInterface */ /** @var ContainerInterface */
private $container; private $container;
@ -15,7 +15,7 @@ class EndpointFactory {
$this->container = $container; $this->container = $container;
} }
public function createEndpoint(string $class): Endpoint { public function get(string $class): Endpoint {
$endpoint = $this->container->get($class); $endpoint = $this->container->get($class);
if (!$endpoint instanceof Endpoint) { if (!$endpoint instanceof Endpoint) {
throw new InvalidStateException(sprintf("Class '%s' doesn't implement '%s'", $class, Endpoint::class)); throw new InvalidStateException(sprintf("Class '%s' doesn't implement '%s'", $class, Endpoint::class));

View File

@ -105,7 +105,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct::class)->setPublic(true); $container->autowire(\MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct::class)->setPublic(true);
// Automation // Automation
$container->autowire(\MailPoet\Automation\Engine\API\API::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\API\API::class)->setPublic(true);
$container->autowire(\MailPoet\Automation\Engine\API\EndpointFactory::class) $container->autowire(\MailPoet\Automation\Engine\API\EndpointContainer::class)
->setPublic(true) ->setPublic(true)
->setArgument('$container', new Reference(ContainerWrapper::class)); ->setArgument('$container', new Reference(ContainerWrapper::class));
$container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowController::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Builder\CreateWorkflowController::class)->setPublic(true);

View File

@ -6,7 +6,7 @@ require_once __DIR__ . '/../../Test.php';
require_once __DIR__ . '/Endpoint.php'; require_once __DIR__ . '/Endpoint.php';
use MailPoet\Automation\Engine\API\API; use MailPoet\Automation\Engine\API\API;
use MailPoet\Automation\Engine\API\EndpointFactory; use MailPoet\Automation\Engine\API\EndpointContainer;
use MailPoet\Automation\Engine\API\Request; use MailPoet\Automation\Engine\API\Request;
use MailPoet\Automation\Engine\WordPress; use MailPoet\Automation\Engine\WordPress;
use MailPoet\REST\Automation\API\Endpoints\Endpoint; use MailPoet\REST\Automation\API\Endpoints\Endpoint;
@ -114,8 +114,8 @@ class EndpointTest extends Test {
rest_get_server(); rest_get_server();
return new API( return new API(
$this->make(EndpointFactory::class, [ $this->make(EndpointContainer::class, [
'createEndpoint' => function () use ($requestCallback) { 'get' => function () use ($requestCallback) {
return new Endpoint($requestCallback); return new Endpoint($requestCallback);
} }
]), ]),