Add endpoint-class factory based on PSR-11 container interface

[MAILPOET-4135]
This commit is contained in:
Jan Jakes
2022-02-10 10:53:06 +01:00
committed by Veljko V
parent 18d722cd51
commit ebfddb73bd
2 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\API;
use MailPoet\InvalidStateException;
use MailPoetVendor\Psr\Container\ContainerInterface;
class EndpointFactory {
/** @var ContainerInterface */
private $container;
public function __construct(
ContainerInterface $container
) {
$this->container = $container;
}
public function createEndpoint(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;
}
}