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

151 lines
5.2 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\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\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
{
private const BASE_URL = 'https://api.gandi.net';
2024-08-06 03:38:00 +02:00
2024-10-02 12:59:37 +02:00
public function __construct(CacheItemPoolInterface $cacheItemPool, private readonly HttpClientInterface $client)
2024-08-06 03:38:00 +02:00
{
2024-09-26 13:39:35 +02:00
parent::__construct($cacheItemPool);
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())
2024-09-30 13:48:15 +02:00
->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())
2024-09-30 13:48:15 +02:00
->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',
]);
2024-09-30 13:48:15 +02:00
if (array_key_exists('sharingId', $this->authData)) {
2024-08-06 03:38:00 +02:00
$httpOptions->setQuery([
2024-09-30 13:48:15 +02:00
'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
}
}
public function verifySpecificAuthData(array $authData): array
2024-08-06 03:38:00 +02:00
{
$token = $authData['token'];
if (!is_string($token) || empty($token)
|| (array_key_exists('sharingId', $authData) && !is_string($authData['sharingId']))
) {
2024-08-06 21:52:35 +02:00
throw new BadRequestHttpException('Bad authData schema');
2024-08-06 03:38:00 +02:00
}
$authDataReturned = [
'token' => $token,
];
if (array_key_exists('sharingId', $authData)) {
$authDataReturned['sharingId'] = $authData['sharingId'];
}
return $authDataReturned;
}
2024-10-02 12:01:07 +02:00
/**
* @throws TransportExceptionInterface
*/
2024-09-30 13:48:15 +02:00
public function assertAuthentication(): void
{
$response = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
->setAuthBearer($this->authData['token'])
->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())
2024-09-30 13:48:15 +02:00
->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
}