feat: add tld type property

This commit is contained in:
Maël Gangloff 2024-07-24 18:52:19 +02:00
parent 0dfc0cdd58
commit 89e967cadf
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
5 changed files with 111 additions and 14 deletions

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240724160636 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE tld ADD COLUMN type VARCHAR(10) DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__tld AS SELECT tld, contract_terminated, date_of_contract_signature, delegation_date, registry_operator, removal_date, specification13 FROM tld');
$this->addSql('DROP TABLE tld');
$this->addSql('CREATE TABLE tld (tld VARCHAR(63) NOT NULL, contract_terminated BOOLEAN DEFAULT NULL, date_of_contract_signature DATE DEFAULT NULL --(DC2Type:date_immutable)
, delegation_date DATE DEFAULT NULL --(DC2Type:date_immutable)
, registry_operator VARCHAR(255) DEFAULT NULL, removal_date DATE DEFAULT NULL --(DC2Type:date_immutable)
, specification13 BOOLEAN DEFAULT NULL, PRIMARY KEY(tld))');
$this->addSql('INSERT INTO tld (tld, contract_terminated, date_of_contract_signature, delegation_date, registry_operator, removal_date, specification13) SELECT tld, contract_terminated, date_of_contract_signature, delegation_date, registry_operator, removal_date, specification13 FROM __temp__tld');
$this->addSql('DROP TABLE __temp__tld');
}
}

13
src/Config/TldType.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Config;
enum TldType: string
{
case iTLD = 'iTLD';
case gTLD = "gTLD";
case sTLD = "sTLD";
case ccTLD = "ccTLD";
case tTLD = "tTLD";
}

View File

@ -5,6 +5,7 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Config\TldType;
use App\Repository\TldRepository;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
@ -63,6 +64,9 @@ class Tld
#[Groups(["tld:item"])]
private ?bool $specification13 = null;
#[ORM\Column(length: 10, nullable: true, enumType: TldType::class)]
private ?TldType $type = null;
public function __construct()
{
$this->rdapServers = new ArrayCollection();
@ -181,4 +185,16 @@ class Tld
return $this;
}
public function getType(): ?TldType
{
return $this->type;
}
public function setType(TldType $type): static
{
$this->type = $type;
return $this;
}
}

View File

@ -4,6 +4,7 @@ namespace App\MessageHandler;
use App\Message\UpdateRdapServers;
use App\Service\RDAPService;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
@ -33,10 +34,6 @@ final readonly class UpdateRdapServersHandler
$throws = [];
try {
$this->RDAPService->updateTldListIANA();
} catch (Throwable $throwable) {
$throws[] = $throwable;
}
try {
$this->RDAPService->updateGTldListICANN();
} catch (Throwable $throwable) {
$throws[] = $throwable;

View File

@ -4,6 +4,7 @@
namespace App\Service;
use App\Config\EventAction;
use App\Config\TldType;
use App\Entity\Domain;
use App\Entity\DomainEntity;
use App\Entity\DomainEvent;
@ -26,6 +27,7 @@ use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Exception;
use Symfony\Component\Intl\Countries;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
@ -36,6 +38,22 @@ use Throwable;
readonly class RDAPService
{
const ISO_TLD_EXCEPTION = ['ac', 'eu', 'uk', 'su', 'tp'];
const INFRA_TLD = ['arpa'];
const SPONSORED_TLD = ['edu', 'gov', 'int', 'mil'];
const TEST_TLD = [
'xn--kgbechtv',
'xn--hgbk6aj7f53bba',
'xn--0zwm56d',
'xn--g6w251d',
'xn--80akhbyknj4f',
'xn--11b5bs3a9aj6g',
'xn--jxalpdlp',
'xn--9t4b11yi5a',
'xn--deba0ad',
'xn--zckzah',
'xn--hlcj6aya9esc7a'
];
public function __construct(private HttpClientInterface $client,
private EntityRepository $entityRepository,
@ -288,7 +306,7 @@ readonly class RDAPService
if ($tld === "") continue;
$tldReference = $this->em->getReference(Tld::class, $tld);
foreach ($service[1] as $rdapServerUrl) {
$server = $this->rdapServerRepository->findOneBy(["tld" => $tldReference, "url" => $rdapServerUrl]); //ICI
$server = $this->rdapServerRepository->findOneBy(["tld" => $tldReference, "url" => $rdapServerUrl]);
if ($server === null) $server = new RdapServer();
$server->setTld($tldReference)->setUrl($rdapServerUrl)->updateTimestamps();
@ -316,16 +334,31 @@ readonly class RDAPService
)->getContent()
));
array_shift($tldList);
$storedTldList = array_map(fn($tld) => $tld->getTld(), $this->tldRepository->findAll());
foreach (array_diff($tldList, $storedTldList) as $tld) {
foreach ($tldList as $tld) {
if ($tld === "") continue;
$this->em->persist((new Tld())->setTld($tld));
$tldEntity = $this->tldRepository->findOneBy(['tld' => $tld]);
if ($tldEntity === null) $tldEntity = new Tld();
$tldEntity->setTld($tld)->setType($this->getTldType($tld));
if ($tldEntity->getRegistryOperator() === NULL) $tldEntity->setType(TldType::ccTLD);
$this->em->persist($tldEntity);
}
$this->em->flush();
}
private function getTldType(string $tld): ?TldType
{
if (Countries::exists(strtoupper($tld)) || in_array($tld, self::ISO_TLD_EXCEPTION)) return TldType::ccTLD;
if (in_array(strtolower($tld), self::INFRA_TLD)) return TldType::iTLD;
if (in_array(strtolower($tld), self::SPONSORED_TLD)) return TldType::sTLD;
if (in_array(strtolower($tld), self::TEST_TLD)) return TldType::tTLD;
return TldType::gTLD;
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
@ -333,6 +366,7 @@ readonly class RDAPService
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws Exception
* @throws ORMException
*/
public function updateGTldListICANN(): void
{
@ -342,14 +376,13 @@ readonly class RDAPService
foreach ($gTldList as $gTld) {
if ($gTld['gTLD'] === "") continue;
$gtTldEntity = $this->tldRepository->findOneBy(['tld' => $gTld['gTLD']]);
if ($gtTldEntity === null) $gtTldEntity = new Tld();
/** @var Tld $gtTldEntity */
$gtTldEntity = $this->em->getReference(Tld::class, $gTld['gTLD']);
$gtTldEntity
->setTld($gTld['gTLD'])
->setContractTerminated($gTld['contractTerminated'])
$gtTldEntity->setContractTerminated($gTld['contractTerminated'])
->setRegistryOperator($gTld['registryOperator'])
->setSpecification13($gTld['specification13']);
if ($gTld['removalDate'] !== null) $gtTldEntity->setRemovalDate(new DateTimeImmutable($gTld['removalDate']));
if ($gTld['delegationDate'] !== null) $gtTldEntity->setDelegationDate(new DateTimeImmutable($gTld['delegationDate']));
if ($gTld['dateOfContractSignature'] !== null) $gtTldEntity->setDateOfContractSignature(new DateTimeImmutable($gTld['dateOfContractSignature']));