fix: update table scheme

This commit is contained in:
Maël Gangloff 2024-07-12 13:16:40 +02:00
parent 975199f328
commit a7c6dfcd13
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
5 changed files with 203 additions and 7 deletions

View File

@ -10,7 +10,7 @@ use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240711233813 extends AbstractMigration
final class Version20240712105119 extends AbstractMigration
{
public function getDescription(): string
{
@ -34,12 +34,12 @@ final class Version20240711233813 extends AbstractMigration
, PRIMARY KEY(domain_id, entity_id), CONSTRAINT FK_614B48A1115F0EE5 FOREIGN KEY (domain_id) REFERENCES domain (handle) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_614B48A181257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_614B48A1115F0EE5 ON domain_entity (domain_id)');
$this->addSql('CREATE INDEX IDX_614B48A181257D5D ON domain_entity (entity_id)');
$this->addSql('CREATE TABLE domain_event ("action" VARCHAR(255) NOT NULL, domain_id VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, PRIMARY KEY("action", domain_id), CONSTRAINT FK_E8D52271115F0EE5 FOREIGN KEY (domain_id) REFERENCES domain (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE TABLE domain_event (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, domain_id VARCHAR(255) NOT NULL, "action" VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_E8D52271115F0EE5 FOREIGN KEY (domain_id) REFERENCES domain (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_E8D52271115F0EE5 ON domain_event (domain_id)');
$this->addSql('CREATE TABLE entity (handle VARCHAR(255) NOT NULL, PRIMARY KEY(handle))');
$this->addSql('CREATE TABLE entity_event ("action" VARCHAR(255) NOT NULL, entity_id VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, PRIMARY KEY("action", entity_id), CONSTRAINT FK_975A3F5E81257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE TABLE entity_event (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, entity_id VARCHAR(255) NOT NULL, "action" VARCHAR(255) NOT NULL, date DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_975A3F5E81257D5D FOREIGN KEY (entity_id) REFERENCES entity (handle) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_975A3F5E81257D5D ON entity_event (entity_id)');
$this->addSql('CREATE TABLE nameserver (handle VARCHAR(255) NOT NULL, ldh_name VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array)
, PRIMARY KEY(handle))');

View File

@ -0,0 +1,189 @@
<?php
namespace App\Controller;
use App\Config\DomainRole;
use App\Config\DomainStatus;
use App\Config\EventAction;
use App\Entity\Domain;
use App\Entity\DomainEntity;
use App\Entity\DomainEvent;
use App\Entity\Entity;
use App\Entity\EntityEvent;
use App\Entity\Nameserver;
use App\Entity\NameserverEntity;
use App\Repository\DomainEntityRepository;
use App\Repository\DomainEventRepository;
use App\Repository\DomainRepository;
use App\Repository\EntityEventRepository;
use App\Repository\EntityRepository;
use App\Repository\NameserverEntityRepository;
use App\Repository\NameserverRepository;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Juanparati\RDAPLib\Exceptions\RDAPWrongRequest;
use Juanparati\RDAPLib\RDAPClient;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route(path: '/test', name: 'test')]
public function testRoute(EntityRepository $entityRepository,
DomainRepository $domainRepository,
DomainEventRepository $domainEventRepository,
NameserverRepository $nameserverRepository,
NameserverEntityRepository $nameserverEntityRepository,
EntityEventRepository $entityEventRepository,
DomainEntityRepository $domainEntityRepository,
EntityManagerInterface $em
): Response
{
$rdap = new RDAPClient(['domain' => 'https://rdap.nic.fr/domain/']);
try {
$res = $rdap->domainLookup('nic.fr', RDAPClient::ARRAY_OUTPUT);
} catch (RDAPWrongRequest $e) {
return new Response(null, Response::HTTP_BAD_REQUEST);
} catch (ClientExceptionInterface $e) {
return new Response(null, Response::HTTP_INTERNAL_SERVER_ERROR);
}
/**
* Création du domaine
*/
$domain = $em->getRepository(Domain::class)->findOneBy(["handle" => $res['handle']]);
if ($domain === null) $domain = new Domain();
$domain->setLdhName($res['ldhName'])
->setHandle($res['handle'])
->setStatus(array_map(fn($str): DomainStatus => DomainStatus::from($str), $res['status']))
->setWhoisStatus($res['whoisStatus']);
/**
* Hydratation des événements
*/
foreach ($res['events'] as $rdapEvent) {
$event = $domainEventRepository->findOneBy([
"action" => EventAction::from($rdapEvent["eventAction"]),
"date" => new DateTimeImmutable($rdapEvent["eventDate"]),
"domain" => $res['handle']
]);
if ($event === null) $event = new DomainEvent();
$domain->addEvent($event
->setAction(EventAction::from($rdapEvent['eventAction']))
->setDate(new DateTimeImmutable($rdapEvent['eventDate'])));
}
/**
* Hydratation des entités
*/
foreach ($res['entities'] as $rdapEntity) {
$entity = $entityRepository->findOneBy([
"handle" => $rdapEntity['handle']
]);
if ($entity === null) $entity = new Entity();
$entity->setHandle($rdapEntity['handle']);
foreach ($rdapEntity['events'] as $rdapEntityEvent) {
$event = $entityEventRepository->findOneBy([
"action" => EventAction::from($rdapEntityEvent["eventAction"]),
"date" => new DateTimeImmutable($rdapEntityEvent["eventDate"]),
"entity" => $entity
]);
if ($event !== null) continue;
$entity->addEvent(
(new EntityEvent())
->setEntity($entity)
->setAction(EventAction::from($rdapEntityEvent['eventAction']))
->setDate(new DateTimeImmutable($rdapEntityEvent['eventDate'])));
}
$domainEntity = $domainEntityRepository->findOneBy([
"domain" => $domain,
"entity" => $entity
]);
if ($domainEntity === null) $domainEntity = new DomainEntity();
$domain->addDomainEntity($domainEntity
->setDomain($domain)
->setEntity($entity)
->setRoles(array_map(fn($str): DomainRole => DomainRole::from($str), $rdapEntity['roles'])));
}
$em->persist($domain);
foreach ($res['nameservers'] as $rdapNameserver) {
$nameserver = $nameserverRepository->findOneBy([
"handle" => $rdapNameserver['handle']
]);
if ($nameserver === null) $nameserver = new Nameserver();
$nameserver
->setHandle($rdapNameserver['handle'])
->setLdhName($rdapNameserver['ldhName'])
->setStatus(array_map(fn($str): DomainStatus => DomainStatus::from($str), $rdapNameserver['status']));
foreach ($rdapNameserver['entities'] as $rdapEntity) {
$entity = $entityRepository->findOneBy([
"handle" => $rdapEntity['handle']
]);
if ($entity === null) $entity = new Entity();
$entity->setHandle($rdapEntity['handle']);
foreach ($rdapEntity['events'] as $rdapEntityEvent) {
$event = $entityEventRepository->findOneBy([
"action" => EventAction::from($rdapEntityEvent["eventAction"]),
"date" => new DateTimeImmutable($rdapEntityEvent["eventDate"]),
"entity" => $entity
]);
if ($event !== null) continue;
$entity->addEvent(
(new EntityEvent())
->setEntity($entity)
->setAction(EventAction::from($rdapEntityEvent['eventAction']))
->setDate(new DateTimeImmutable($rdapEntityEvent['eventDate'])));
}
$nameserverEntity = $nameserverEntityRepository->findOneBy([
"nameserver" => $nameserver,
"entity" => $entity
]);
if ($nameserverEntity === null) $nameserverEntity = new NameserverEntity();
$nameserver->addNameserverEntity($nameserverEntity
->setNameserver($nameserver)
->setStatus(array_map(fn($str): DomainStatus => DomainStatus::from($str), $rdapNameserver['status']))
->setEntity($entity)
->setRoles(array_map(fn($str): DomainRole => DomainRole::from($str), $rdapEntity['roles'])));
}
$domain->addNameserver($nameserver);
}
$em->persist($domain);
$em->flush();
return new Response(null, Response::HTTP_OK);
}
}

View File

@ -8,7 +8,6 @@ use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: DomainEventRepository::class)]
class DomainEvent extends Event
{
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Domain::class, cascade: ['persist'], inversedBy: 'events')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Domain $domain = null;

View File

@ -9,7 +9,6 @@ use Doctrine\ORM\Mapping as ORM;
class EntityEvent extends Event
{
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Entity::class, inversedBy: 'events')]
#[ORM\JoinColumn(referencedColumnName: 'handle', nullable: false)]
private ?Entity $entity = null;

View File

@ -10,6 +10,10 @@ use Doctrine\ORM\Mapping as ORM;
class Event
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(enumType: EventAction::class)]
private ?EventAction $action = null;
@ -17,6 +21,11 @@ class Event
private ?DateTimeImmutable $date = null;
public function getId(): ?int
{
return $this->id;
}
public function getAction(): ?EventAction
{
return $this->action;