feat: improve Connector abstract

This commit is contained in:
Maël Gangloff
2024-08-07 01:10:56 +02:00
parent e04e880d33
commit f3650f3b9f
6 changed files with 42 additions and 45 deletions

View File

@@ -3,8 +3,13 @@
namespace App\Config\Connector;
use App\Entity\Domain;
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);
}

View File

@@ -3,6 +3,7 @@
namespace App\Config\Connector;
use App\Entity\Domain;
use http\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -29,26 +30,16 @@ readonly class GandiConnector implements ConnectorInterface
public function orderDomain(Domain $domain, bool $dryRun = false): void
{
if (!$domain->getDeleted()) {
throw new \Exception('The domain name still appears in the WHOIS database');
throw new InvalidArgumentException('The domain name still appears in the WHOIS database');
}
$ldhName = $domain->getLdhName();
if (!$ldhName) {
throw new \Exception('Domain name cannot be null');
throw new InvalidArgumentException('Domain name cannot be null');
}
$authData = self::verifyAuthData($this->authData, $this->client);
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
$user = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
->setAuthBearer($authData['token'])
->setHeader('Accept', 'application/json')
@@ -112,7 +103,7 @@ readonly class GandiConnector implements ConnectorInterface
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent', null);
throw new HttpException(451, 'The user has not given explicit consent');
}
$response = $client->request('GET', '/v5/organization/user-info', (new HttpOptions())

View File

@@ -6,6 +6,7 @@ use App\Entity\Domain;
use Ovh\Api;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
readonly class OvhConnector implements ConnectorInterface
{
@@ -31,7 +32,7 @@ readonly class OvhConnector implements ConnectorInterface
],
];
public function __construct(private array $authData)
public function __construct(private array $authData, private HttpClientInterface $client)
{
}
@@ -51,18 +52,12 @@ readonly class OvhConnector implements ConnectorInterface
throw new \Exception('Domain name cannot be null');
}
$authData = self::verifyAuthData($this->authData);
$authData = self::verifyAuthData($this->authData, $this->client);
$acceptConditions = $authData['acceptConditions'];
$ownerLegalAge = $authData['ownerLegalAge'];
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent');
}
$conn = new Api(
$authData['appKey'],
$authData['appSecret'],
@@ -125,7 +120,7 @@ readonly class OvhConnector implements ConnectorInterface
/**
* @throws \Exception
*/
public static function verifyAuthData(array $authData): array
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
{
$appKey = $authData['appKey'];
$appSecret = $authData['appSecret'];
@@ -151,7 +146,7 @@ readonly class OvhConnector implements ConnectorInterface
if (true !== $acceptConditions
|| true !== $ownerLegalAge
|| true !== $waiveRetractationPeriod) {
throw new HttpException(451, 'The user has not given explicit consent', null);
throw new HttpException(451, 'The user has not given explicit consent');
}
$conn = new Api(

View File

@@ -2,8 +2,19 @@
namespace App\Config;
use App\Config\Connector\GandiConnector;
use App\Config\Connector\OvhConnector;
enum ConnectorProvider: string
{
case OVH = 'ovh';
case GANDI = 'gandi';
public function getConnectorProvider(): string
{
return match ($this) {
ConnectorProvider::OVH => OvhConnector::class,
ConnectorProvider::GANDI => GandiConnector::class
};
}
}