mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-17 17:55:42 +00:00
feat: improve Connector abstract
This commit is contained in:
parent
e04e880d33
commit
f3650f3b9f
@ -3,8 +3,13 @@
|
||||
namespace App\Config\Connector;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
interface ConnectorInterface
|
||||
{
|
||||
public function __construct(array $authData, HttpClientInterface $client);
|
||||
|
||||
public function orderDomain(Domain $domain, bool $dryRun): void;
|
||||
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Config\Connector;
|
||||
|
||||
use App\Entity\Domain;
|
||||
use http\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\HttpClient\HttpOptions;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
@ -29,26 +30,16 @@ readonly class GandiConnector implements ConnectorInterface
|
||||
public function orderDomain(Domain $domain, bool $dryRun = false): void
|
||||
{
|
||||
if (!$domain->getDeleted()) {
|
||||
throw new \Exception('The domain name still appears in the WHOIS database');
|
||||
throw new InvalidArgumentException('The domain name still appears in the WHOIS database');
|
||||
}
|
||||
|
||||
$ldhName = $domain->getLdhName();
|
||||
if (!$ldhName) {
|
||||
throw new \Exception('Domain name cannot be null');
|
||||
throw new InvalidArgumentException('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$authData = self::verifyAuthData($this->authData, $this->client);
|
||||
|
||||
$acceptConditions = $authData['acceptConditions'];
|
||||
$ownerLegalAge = $authData['ownerLegalAge'];
|
||||
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
|
||||
|
||||
if (true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod) {
|
||||
throw new HttpException(451, 'The user has not given explicit consent');
|
||||
}
|
||||
|
||||
$user = $this->client->request('GET', '/v5/organization/user-info', (new HttpOptions())
|
||||
->setAuthBearer($authData['token'])
|
||||
->setHeader('Accept', 'application/json')
|
||||
@ -112,7 +103,7 @@ readonly class GandiConnector implements ConnectorInterface
|
||||
if (true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod) {
|
||||
throw new HttpException(451, 'The user has not given explicit consent', null);
|
||||
throw new HttpException(451, 'The user has not given explicit consent');
|
||||
}
|
||||
|
||||
$response = $client->request('GET', '/v5/organization/user-info', (new HttpOptions())
|
||||
|
||||
@ -6,6 +6,7 @@ use App\Entity\Domain;
|
||||
use Ovh\Api;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
readonly class OvhConnector implements ConnectorInterface
|
||||
{
|
||||
@ -31,7 +32,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(private array $authData)
|
||||
public function __construct(private array $authData, private HttpClientInterface $client)
|
||||
{
|
||||
}
|
||||
|
||||
@ -51,18 +52,12 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
throw new \Exception('Domain name cannot be null');
|
||||
}
|
||||
|
||||
$authData = self::verifyAuthData($this->authData);
|
||||
$authData = self::verifyAuthData($this->authData, $this->client);
|
||||
|
||||
$acceptConditions = $authData['acceptConditions'];
|
||||
$ownerLegalAge = $authData['ownerLegalAge'];
|
||||
$waiveRetractationPeriod = $authData['waiveRetractationPeriod'];
|
||||
|
||||
if (true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod) {
|
||||
throw new HttpException(451, 'The user has not given explicit consent');
|
||||
}
|
||||
|
||||
$conn = new Api(
|
||||
$authData['appKey'],
|
||||
$authData['appSecret'],
|
||||
@ -125,7 +120,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function verifyAuthData(array $authData): array
|
||||
public static function verifyAuthData(array $authData, HttpClientInterface $client): array
|
||||
{
|
||||
$appKey = $authData['appKey'];
|
||||
$appSecret = $authData['appSecret'];
|
||||
@ -151,7 +146,7 @@ readonly class OvhConnector implements ConnectorInterface
|
||||
if (true !== $acceptConditions
|
||||
|| true !== $ownerLegalAge
|
||||
|| true !== $waiveRetractationPeriod) {
|
||||
throw new HttpException(451, 'The user has not given explicit consent', null);
|
||||
throw new HttpException(451, 'The user has not given explicit consent');
|
||||
}
|
||||
|
||||
$conn = new Api(
|
||||
|
||||
@ -2,8 +2,19 @@
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
use App\Config\Connector\GandiConnector;
|
||||
use App\Config\Connector\OvhConnector;
|
||||
|
||||
enum ConnectorProvider: string
|
||||
{
|
||||
case OVH = 'ovh';
|
||||
case GANDI = 'gandi';
|
||||
|
||||
public function getConnectorProvider(): string
|
||||
{
|
||||
return match ($this) {
|
||||
ConnectorProvider::OVH => OvhConnector::class,
|
||||
ConnectorProvider::GANDI => GandiConnector::class
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,9 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Config\Connector\GandiConnector;
|
||||
use App\Config\Connector\OvhConnector;
|
||||
use App\Config\ConnectorProvider;
|
||||
use App\Config\Connector\ConnectorInterface;
|
||||
use App\Entity\Connector;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
@ -12,7 +10,6 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
@ -71,14 +68,14 @@ class ConnectorController extends AbstractController
|
||||
'provider' => $provider->value,
|
||||
]);
|
||||
|
||||
if (ConnectorProvider::OVH === $provider) {
|
||||
$authData = OvhConnector::verifyAuthData($connector->getAuthData());
|
||||
} elseif (ConnectorProvider::GANDI === $provider) {
|
||||
$authData = GandiConnector::verifyAuthData($connector->getAuthData(), $client);
|
||||
} else {
|
||||
throw new BadRequestHttpException('Unknown provider');
|
||||
if (null === $provider) {
|
||||
throw new \Exception('Provider not found');
|
||||
}
|
||||
|
||||
/** @var ConnectorInterface $connectorProviderClass */
|
||||
$connectorProviderClass = $provider->getConnectorProvider();
|
||||
|
||||
$authData = $connectorProviderClass::verifyAuthData($connector->getAuthData(), $client);
|
||||
$connector->setAuthData($authData);
|
||||
|
||||
$this->logger->info('User {username} authentication data with the {provider} provider has been validated.', [
|
||||
|
||||
@ -2,9 +2,7 @@
|
||||
|
||||
namespace App\MessageHandler;
|
||||
|
||||
use App\Config\Connector\GandiConnector;
|
||||
use App\Config\Connector\OvhConnector;
|
||||
use App\Config\ConnectorProvider;
|
||||
use App\Config\Connector\ConnectorInterface;
|
||||
use App\Config\TriggerAction;
|
||||
use App\Entity\Connector;
|
||||
use App\Entity\Domain;
|
||||
@ -60,17 +58,17 @@ final readonly class ProcessDomainTriggerHandler
|
||||
'provider' => $connector->getProvider()->value,
|
||||
]);
|
||||
try {
|
||||
$isDebug = $this->kernel->isDebug();
|
||||
|
||||
if (ConnectorProvider::OVH === $connector->getProvider()) {
|
||||
$provider = new OvhConnector($connector->getAuthData());
|
||||
} elseif (ConnectorProvider::GANDI === $connector->getProvider()) {
|
||||
$provider = new GandiConnector($connector->getAuthData(), $this->client);
|
||||
} else {
|
||||
throw new \Exception('Unknown provider');
|
||||
$provider = $connector->getProvider();
|
||||
if (null === $provider) {
|
||||
throw new \Exception('Provider not found');
|
||||
}
|
||||
|
||||
$provider->orderDomain($domain, $isDebug);
|
||||
$connectorProviderClass = $provider->getConnectorProvider();
|
||||
|
||||
/** @var ConnectorInterface $connectorProvider */
|
||||
$connectorProvider = new $connectorProviderClass($connector->getAuthData(), $this->client);
|
||||
|
||||
$connectorProvider->orderDomain($domain, $this->kernel->isDebug());
|
||||
|
||||
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
|
||||
} catch (\Throwable) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user