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

212 lines
6.5 KiB
PHP
Raw Normal View History

2024-07-28 23:05:41 +02:00
<?php
2024-08-19 21:17:57 +02:00
namespace App\Service\Connector;
2024-07-28 23:05:41 +02:00
use App\Dto\Connector\DefaultProviderDto;
use App\Dto\Connector\OvhProviderDto;
2024-07-29 15:28:05 +02:00
use App\Entity\Domain;
use GuzzleHttp\Exception\ClientException;
2024-07-30 18:48:23 +02:00
use Ovh\Api;
use Ovh\Exceptions\InvalidParameterException;
2024-08-23 02:35:09 +02:00
use Psr\Cache\CacheItemInterface;
2024-09-30 13:48:15 +02:00
use Psr\Cache\CacheItemPoolInterface;
2024-08-23 02:35:09 +02:00
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
2024-08-06 21:43:37 +02:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
2024-07-28 23:05:41 +02:00
#[Autoconfigure(public: true)]
2024-08-23 02:35:09 +02:00
class OvhProvider extends AbstractProvider
2024-07-28 23:05:41 +02:00
{
protected string $dtoClass = OvhProviderDto::class;
/** @var OvhProviderDto */
protected DefaultProviderDto $authData;
2024-08-06 00:35:05 +02:00
public const REQUIRED_ROUTES = [
[
'method' => 'GET',
'path' => '/domain/extensions',
],
2024-08-06 00:35:05 +02:00
[
'method' => 'GET',
'path' => '/order/cart',
2024-08-19 21:17:57 +02:00
],
[
2024-08-06 00:35:05 +02:00
'method' => 'GET',
'path' => '/order/cart/*',
],
[
'method' => 'POST',
'path' => '/order/cart',
],
[
'method' => 'POST',
'path' => '/order/cart/*',
],
[
'method' => 'DELETE',
'path' => '/order/cart/*',
],
];
public function __construct(
CacheItemPoolInterface $cacheItemPool,
DenormalizerInterface&NormalizerInterface $serializer,
ValidatorInterface $validator,
) {
parent::__construct($cacheItemPool, $serializer, $validator);
2024-07-29 15:28:05 +02:00
}
2024-07-29 03:27:55 +02:00
/**
2024-08-02 23:24:52 +02:00
* Order a domain name with the OVH API.
*
* @throws \Exception
2024-07-29 03:27:55 +02:00
*/
2024-08-06 03:38:00 +02:00
public function orderDomain(Domain $domain, bool $dryRun = false): void
{
2024-07-29 03:27:55 +02:00
$ldhName = $domain->getLdhName();
2024-08-02 23:24:52 +02:00
if (!$ldhName) {
2024-08-30 12:54:42 +02:00
throw new \InvalidArgumentException('Domain name cannot be null');
2024-08-02 23:24:52 +02:00
}
2024-07-29 03:27:55 +02:00
$acceptConditions = $this->authData->acceptConditions;
$ownerLegalAge = $this->authData->ownerLegalAge;
$waiveRetractationPeriod = $this->authData->waiveRetractationPeriod;
2024-07-29 03:27:55 +02:00
$conn = new Api(
$this->authData->appKey,
$this->authData->appSecret,
$this->authData->apiEndpoint,
$this->authData->consumerKey,
2024-07-29 03:27:55 +02:00
);
$cart = $conn->post('/order/cart', [
'ovhSubsidiary' => $this->authData->ovhSubsidiary,
2024-08-02 23:24:52 +02:00
'description' => 'Domain Watchdog',
2024-07-29 03:27:55 +02:00
]);
$cartId = $cart['cartId'];
$offers = $conn->get("/order/cart/{$cartId}/domain", [
2024-08-02 23:24:52 +02:00
'domain' => $ldhName,
2024-07-29 03:27:55 +02:00
]);
2024-08-10 00:27:30 +02:00
2024-08-10 00:56:46 +02:00
$pricingModes = ['create-default'];
if ('create-default' !== $this->authData->pricingMode) {
$pricingModes[] = $this->authData->pricingMode;
2024-08-10 00:27:30 +02:00
}
2024-08-02 23:24:52 +02:00
$offer = array_filter($offers, fn ($offer) => 'create' === $offer['action']
&& true === $offer['orderable']
2024-08-10 00:27:30 +02:00
&& in_array($offer['pricingMode'], $pricingModes)
2024-07-29 03:27:55 +02:00
);
2024-07-30 01:01:04 +02:00
if (empty($offer)) {
$conn->delete("/order/cart/{$cartId}");
2024-08-30 12:54:42 +02:00
throw new \InvalidArgumentException('Cannot buy this domain name');
2024-07-30 01:01:04 +02:00
}
2024-07-29 03:27:55 +02:00
$item = $conn->post("/order/cart/{$cartId}/domain", [
2024-08-02 23:24:52 +02:00
'domain' => $ldhName,
'duration' => 'P1Y',
2024-07-29 03:27:55 +02:00
]);
$itemId = $item['itemId'];
2024-08-02 23:24:52 +02:00
// $conn->get("/order/cart/{$cartId}/summary");
2024-07-29 03:27:55 +02:00
$conn->post("/order/cart/{$cartId}/assign");
$conn->get("/order/cart/{$cartId}/item/{$itemId}/requiredConfiguration");
$configuration = [
2024-08-02 23:24:52 +02:00
'ACCEPT_CONDITIONS' => $acceptConditions,
'OWNER_LEGAL_AGE' => $ownerLegalAge,
2024-07-29 03:27:55 +02:00
];
foreach ($configuration as $label => $value) {
$conn->post("/order/cart/{$cartId}/item/{$itemId}/configuration", [
2024-08-02 23:24:52 +02:00
'cartId' => $cartId,
'itemId' => $itemId,
'label' => $label,
'value' => $value,
2024-07-29 03:27:55 +02:00
]);
}
$conn->get("/order/cart/{$cartId}/checkout");
2024-07-29 11:54:47 +02:00
2024-08-02 23:24:52 +02:00
if ($dryRun) {
return;
}
2024-07-29 03:27:55 +02:00
$conn->post("/order/cart/{$cartId}/checkout", [
2024-08-02 23:24:52 +02:00
'autoPayWithPreferredPaymentMethod' => true,
'waiveRetractationPeriod' => $waiveRetractationPeriod,
2024-07-29 03:27:55 +02:00
]);
}
2024-07-29 15:28:05 +02:00
protected function assertAuthentication(): void
2024-09-30 13:48:15 +02:00
{
2024-07-30 18:48:23 +02:00
$conn = new Api(
$this->authData->appKey,
$this->authData->appSecret,
$this->authData->apiEndpoint,
$this->authData->consumerKey,
);
try {
$res = $conn->get('/auth/currentCredential');
if (null !== $res['expiration'] && new \DateTimeImmutable($res['expiration']) < new \DateTimeImmutable()) {
throw new BadRequestHttpException('These credentials have expired');
}
$status = $res['status'];
if ('validated' !== $status) {
throw new BadRequestHttpException("The status of these credentials is not valid ($status)");
}
} catch (ClientException $exception) {
throw new BadRequestHttpException($exception->getMessage());
2024-08-02 23:24:52 +02:00
}
2024-07-29 15:28:05 +02:00
2024-08-06 00:35:05 +02:00
foreach (self::REQUIRED_ROUTES as $requiredRoute) {
$ok = false;
foreach ($res['rules'] as $allowedRoute) {
if (
$requiredRoute['method'] === $allowedRoute['method']
&& fnmatch($allowedRoute['path'], $requiredRoute['path'])
) {
$ok = true;
}
}
if (!$ok) {
2024-08-19 16:48:37 +02:00
throw new BadRequestHttpException('This Connector does not have enough permissions on the Provider API. Please recreate this Connector.');
2024-08-06 00:35:05 +02:00
}
}
2024-07-29 15:28:05 +02:00
}
/**
* @throws InvalidParameterException
* @throws \JsonException
* @throws \Exception
*/
2024-08-23 02:35:09 +02:00
protected function getSupportedTldList(): array
{
$conn = new Api(
$this->authData->appKey,
$this->authData->appSecret,
$this->authData->apiEndpoint,
$this->authData->consumerKey,
);
2024-08-23 02:35:09 +02:00
return $conn->get('/domain/extensions', [
'ovhSubsidiary' => $this->authData->ovhSubsidiary,
]);
2024-08-23 02:35:09 +02:00
}
2024-08-23 02:35:09 +02:00
/**
* @throws InvalidArgumentException
*/
protected function getCachedTldList(): CacheItemInterface
{
return $this->cacheItemPool->getItem('app.provider.ovh.supported-tld');
}
2024-08-02 23:24:52 +02:00
}