domain-watchdog/src/Controller/DomainRefreshController.php

42 lines
1.3 KiB
PHP
Raw Normal View History

2024-07-17 18:18:11 +02:00
<?php
namespace App\Controller;
use App\Entity\Domain;
use App\Repository\DomainRepository;
2024-07-17 18:18:11 +02:00
use App\Service\RDAPService;
use DateTimeImmutable;
2024-07-17 18:18:11 +02:00
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
2024-07-17 18:18:11 +02:00
class DomainRefreshController extends AbstractController
{
public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $authenticatedApiLimiter)
{
}
2024-07-17 18:18:11 +02:00
/**
* @throws Exception
*/
public function __invoke(string $ldhName,): ?Domain
2024-07-17 18:18:11 +02:00
{
/** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
if ($domain === null ||
$domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days >= 7) {
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
if (false === $limiter->consume()->isAccepted()) {
throw new TooManyRequestsHttpException();
}
$domain = $this->RDAPService->registerDomain($ldhName);
}
return $domain;
2024-07-17 18:18:11 +02:00
}
}