feat: entité Domain liée à Customer avec migration

Entity Domain :
- customer (ManyToOne, CASCADE) : client propriétaire du domaine
- fqdn (unique) : nom de domaine complet, lowercase auto
- registrar : bureau d'enregistrement (OVH, Gandi, etc.)
- zoneCloudflare : statut zone Cloudflare (active, pending, etc.)
- zoneIdCloudflare : identifiant zone Cloudflare
- expiredAt : date d'expiration du domaine
- isGestion : domaine géré par SITECONSEIL
- isBilling : domaine facturé par SITECONSEIL
- createdAt / updatedAt : timestamps
- isExpired() : vérifie si le domaine est expiré
- isExpiringSoon(days) : vérifie si expiration dans les N jours

DomainTest (4 tests, 25 assertions) :
- testConstructor : valeurs par défaut, fqdn lowercase/trim
- testSetters : tous les setters/getters
- testIsExpired : null/passé/futur
- testIsExpiringSoon : null/15j (true pour 30j)/60j (false pour 30j)

Migration : CREATE TABLE domain avec FK customer ON DELETE CASCADE

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-04 00:02:30 +02:00
parent fe42f221a6
commit 817fad4150
3 changed files with 309 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?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 Version20260403220210 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('CREATE TABLE domain (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, fqdn VARCHAR(255) NOT NULL, registrar VARCHAR(255) DEFAULT NULL, zone_cloudflare VARCHAR(50) DEFAULT NULL, zone_id_cloudflare VARCHAR(100) DEFAULT NULL, expired_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, is_gestion BOOLEAN NOT NULL, is_billing BOOLEAN NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, customer_id INT NOT NULL, PRIMARY KEY (id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_A7A91E0BC1A19758 ON domain (fqdn)');
$this->addSql('CREATE INDEX IDX_A7A91E0B9395C3F3 ON domain (customer_id)');
$this->addSql('ALTER TABLE domain ADD CONSTRAINT FK_A7A91E0B9395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE NOT DEFERRABLE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE domain DROP CONSTRAINT FK_A7A91E0B9395C3F3');
$this->addSql('DROP TABLE domain');
}
}

177
src/Entity/Domain.php Normal file
View File

