Files
domain-watchdog/src/Service/Connector/GandiProvider.php

144 lines
5.1 KiB
PHP
Raw Normal View History

2024-08-06 03:38:00 +02:00
<?php
2024-08-19 21:17:57 +02:00
namespace App\Service\Connector;
2024-08-06 03:38:00 +02:00
use App\Dto\Connector\DefaultProviderDto;
use App\Dto\Connector\GandiProviderDto;
2024-08-06 03:38:00 +02:00
use App\Entity\Domain;
2024-08-23 02:35:09 +02:00
use Psr\Cache\CacheItemInterface;
2024-09-26 13:39:35 +02:00
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
2024-08-06 03:38:00 +02:00
use Symfony\Component\HttpClient\HttpOptions;
use Symfony\Component\HttpFoundation\Response;
2024-08-06 21:52:35 +02:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
2024-08-06 03:38:00 +02:00
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
2024-08-06 03:38:00 +02:00
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Autoconfigure(public: true)]
2024-08-23 02:35:09 +02:00
class GandiProvider extends AbstractProvider
2024-08-06 03:38:00 +02:00
{
protected string $dtoClass = GandiProviderDto::class;
/** @var GandiProviderDto */
protected DefaultProviderDto $authData;
private const BASE_URL = 'https://api.gandi.net';
2024-08-06 03:38:00 +02:00
public function __construct(
CacheItemPoolInterface $cacheItemPool,
private readonly HttpClientInterface $client,
DenormalizerInterface&NormalizerInterface $serializer,
ValidatorInterface $validator,
) {
parent::__construct($cacheItemPool, $serializer, $validator);
2024-08-06 03:38:00 +02:00
}
/**
* Order a domain name with the Gandi API.
*
* @throws \Exception
* @throws TransportExceptionInterface
* @throws DecodingExceptionInterface
*/
public function orderDomain(Domain $domain, bool $dryRun = false): void
{
$ldhName = $domain->getLdhName();
if (!$ldhName) {
2024-08-30 12:54:42 +02:00
throw new \InvalidArgumentException('Domain name cannot be null');
2024-08-06 03:38:00 +02:00
}
$user = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
->setAuthBearer($this->authData->token)
2024-08-06 03:38:00 +02:00
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->toArray()
)->toArray();
$httpOptions = (new HttpOptions())
->setAuthBearer($this->authData->token)
2024-08-06 03:38:00 +02:00
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->setHeader('Dry-Run', $dryRun ? '1' : '0')
->setJson([
'fqdn' => $ldhName,
'owner' => [
'email' => $user['email'],
'given' => $user['firstname'],
'family' => $user['lastname'],
'streetaddr' => $user['streetaddr'],
'zip' => $user['zip'],
'city' => $user['city'],
'state' => $user['state'],
'phone' => $user['phone'],
'country' => $user['country'],
'type' => 'individual',
],
'tld_period' => 'golive',
]);
if ($this->authData->sharingId) {
2024-08-06 03:38:00 +02:00
$httpOptions->setQuery([
'sharing_id' => $this->authData->sharingId,
2024-08-06 03:38:00 +02:00
]);
}
$res = $this->client->request('POST', '/domain/domains', $httpOptions->toArray());
if ((!$dryRun && Response::HTTP_ACCEPTED !== $res->getStatusCode())
|| ($dryRun && Response::HTTP_OK !== $res->getStatusCode())) {
2024-08-11 15:53:17 +02:00
throw new HttpException($res->toArray()['message']);
2024-08-06 03:38:00 +02:00
}
}
2024-10-02 12:01:07 +02:00
/**
* @throws TransportExceptionInterface
*/
protected function assertAuthentication(): void
2024-09-30 13:48:15 +02:00
{
$response = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
->setAuthBearer($this->authData->token)
2024-09-30 13:48:15 +02:00
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->toArray()
);
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new BadRequestHttpException('The status of these credentials is not valid');
}
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
2024-08-23 02:35:09 +02:00
protected function getSupportedTldList(): array
{
$response = $this->client->request('GET', '/v5/domain/tlds', (new HttpOptions())
->setAuthBearer($this->authData->token)
->setHeader('Accept', 'application/json')
->setBaseUri(self::BASE_URL)
->toArray())->toArray();
2024-08-23 02:35:09 +02:00
return array_map(fn ($tld) => $tld['name'], $response);
}
2024-08-23 02:35:09 +02:00
/**
* @throws \Psr\Cache\InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
2024-08-06 03:38:00 +02:00
}