2024-07-17 18:18:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
2024-07-20 09:45:13 +02:00
|
|
|
use App\Entity\Domain;
|
2024-07-21 16:55:39 +02:00
|
|
|
use App\Entity\WatchList;
|
|
|
|
|
use App\Message\ProcessDomainTrigger;
|
2024-07-20 09:45:13 +02:00
|
|
|
use App\Repository\DomainRepository;
|
2024-07-17 18:18:11 +02:00
|
|
|
use App\Service\RDAPService;
|
2024-07-20 09:45:13 +02:00
|
|
|
use DateTimeImmutable;
|
2024-07-17 18:18:11 +02:00
|
|
|
use Exception;
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2024-07-21 14:56:10 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
2024-07-21 19:47:25 +02:00
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2024-07-21 16:55:39 +02:00
|
|
|
use Symfony\Component\Messenger\Exception\ExceptionInterface;
|
|
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
2024-07-21 14:56:10 +02:00
|
|
|
use Symfony\Component\RateLimiter\RateLimiterFactory;
|
2024-07-17 18:18:11 +02:00
|
|
|
|
|
|
|
|
class DomainRefreshController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
public function __construct(private readonly DomainRepository $domainRepository,
|
|
|
|
|
private readonly RDAPService $RDAPService,
|
|
|
|
|
private readonly RateLimiterFactory $authenticatedApiLimiter,
|
|
|
|
|
private readonly MessageBusInterface $bus)
|
2024-07-20 09:45:13 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 18:18:11 +02:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
2024-07-21 16:55:39 +02:00
|
|
|
* @throws ExceptionInterface
|
2024-07-17 18:18:11 +02:00
|
|
|
*/
|
2024-07-21 19:47:25 +02:00
|
|
|
public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain
|
2024-07-17 18:18:11 +02:00
|
|
|
{
|
2024-07-20 09:45:13 +02:00
|
|
|
/** @var Domain $domain */
|
|
|
|
|
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
|
2024-07-21 16:55:39 +02:00
|
|
|
if ($domain !== null && $domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days < 7) return $domain;
|
2024-07-20 09:45:13 +02:00
|
|
|
|
2024-07-21 19:47:25 +02:00
|
|
|
if (false === $kernel->isDebug()) {
|
2024-07-21 14:56:10 +02:00
|
|
|
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
|
2024-07-21 16:55:39 +02:00
|
|
|
if (false === $limiter->consume()->isAccepted()) throw new TooManyRequestsHttpException();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 19:47:25 +02:00
|
|
|
if ($domain === null) return $this->RDAPService->registerDomain($ldhName);
|
|
|
|
|
|
2024-07-21 16:55:39 +02:00
|
|
|
$updatedAt = $domain->getUpdatedAt();
|
|
|
|
|
$domain = $this->RDAPService->registerDomain($ldhName);
|
|
|
|
|
/** @var WatchList $watchList */
|
|
|
|
|
foreach ($domain->getWatchLists()->getIterator() as $watchList) {
|
|
|
|
|
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
|
2024-07-20 09:45:13 +02:00
|
|
|
}
|
2024-07-21 16:55:39 +02:00
|
|
|
|
2024-07-20 09:45:13 +02:00
|
|
|
return $domain;
|
2024-07-17 18:18:11 +02:00
|
|
|
}
|
|
|
|
|
}
|