@@ -0,0 +1,177 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Domain
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Customer::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private Customer $customer;
#[ORM\Column(length: 255, unique: true)]
private string $fqdn;
#[ORM\Column(length: 255, nullable: true)]
private ?string $registrar = null;
#[ORM\Column(length: 50, nullable: true)]
private ?string $zoneCloudflare = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $zoneIdCloudflare = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $expiredAt = null;
#[ORM\Column]
private bool $isGestion = false;
#[ORM\Column]
private bool $isBilling = false;
#[ORM\Column]
private \DateTimeImmutable $createdAt;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $updatedAt = null;
public function __construct(Customer $customer, string $fqdn)
{
$this->customer = $customer;
$this->fqdn = strtolower(trim($fqdn));
$this->createdAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getCustomer(): Customer
{
return $this->customer;
}
public function getFqdn(): string
{
return $this->fqdn;
}
public function setFqdn(string $fqdn): static
{
$this->fqdn = strtolower(trim($fqdn));
return $this;
}
public function getRegistrar(): ?string
{
return $this->registrar;
}
public function setRegistrar(?string $registrar): static
{
$this->registrar = $registrar;
return $this;
}
public function getZoneCloudflare(): ?string
{
return $this->zoneCloudflare;
}
public function setZoneCloudflare(?string $zoneCloudflare): static
{
$this->zoneCloudflare = $zoneCloudflare;
return $this;
}
public function getZoneIdCloudflare(): ?string
{
return $this->zoneIdCloudflare;
}
public function setZoneIdCloudflare(?string $zoneIdCloudflare): static
{
$this->zoneIdCloudflare = $zoneIdCloudflare;
return $this;
}
public function getExpiredAt(): ?\DateTimeImmutable
{
return $this->expiredAt;
}
public function setExpiredAt(?\DateTimeImmutable $expiredAt): static
{
$this->expiredAt = $expiredAt;
return $this;
}
public function isGestion(): bool
{
return $this->isGestion;
}
public function setIsGestion(bool $isGestion): static
{
$this->isGestion = $isGestion;
return $this;
}
public function isBilling(): bool
{
return $this->isBilling;
}
public function setIsBilling(bool $isBilling): static
{
$this->isBilling = $isBilling;
return $this;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function isExpired(): bool
{
return null !== $this->expiredAt && $this->expiredAt < new \DateTimeImmutable();
}
public function isExpiringSoon(int $days = 30): bool
{
if (null === $this->expiredAt) {
return false;
}
return $this->expiredAt < (new \DateTimeImmutable())->modify('+'.$days.' days');
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Customer;
use App\Entity\Domain;
use App\Entity\User;
use PHPUnit\Framework\TestCase;
class DomainTest extends TestCase
{
private function createCustomer(): Customer
{
$user = new User();
$user->setEmail('c@t.com');
$user->setFirstName('C');
$user->setLastName('T');
$user->setPassword('h');
return new Customer($user);
}
public function testConstructor(): void
{
$customer = $this->createCustomer();
$domain = new Domain($customer, 'Example.COM ');
$this->assertNull($domain->getId());
$this->assertSame($customer, $domain->getCustomer());
$this->assertSame('example.com', $domain->getFqdn());
$this->assertNull($domain->getRegistrar());
$this->assertNull($domain->getZoneCloudflare());
$this->assertNull($domain->getZoneIdCloudflare());
$this->assertNull($domain->getExpiredAt());
$this->assertFalse($domain->isGestion());
$this->assertFalse($domain->isBilling());
$this->assertInstanceOf(\DateTimeImmutable::class, $domain->getCreatedAt());
$this->assertNull($domain->getUpdatedAt());
}
public function testSetters(): void
{
$domain = new Domain($this->createCustomer(), 'siteconseil.fr');
$domain->setFqdn(' ESY-WEB.DEV ');
$this->assertSame('esy-web.dev', $domain->getFqdn());
$domain->setRegistrar('OVH');
$this->assertSame('OVH', $domain->getRegistrar());
$domain->setZoneCloudflare('active');
$this->assertSame('active', $domain->getZoneCloudflare());
$domain->setZoneIdCloudflare('zone_abc123');
$this->assertSame('zone_abc123', $domain->getZoneIdCloudflare());
$expiry = new \DateTimeImmutable('2027-01-01');
$domain->setExpiredAt($expiry);
$this->assertSame($expiry, $domain->getExpiredAt());
$domain->setIsGestion(true);
$this->assertTrue($domain->isGestion());
$domain->setIsBilling(true);
$this->assertTrue($domain->isBilling());
$now = new \DateTimeImmutable();
$domain->setUpdatedAt($now);
$this->assertSame($now, $domain->getUpdatedAt());
}
public function testIsExpired(): void
{
$domain = new Domain($this->createCustomer(), 'test.fr');
$this->assertFalse($domain->isExpired());
$domain->setExpiredAt(new \DateTimeImmutable('-1 day'));
$this->assertTrue($domain->isExpired());
$domain->setExpiredAt(new \DateTimeImmutable('+1 year'));
$this->assertFalse($domain->isExpired());
}
public function testIsExpiringSoon(): void
{
$domain = new Domain($this->createCustomer(), 'test.fr');
$this->assertFalse($domain->isExpiringSoon());
$domain->setExpiredAt(new \DateTimeImmutable('+15 days'));
$this->assertTrue($domain->isExpiringSoon(30));
$domain->setExpiredAt(new \DateTimeImmutable('+60 days'));
$this->assertFalse($domain->isExpiringSoon(30));
}
}