fix: isToBeWatchClosely function

This commit is contained in:
Maël Gangloff
2024-07-25 20:21:57 +02:00
parent d153d3af78
commit 8ede04cfa6
3 changed files with 28 additions and 27 deletions

View File

@@ -40,17 +40,18 @@ class DomainRefreshController extends AbstractController
{ {
/** @var Domain $domain */ /** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]); $domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
if ($domain !== null && $domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days < 7) return $domain;
// If the domain name exists in the database, recently updated and not important, we return the stored Domain
if ($domain !== null && ($domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days < 7) && !$this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt())) return $domain;
if (false === $kernel->isDebug()) { if (false === $kernel->isDebug()) {
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier()); $limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
if (false === $limiter->consume()->isAccepted()) throw new TooManyRequestsHttpException(); if (false === $limiter->consume()->isAccepted()) throw new TooManyRequestsHttpException();
} }
if ($domain === null) return $this->RDAPService->registerDomain($ldhName); $updatedAt = $domain === null ? new DateTimeImmutable('now') : $domain->getUpdatedAt();
$updatedAt = $domain->getUpdatedAt();
$domain = $this->RDAPService->registerDomain($ldhName); $domain = $this->RDAPService->registerDomain($ldhName);
/** @var WatchList $watchList */ /** @var WatchList $watchList */
foreach ($domain->getWatchLists()->getIterator() as $watchList) { foreach ($domain->getWatchLists()->getIterator() as $watchList) {
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt)); $this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));

View File

@@ -2,9 +2,7 @@
namespace App\MessageHandler; namespace App\MessageHandler;
use App\Config\EventAction;
use App\Entity\Domain; use App\Entity\Domain;
use App\Entity\DomainEvent;
use App\Entity\User; use App\Entity\User;
use App\Entity\WatchList; use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger; use App\Message\ProcessDomainTrigger;
@@ -25,7 +23,6 @@ use Throwable;
#[AsMessageHandler] #[AsMessageHandler]
final readonly class ProcessWatchListTriggerHandler final readonly class ProcessWatchListTriggerHandler
{ {
const IMPORTANT_EVENTS = [EventAction::Deletion->value, EventAction::Expiration->value];
public function __construct( public function __construct(
private RDAPService $RDAPService, private RDAPService $RDAPService,
@@ -51,7 +48,7 @@ final readonly class ProcessWatchListTriggerHandler
->filter(fn($domain) => $domain->getUpdatedAt() ->filter(fn($domain) => $domain->getUpdatedAt()
->diff( ->diff(
new DateTimeImmutable('now'))->days >= 7 new DateTimeImmutable('now'))->days >= 7
|| self::isToBeWatchClosely($domain, $domain->getUpdatedAt()) || $this->RDAPService::isToBeWatchClosely($domain, $domain->getUpdatedAt())
) as $domain ) as $domain
) { ) {
$updatedAt = $domain->getUpdatedAt(); $updatedAt = $domain->getUpdatedAt();
@@ -66,7 +63,7 @@ final readonly class ProcessWatchListTriggerHandler
/** /**
* If the domain name must be consulted regularly, we reschedule an update in one day * If the domain name must be consulted regularly, we reschedule an update in one day
*/ */
if (self::isToBeWatchClosely($domain, $updatedAt)) { if ($this->RDAPService::isToBeWatchClosely($domain, $updatedAt)) {
$this->bus->dispatch(new ProcessWatchListTrigger($message->watchListToken), [ $this->bus->dispatch(new ProcessWatchListTrigger($message->watchListToken), [
new DelayStamp(24 * 60 * 60 * 1e3)]); new DelayStamp(24 * 60 * 60 * 1e3)]);
} }
@@ -75,22 +72,6 @@ final readonly class ProcessWatchListTriggerHandler
} }
} }
/**
* Determines if a domain name needs special attention.
* These domain names are those whose last event was expiration or deletion.
* @throws Exception
*/
public static function isToBeWatchClosely(Domain $domain, DateTimeImmutable $updatedAt): bool
{
if ($updatedAt->diff(new DateTimeImmutable('now'))->days < 1) return false;
/** @var DomainEvent[] $events */
$events = $domain->getEvents()->toArray();
usort($events, fn(DomainEvent $e1, DomainEvent $e2) => $e2->getDate() - $e1->getDate());
return !empty($events) && in_array($events[0]->getAction(), self::IMPORTANT_EVENTS);
}
/** /**
* @throws TransportExceptionInterface * @throws TransportExceptionInterface

View File

@@ -72,6 +72,8 @@ readonly class RDAPService
'xn--hlcj6aya9esc7a' 'xn--hlcj6aya9esc7a'
]; ];
const IMPORTANT_EVENTS = [EventAction::Deletion->value, EventAction::Expiration->value];
public function __construct(private HttpClientInterface $client, public function __construct(private HttpClientInterface $client,
private EntityRepository $entityRepository, private EntityRepository $entityRepository,
private DomainRepository $domainRepository, private DomainRepository $domainRepository,
@@ -88,6 +90,25 @@ readonly class RDAPService
} }
/**
* Determines if a domain name needs special attention.
* These domain names are those whose last event was expiration or deletion.
* @throws Exception
*/
public static function isToBeWatchClosely(Domain $domain, DateTimeImmutable $updatedAt): bool
{
if ($updatedAt->diff(new DateTimeImmutable('now'))->days < 1) return false;
/** @var DomainEvent[] $events */
$events = $domain->getEvents()
->filter(fn(DomainEvent $e) => $e->getDate() <= new DateTimeImmutable('now'))
->toArray();
usort($events, fn(DomainEvent $e1, DomainEvent $e2) => $e2->getDate() > $e1->getDate());
return !empty($events) && in_array($events[0]->getAction(), self::IMPORTANT_EVENTS);
}
/** /**
* @throws HttpExceptionInterface * @throws HttpExceptionInterface
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
@@ -253,7 +274,6 @@ readonly class RDAPService
return $domain; return $domain;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
@@ -409,7 +429,6 @@ readonly class RDAPService
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
* @throws DecodingExceptionInterface * @throws DecodingExceptionInterface
* @throws Exception * @throws Exception
* @throws ORMException
*/ */
public function updateGTldListICANN(): void public function updateGTldListICANN(): void
{ {