domain-watchdog/src/Controller/DomainRefreshController.php

70 lines
2.6 KiB
PHP
Raw Normal View History

2024-07-17 18:18:11 +02:00
<?php
namespace App\Controller;
use App\Entity\Domain;
use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger;
use App\Repository\DomainRepository;
2024-07-17 18:18:11 +02:00
use App\Service\RDAPService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
2024-07-21 19:47:25 +02:00
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2024-07-17 18:18:11 +02:00
class DomainRefreshController extends AbstractController
{
2024-08-02 23:24:52 +02:00
public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $authenticatedApiLimiter,
private readonly MessageBusInterface $bus)
{
}
2024-07-17 18:18:11 +02:00
/**
* @throws TransportExceptionInterface
* @throws HttpExceptionInterface
* @throws DecodingExceptionInterface
* @throws ExceptionInterface
2024-08-02 23:24:52 +02:00
* @throws \Exception
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
{
$idnDomain = strtolower(idn_to_ascii($ldhName));
2024-08-03 00:30:03 +02:00
/** @var ?Domain $domain */
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
2024-07-25 20:21:57 +02:00
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
2024-08-02 23:24:52 +02:00
if (null !== $domain
&& !$domain->getDeleted()
&& ($domain->getUpdatedAt()->diff(new \DateTimeImmutable('now'))->days < 7)
&& !$this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt())
) {
return $domain;
}
2024-07-21 19:47:25 +02:00
if (false === $kernel->isDebug()) {
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
2024-08-02 23:24:52 +02:00
if (false === $limiter->consume()->isAccepted()) {
throw new TooManyRequestsHttpException();
}
}
2024-08-03 00:30:03 +02:00
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
$domain = $this->RDAPService->registerDomain($idnDomain);
2024-07-25 20:21:57 +02:00
/** @var WatchList $watchList */
foreach ($domain->getWatchLists()->getIterator() as $watchList) {
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}
return $domain;
2024-07-17 18:18:11 +02:00
}
2024-08-02 23:24:52 +02:00
}