refactor: add AbstractProvider

This commit is contained in:
Maël Gangloff
2024-08-23 02:35:09 +02:00
parent adb67d2e24
commit b59c1c61dc
10 changed files with 95 additions and 67 deletions

View File

@@ -1,18 +0,0 @@
<?php
namespace App\Config\Connector;
use App\Entity\Domain;
use App\Entity\Tld;
use Symfony\Contracts\HttpClient\HttpClientInterface;
interface ConnectorInterface
{
public function __construct(array $authData, HttpClientInterface $client);
public function orderDomain(Domain $domain, bool $dryRun): void;
public static function verifyAuthData(array $authData, HttpClientInterface $client): array;
public function isSupported(Tld ...$tld): bool;
}

View File

@@ -2,8 +2,8 @@
namespace App\Config;
use App\Config\Connector\GandiConnector;
use App\Config\Connector\OvhConnector;
use App\Config\Provider\GandiProvider;
use App\Config\Provider\OvhProvider;
enum ConnectorProvider: string
{
@@ -13,8 +13,8 @@ enum ConnectorProvider: string
public function getConnectorProvider(): string
{
return match ($this) {
ConnectorProvider::OVH => OvhConnector::class,
ConnectorProvider::GANDI => GandiConnector::class
ConnectorProvider::OVH => OvhProvider::class,
ConnectorProvider::GANDI => GandiProvider::class
};
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Config\Provider;
use App\Entity\Domain;
use App\Entity\Tld;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
abstract class AbstractProvider
{
public function __construct(
protected array $authData,
protected HttpClientInterface $client,
protected CacheItemPoolInterface $cacheItemPool,
protected KernelInterface $kernel
) {
}
abstract public static function verifyAuthData(array $authData, HttpClientInterface $client): array;
abstract public function orderDomain(Domain $domain, bool $dryRun): void;
public function isSupported(Tld ...$tldList): bool
{
$item = $this->getCachedTldList();
if (!$item->isHit()) {
$supportedTldList = $this->getSupportedTldList();
$item
->set($supportedTldList)
->expiresAfter(new \DateInterval('P1M'));
$this->cacheItemPool->saveDeferred($item);
} else {
$supportedTldList = $item->get();
}
/** @var string $tldString */
foreach (array_unique(array_map(fn (Tld $tld) => $tld->getTld(), $tldList)) as $tldString) {
if (!in_array($tldString, $supportedTldList)) {
return false;
}
}
return true;
}
abstract protected function getCachedTldList(): CacheItemInterface;
abstract protected function getSupportedTldList(): array;
}

View File

@@ -1,10 +1,10 @@
<?php
namespace App\Config\Connector;
namespace App\Config\Provider;
use App\Entity\Domain;
use App\Entity\Tld;
use http\Exception\InvalidArgumentException;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -16,14 +16,10 @@ use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
readonly class GandiConnector implements ConnectorInterface
class GandiProvider extends AbstractProvider
{
private const BASE_URL = 'https://api.gandi.net';
public function __construct(private array $authData, private HttpClientInterface $client)
{
}
/**
* Order a domain name with the Gandi API.
*
@@ -142,7 +138,7 @@ readonly class GandiConnector implements ConnectorInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function isSupported(Tld ...$tldList): bool
protected function getSupportedTldList(): array
{
$authData = self::verifyAuthData($this->authData, $this->client);
@@ -152,15 +148,14 @@ readonly class GandiConnector implements ConnectorInterface
->setBaseUri(self::BASE_URL)
->toArray())->toArray();
$supportedTldList = array_map(fn ($tld) => $tld['name'], $response);
return array_map(fn ($tld) => $tld['name'], $response);
}
/** @var string $tldString */
foreach (array_unique(array_map(fn (Tld $tld) => $tld->getTld(), $tldList)) as $tldString) {
if (!in_array($tldString, $supportedTldList)) {
return false;
}
}
return true;
/**
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
}

View File

@@ -1,17 +1,18 @@
<?php
namespace App\Config\Connector;
namespace App\Config\Provider;
use App\Entity\Domain;
use App\Entity\Tld;
use GuzzleHttp\Exception\ClientException;
use Ovh\Api;
use Ovh\Exceptions\InvalidParameterException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
readonly class OvhConnector implements ConnectorInterface
class OvhProvider extends AbstractProvider
{
public const REQUIRED_ROUTES = [
[
@@ -40,10 +41,6 @@ readonly class OvhConnector implements ConnectorInterface
],
];
public function __construct(private array $authData, private HttpClientInterface $client)
{
}
/**
* Order a domain name with the OVH API.
*
@@ -219,7 +216,7 @@ readonly class OvhConnector implements ConnectorInterface
* @throws \JsonException
* @throws \Exception
*/
public function isSupported(Tld ...$tldList): bool
protected function getSupportedTldList(): array
{
$authData = self::verifyAuthData($this->authData, $this->client);
@@ -230,17 +227,16 @@ readonly class OvhConnector implements ConnectorInterface
$authData['consumerKey']
);
$supportedTldList = $conn->get('/domain/extensions', [
return $conn->get('/domain/extensions', [
'ovhSubsidiary' => $authData['ovhSubsidiary'],
]);
}
/** @var string $tldString */
foreach (array_unique(array_map(fn (Tld $tld) => $tld->getTld(), $tldList)) as $tldString) {
if (!in_array($tldString, $supportedTldList)) {
return false;
}
}
return true;
/**
* @throws InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
}