2024-08-23 02:35:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-09-18 13:37:07 +02:00
|
|
|
namespace App\Service\Connector;
|
2024-08-23 02:35:09 +02:00
|
|
|
|
|
|
|
|
use App\Entity\Domain;
|
|
|
|
|
use Psr\Cache\CacheItemInterface;
|
|
|
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
|
|
|
|
2024-09-30 13:48:15 +02:00
|
|
|
/**
|
|
|
|
|
* The typical flow of a provider will go as follows:
|
|
|
|
|
*
|
|
|
|
|
* MyProvider $provider; // gotten from DI
|
|
|
|
|
* $provider->authenticate($authData);
|
|
|
|
|
* $provider->orderDomain($domain, $dryRun);
|
|
|
|
|
*/
|
2024-08-23 02:35:09 +02:00
|
|
|
abstract class AbstractProvider
|
|
|
|
|
{
|
2024-09-18 13:37:07 +02:00
|
|
|
protected array $authData;
|
|
|
|
|
|
2024-08-23 02:35:09 +02:00
|
|
|
public function __construct(
|
2024-09-18 13:37:07 +02:00
|
|
|
protected CacheItemPoolInterface $cacheItemPool
|
2024-08-23 02:35:09 +02:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 13:48:15 +02:00
|
|
|
/**
|
|
|
|
|
* @param array $authData raw authentication data as supplied by the user
|
|
|
|
|
*
|
|
|
|
|
* @return array a cleaned up version of the authentication data
|
|
|
|
|
*/
|
|
|
|
|
abstract public function verifyAuthData(array $authData): array;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws \Exception when the registrar denies the authentication
|
|
|
|
|
*/
|
|
|
|
|
abstract public function assertAuthentication(): void; // TODO use dedicated exception type
|
2024-08-23 02:35:09 +02:00
|
|
|
|
|
|
|
|
abstract public function orderDomain(Domain $domain, bool $dryRun): void;
|
|
|
|
|
|
2024-08-24 18:22:15 +02:00
|
|
|
public function isSupported(Domain ...$domainList): bool
|
2024-08-23 02:35:09 +02:00
|
|
|
{
|
|
|
|
|
$item = $this->getCachedTldList();
|
|
|
|
|
if (!$item->isHit()) {
|
|
|
|
|
$supportedTldList = $this->getSupportedTldList();
|
|
|
|
|
$item
|
|
|
|
|
->set($supportedTldList)
|
2024-08-24 18:22:15 +02:00
|
|
|
->expiresAfter(new \DateInterval('PT1H'));
|
2024-08-23 02:35:09 +02:00
|
|
|
$this->cacheItemPool->saveDeferred($item);
|
|
|
|
|
} else {
|
|
|
|
|
$supportedTldList = $item->get();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 18:22:15 +02:00
|
|
|
$extensionList = [];
|
|
|
|
|
foreach ($domainList as $domain) {
|
|
|
|
|
// We want to check the support of TLDs and SLDs here.
|
|
|
|
|
// For example, it is not enough for the Connector to support .fr for it to support the domain name example.asso.fr.
|
|
|
|
|
// It must support .asso.fr.
|
|
|
|
|
$extension = explode('.', $domain->getLdhName(), 2)[1];
|
|
|
|
|
if (!in_array($extension, $extensionList)) {
|
|
|
|
|
$extensionList[] = $extension;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($extensionList as $extension) {
|
|
|
|
|
if (!in_array($extension, $supportedTldList)) {
|
2024-08-23 02:35:09 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 13:48:15 +02:00
|
|
|
/**
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2024-09-18 13:37:07 +02:00
|
|
|
public function authenticate(array $authData): void
|
|
|
|
|
{
|
2024-09-30 13:48:15 +02:00
|
|
|
$this->authData = $this->verifyAuthData($authData);
|
|
|
|
|
$this->assertAuthentication();
|
2024-09-18 13:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 02:35:09 +02:00
|
|
|
abstract protected function getCachedTldList(): CacheItemInterface;
|
|
|
|
|
|
|
|
|
|
abstract protected function getSupportedTldList(): array;
|
|
|
|
|
}
|