mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: start Open Provider support
This commit is contained in:
34
src/Dto/Connector/OpenProviderProviderDto.php
Normal file
34
src/Dto/Connector/OpenProviderProviderDto.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dto\Connector;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
final class OpenProviderProviderDto extends DefaultProviderDto
|
||||
{
|
||||
#[Assert\NotBlank]
|
||||
public string $token;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
public string $adminHandle;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
public string $billingHandle;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
public string $ownerHandle;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
public string $techHandle;
|
||||
|
||||
public ?string $resellerHandle;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
public int $period = 1;
|
||||
|
||||
public string $nsGroup;
|
||||
|
||||
#[Assert\Choice(['off', 'on', 'default'])]
|
||||
#[Assert\NotBlank]
|
||||
public string $autoRenew = 'default';
|
||||
}
|
||||
114
src/Service/Connector/OpenProviderProvider.php
Normal file
114
src/Service/Connector/OpenProviderProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Connector;
|
||||
|
||||
use App\Dto\Connector\DefaultProviderDto;
|
||||
use App\Dto\Connector\OpenProviderProviderDto;
|
||||
use App\Entity\Domain;
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\HttpClient\HttpOptions;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
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\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
#[Autoconfigure(public: true)]
|
||||
class OpenProviderProvider extends AbstractProvider
|
||||
{
|
||||
protected string $dtoClass = OpenProviderProviderDto::class;
|
||||
|
||||
/** @var OpenProviderProviderDto */
|
||||
protected DefaultProviderDto $authData;
|
||||
|
||||
private const BASE_URL = 'https://api.openprovider.eu/v1beta';
|
||||
|
||||
public function __construct(
|
||||
CacheItemPoolInterface $cacheItemPool,
|
||||
private readonly HttpClientInterface $client,
|
||||
DenormalizerInterface&NormalizerInterface $serializer,
|
||||
ValidatorInterface $validator,
|
||||
) {
|
||||
parent::__construct($cacheItemPool, $serializer, $validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order a domain name with the Open Provider API.
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
*/
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false): void
|
||||
{
|
||||
$ldhName = $domain->getLdhName();
|
||||
if (!$ldhName) {
|
||||
throw new \InvalidArgumentException('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'admin_handle' => $this->authData->adminHandle,
|
||||
'billing_handle' => $this->authData->billingHandle,
|
||||
'owner_handle' => $this->authData->ownerHandle,
|
||||
'tech_handle' => $this->authData->techHandle,
|
||||
'domain' => [
|
||||
'name' => explode('.', $domain->getLdhName(), 2)[0],
|
||||
'extension' => explode('.', $domain->getLdhName(), 2)[1],
|
||||
],
|
||||
'period' => '1',
|
||||
'ns_group' => $this->authData->nsGroup,
|
||||
'autorenew' => 'default',
|
||||
];
|
||||
|
||||
if (null !== $this->authData->resellerHandle) {
|
||||
$payload['resellerHandle'] = $this->authData->resellerHandle;
|
||||
}
|
||||
|
||||
$res = $this->client->request('POST', '/domain', (new HttpOptions())
|
||||
->setAuthBearer($this->authData->token)
|
||||
->setHeader('Accept', 'application/json')
|
||||
->setBaseUri(self::BASE_URL)
|
||||
->setJson($payload)->toArray());
|
||||
|
||||
if ((!$dryRun && Response::HTTP_ACCEPTED !== $res->getStatusCode())
|
||||
|| ($dryRun && Response::HTTP_OK !== $res->getStatusCode())) {
|
||||
throw new HttpException($res->toArray()['message']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
protected function assertAuthentication(): void
|
||||
{
|
||||
$response = $this->client->request('GET', '/customers', (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');
|
||||
}
|
||||
}
|
||||
|
||||
protected function getSupportedTldList(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Psr\Cache\InvalidArgumentException
|
||||
*/
|
||||
protected function getCachedTldList(): CacheItemInterface
|
||||
{
|
||||
return $this->cacheItemPool->getItem('app.provider.openprovider.supported-tld');
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpClient\Exception\ClientException;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
@@ -264,7 +265,7 @@ class RDAPService
|
||||
private function handleRdapException(\Exception $e, string $idnDomain, ?Domain $domain, ?ResponseInterface $response): \Exception
|
||||
{
|
||||
if (
|
||||
($e instanceof ClientException && 404 === $e->getResponse()->getStatusCode())
|
||||
($e instanceof ClientException && Response::HTTP_NOT_FOUND === $e->getResponse()->getStatusCode())
|
||||
|| ($e instanceof TransportExceptionInterface && null !== $response && !in_array('content-length', $response->getHeaders(false)) && 404 === $response->getStatusCode())
|
||||
) {
|
||||
if (null !== $domain) {
|
||||
|
||||
Reference in New Issue
Block a user