diff --git a/migrations/Version20260403220210.php b/migrations/Version20260403220210.php new file mode 100644 index 0000000..545efb2 --- /dev/null +++ b/migrations/Version20260403220210.php @@ -0,0 +1,35 @@ +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'); + } +} diff --git a/src/Entity/Domain.php b/src/Entity/Domain.php new file mode 100644 index 0000000..f0d6f8b --- /dev/null +++ b/src/Entity/Domain.php @@ -0,0 +1,177 @@ +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'); + } +} diff --git a/tests/Entity/DomainTest.php b/tests/Entity/DomainTest.php new file mode 100644 index 0000000..c268e98 --- /dev/null +++ b/tests/Entity/DomainTest.php @@ -0,0 +1,97 @@ +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)); + } +}