Files
domain-watchdog/src/State/AutoRegisterDomainProvider.php

113 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Entity\Domain;
2025-10-14 23:34:17 +02:00
use App\Entity\WatchList;
2025-10-22 15:58:20 +02:00
use App\Exception\DomainNotFoundException;
use App\Exception\MalformedDomainException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
2025-10-14 23:34:17 +02:00
use App\Message\SendDomainEventNotif;
use App\Repository\DomainRepository;
use App\Service\RDAPService;
2025-10-22 15:58:20 +02:00
use Doctrine\ORM\OptimisticLockException;
2025-10-14 23:34:17 +02:00
use Psr\Log\LoggerInterface;
use Random\Randomizer;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
2025-10-16 11:30:33 +02:00
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
2025-10-22 15:58:20 +02:00
use Symfony\Component\Messenger\Exception\ExceptionInterface;
2025-10-14 23:34:17 +02:00
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory;
2025-10-22 15:58:20 +02:00
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2025-06-14 00:09:41 +02:00
readonly class AutoRegisterDomainProvider implements ProviderInterface
{
public function __construct(
2025-06-14 00:09:41 +02:00
private RDAPService $RDAPService,
private KernelInterface $kernel,
private ParameterBagInterface $parameterBag,
2025-10-14 23:34:17 +02:00
private RateLimiterFactory $rdapRequestsLimiter,
private Security $security,
private LoggerInterface $logger,
private DomainRepository $domainRepository,
private MessageBusInterface $bus,
2025-10-16 11:30:33 +02:00
private RequestStack $requestStack,
) {
}
2025-10-22 15:58:20 +02:00
/**
* @throws RedirectionExceptionInterface
* @throws DomainNotFoundException
* @throws TldNotSupportedException
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws OptimisticLockException
* @throws TransportExceptionInterface
* @throws MalformedDomainException
* @throws ServerExceptionInterface
* @throws UnknownRdapServerException
* @throws ExceptionInterface
* @throws \Exception
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
2025-10-14 23:34:17 +02:00
$userId = $this->security->getUser()->getUserIdentifier();
$idnDomain = RDAPService::convertToIdn($uriVariables['ldhName']);
$this->logger->info('User wants to update a domain name', [
'username' => $userId,
'ldhName' => $idnDomain,
]);
2025-10-16 11:30:33 +02:00
$request = $this->requestStack->getCurrentRequest();
2025-10-14 23:34:17 +02:00
/** @var ?Domain $domain */
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
if (null !== $domain
&& !$domain->getDeleted()
2025-10-22 15:58:20 +02:00
&& !$this->RDAPService->isToBeUpdated($domain, true, true)
2025-10-16 11:30:33 +02:00
&& ($request && !filter_var($request->get('forced', false), FILTER_VALIDATE_BOOLEAN))
2025-10-14 23:34:17 +02:00
) {
$this->logger->debug('It is not necessary to update the domain name', [
'ldhName' => $idnDomain,
'updatedAt' => $domain->getUpdatedAt()->format(\DateTimeInterface::ATOM),
]);
return $domain;
}
if (false === $this->kernel->isDebug() && true === $this->parameterBag->get('limited_features')) {
2025-10-14 23:34:17 +02:00
$limiter = $this->rdapRequestsLimiter->create($userId);
$limit = $limiter->consume();
if (!$limit->isAccepted()) {
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
}
}
2025-10-14 23:34:17 +02:00
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
$domain = $this->RDAPService->registerDomain($idnDomain);
2025-10-14 23:34:17 +02:00
$randomizer = new Randomizer();
$watchLists = $randomizer->shuffleArray($domain->getWatchLists()->toArray());
2025-10-14 23:34:17 +02:00
/** @var WatchList $watchList */
foreach ($watchLists as $watchList) {
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
return $domain;
}
}