feat: merging the domain refresh endpoint with the GET endpoint

This commit is contained in:
Maël Gangloff
2024-07-20 09:45:13 +02:00
parent 00a58b2447
commit 0b4f3fb2b3
3 changed files with 22 additions and 14 deletions

View File

@@ -2,18 +2,34 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Domain;
use App\Repository\DomainRepository;
use App\Service\RDAPService; use App\Service\RDAPService;
use DateTimeImmutable;
use Exception; use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DomainRefreshController extends AbstractController class DomainRefreshController extends AbstractController
{ {
public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService)
{
}
/** /**
* @throws Exception * @throws Exception
*/ */
public function __invoke(string $ldhName, RDAPService $RDAPService): void public function __invoke(string $ldhName,): ?Domain
{ {
$RDAPService->registerDomains([$ldhName]); /** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $ldhName]);
if ($domain === null ||
$domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days >= 7) {
//TODO : Domain search rate limit here, before the RDAP request
$domain = $this->RDAPService->registerDomain($ldhName);
}
return $domain;
} }
} }

View File

@@ -29,6 +29,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
), ),
new Get( new Get(
uriTemplate: '/domains/{ldhName}', # Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type uriTemplate: '/domains/{ldhName}', # Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type
controller: DomainRefreshController::class,
normalizationContext: [ normalizationContext: [
'groups' => [ 'groups' => [
'domain:item', 'domain:item',
@@ -39,18 +40,8 @@ use Symfony\Component\Serializer\Attribute\Groups;
'nameserver-entity:entity', 'nameserver-entity:entity',
'tld:item' 'tld:item'
] ]
]
),
new Post(
uriTemplate: '/domains/{ldhName}',
status: 204,
controller: DomainRefreshController::class,
openapiContext: [
'summary' => 'Request an update of domain name data',
'description' => 'Triggers a refresh of the domain information.',
'requestBody' => false
], ],
deserialize: false read: false
) )
] ]
)] )]

View File

@@ -67,7 +67,7 @@ readonly class RDAPService
/** /**
* @throws Exception * @throws Exception
*/ */
private function registerDomain(string $fqdn): void public function registerDomain(string $fqdn): Domain
{ {
$idnDomain = idn_to_ascii($fqdn); $idnDomain = idn_to_ascii($fqdn);
$tld = $this->getTld($idnDomain); $tld = $this->getTld($idnDomain);
@@ -178,6 +178,7 @@ readonly class RDAPService
$this->em->persist($domain); $this->em->persist($domain);
$this->em->flush(); $this->em->flush();
return $domain;
} }