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;
|
2024-11-01 00:46:25 +01:00
|
|
|
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;
|
2024-08-06 22:11:57 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2024-08-19 16:34:08 +02:00
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
2024-08-06 03:38:00 +02:00
|
|
|
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
2024-08-19 16:34:08 +02:00
|
|
|
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;
|
|
|
|
|
|
2024-11-01 00:46:25 +01:00
|
|
|
#[Autoconfigure(public: true)]
|
2024-08-23 02:35:09 +02:00
|
|
|
class GandiProvider extends AbstractProvider
|
2024-08-06 03:38:00 +02:00
|
|
|
{
|
2024-08-19 16:34:08 +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());
|
|
|
|
|
|
2024-08-06 11:58:41 +02:00
|
|
|
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-11-01 00:46:25 +01: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-08-19 16:34:08 +02:00
|
|
|
|
2024-10-02 12:01:07 +02:00
|
|
|
/**
|
|
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
|
*/
|
2024-11-02 16:11:41 +01:00
|
|
|
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'])
|
|
|
|
|
->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');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 16:34:08 +02:00
|
|
|
/**
|
|
|
|
|
* @throws TransportExceptionInterface
|
|
|
|
|
* @throws ServerExceptionInterface
|
|
|
|
|
* @throws RedirectionExceptionInterface
|
|
|
|
|
* @throws DecodingExceptionInterface
|
|
|
|
|
* @throws ClientExceptionInterface
|
|
|
|
|
*/
|
2024-08-23 02:35:09 +02:00
|
|
|
protected function getSupportedTldList(): array
|
2024-08-19 16:34:08 +02:00
|
|
|
{
|
|
|
|
|
$response = $this->client->request('GET', '/v5/domain/tlds', (new HttpOptions())
|
2024-09-30 13:48:15 +02:00
|
|
|
->setAuthBearer($this->authData['token'])
|
2024-08-19 16:34:08 +02:00
|
|
|
->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-19 16:34:08 +02:00
|
|
|
|
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-19 16:34:08 +02:00
|
|
|
}
|
2024-08-06 03:38:00 +02:00
|
|
|
}
|