416 lines
12 KiB
PHP
Raw Normal View History

2024-07-10 23:30:59 +02:00
<?php
namespace App\Entity;
2024-07-17 00:19:27 +02:00
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
2024-12-07 20:08:29 +01:00
use App\Config\EventAction;
2024-07-17 18:18:11 +02:00
use App\Controller\DomainRefreshController;
2024-07-10 23:30:59 +02:00
use App\Repository\DomainRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
2024-07-17 00:19:27 +02:00
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
2024-07-10 23:30:59 +02:00
#[ORM\Entity(repositoryClass: DomainRepository::class)]
2024-07-17 00:19:27 +02:00
#[ApiResource(
operations: [
2024-07-24 13:46:10 +02:00
/*
2024-07-17 14:00:46 +02:00
new GetCollection(
normalizationContext: [
'groups' => [
'domain:list'
]
]
),
2024-07-24 13:46:10 +02:00
*/
2024-07-17 00:19:27 +02:00
new Get(
2024-08-02 23:24:52 +02:00
uriTemplate: '/domains/{ldhName}', // Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type
controller: DomainRefreshController::class,
2024-07-17 14:00:46 +02:00
normalizationContext: [
'groups' => [
'domain:item',
'event:list',
'domain-entity:entity',
2024-07-18 03:03:46 +02:00
'nameserver-entity:nameserver',
'nameserver-entity:entity',
2024-08-02 23:24:52 +02:00
'tld:item',
],
2024-07-17 18:18:11 +02:00
],
read: false
2024-08-02 23:24:52 +02:00
),
2024-07-17 00:19:27 +02:00
]
)]
2024-07-10 23:30:59 +02:00
class Domain
{
2024-07-13 12:49:11 +02:00
#[ORM\Id]
2024-07-10 23:30:59 +02:00
#[ORM\Column(length: 255)]
2024-07-27 20:44:10 +02:00
#[Groups(['domain:item', 'domain:list', 'watchlist:item', 'watchlist:list'])]
2024-07-11 17:01:16 +02:00
private ?string $ldhName = null;
2024-07-10 23:30:59 +02:00
#[ORM\Column(length: 255, nullable: true)]
2024-07-18 13:40:49 +02:00
#[Groups(['domain:item', 'domain:list', 'watchlist:item'])]
2024-07-10 23:30:59 +02:00
private ?string $handle = null;
/**
2024-07-11 17:01:16 +02:00
* @var Collection<int, DomainEvent>
2024-07-10 23:30:59 +02:00
*/
2024-07-12 00:50:30 +02:00
#[ORM\OneToMany(targetEntity: DomainEvent::class, mappedBy: 'domain', cascade: ['persist'], orphanRemoval: true)]
2024-09-09 11:31:33 +02:00
#[Groups(['domain:item', 'domain:list'])]
2024-07-10 23:30:59 +02:00
private Collection $events;
/**
* @var Collection<int, DomainEntity>
*/
2024-07-12 00:50:30 +02:00
#[ORM\OneToMany(targetEntity: DomainEntity::class, mappedBy: 'domain', cascade: ['persist'], orphanRemoval: true)]
2024-07-17 00:19:27 +02:00
#[Groups(['domain:item'])]
#[SerializedName('entities')]
2024-07-10 23:30:59 +02:00
private Collection $domainEntities;
2024-07-23 14:30:21 +02:00
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: true)]
2024-09-09 11:31:33 +02:00
#[Groups(['domain:item', 'domain:list'])]
2024-07-11 13:15:04 +02:00
private array $status = [];
2024-07-11 22:20:20 +02:00
/**
2024-07-12 15:20:24 +02:00
* @var Collection<int, WatchList>
2024-07-11 22:20:20 +02:00
*/
2024-07-12 15:20:24 +02:00
#[ORM\ManyToMany(targetEntity: WatchList::class, mappedBy: 'domains', cascade: ['persist'])]
private Collection $watchLists;
2024-07-11 22:20:20 +02:00
2024-07-11 17:01:16 +02:00
/**
* @var Collection<int, Nameserver>
*/
2024-07-12 00:50:30 +02:00
#[ORM\ManyToMany(targetEntity: Nameserver::class, inversedBy: 'domains', cascade: ['persist'])]
2024-07-11 22:20:20 +02:00
#[ORM\JoinTable(name: 'domain_nameservers',
2024-07-13 15:52:52 +02:00
joinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'nameserver_ldh_name', referencedColumnName: 'ldh_name')]
2024-07-11 22:20:20 +02:00
)]
2024-07-17 00:19:27 +02:00
#[Groups(['domain:item'])]
2024-07-11 17:01:16 +02:00
private Collection $nameservers;
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
private ?\DateTimeImmutable $createdAt;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
2024-09-09 11:31:33 +02:00
#[Groups(['domain:item', 'domain:list'])]
private ?\DateTimeImmutable $updatedAt;
2024-07-19 18:59:21 +02:00
#[ORM\ManyToOne]
#[ORM\JoinColumn(referencedColumnName: 'tld', nullable: false)]
2024-09-09 11:31:33 +02:00
#[Groups(['domain:item', 'domain:list'])]
2024-07-19 18:59:21 +02:00
private ?Tld $tld = null;
2024-09-01 21:26:07 +02:00
#[ORM\Column(nullable: false)]
2024-09-09 11:31:33 +02:00
#[Groups(['domain:item', 'domain:list'])]
2024-09-01 21:26:07 +02:00
private ?bool $deleted;
2024-07-25 16:19:57 +02:00
/**
* @var Collection<int, DomainStatus>
*/
#[ORM\OneToMany(targetEntity: DomainStatus::class, mappedBy: 'domain', orphanRemoval: true)]
private Collection $domainStatuses;
2024-12-07 20:08:29 +01:00
private const IMPORTANT_EVENTS = [EventAction::Deletion->value, EventAction::Expiration->value];
private const IMPORTANT_STATUS = [
'redemption period',
'pending delete',
'pending create',
'pending renew',
'pending restore',
'pending transfer',
'pending update',
'add period',
'client hold',
'server hold',
];
2024-07-10 23:30:59 +02:00
public function __construct()
{
$this->events = new ArrayCollection();
$this->domainEntities = new ArrayCollection();
2024-07-12 15:20:24 +02:00
$this->watchLists = new ArrayCollection();
2024-07-11 17:01:16 +02:00
$this->nameservers = new ArrayCollection();
2024-08-02 23:24:52 +02:00
$this->createdAt = new \DateTimeImmutable('now');
$this->updatedAt = new \DateTimeImmutable('now');
2024-07-25 16:19:57 +02:00
$this->deleted = false;
$this->domainStatuses = new ArrayCollection();
2024-07-10 23:30:59 +02:00
}
2024-07-11 17:01:16 +02:00
public function getLdhName(): ?string
2024-07-10 23:30:59 +02:00
{
2024-07-11 17:01:16 +02:00
return $this->ldhName;
2024-07-10 23:30:59 +02:00
}
2024-07-11 17:01:16 +02:00
public function setLdhName(string $ldhName): static
2024-07-10 23:30:59 +02:00
{
$this->ldhName = strtolower($ldhName);
2024-07-10 23:30:59 +02:00
return $this;
}
public function getHandle(): ?string
{
return $this->handle;
}
public function setHandle(string $handle): static
{
$this->handle = $handle;
return $this;
}
/**
2024-07-11 17:01:16 +02:00
* @return Collection<int, DomainEvent>
2024-07-10 23:30:59 +02:00
*/
public function getEvents(): Collection
{
return $this->events;
}
2024-07-11 17:01:16 +02:00
public function addEvent(DomainEvent $event): static
2024-07-10 23:30:59 +02:00
{
if (!$this->events->contains($event)) {
$this->events->add($event);
$event->setDomain($this);
}
return $this;
}
2024-07-11 17:01:16 +02:00
public function removeEvent(DomainEvent $event): static
2024-07-10 23:30:59 +02:00
{
if ($this->events->removeElement($event)) {
// set the owning side to null (unless already changed)
if ($event->getDomain() === $this) {
$event->setDomain(null);
}
}
return $this;
}
/**
* @return Collection<int, DomainEntity>
*/
public function getDomainEntities(): Collection
{
return $this->domainEntities;
}
public function addDomainEntity(DomainEntity $domainEntity): static
{
if (!$this->domainEntities->contains($domainEntity)) {
$this->domainEntities->add($domainEntity);
$domainEntity->setDomain($this);
}
return $this;
}
public function removeDomainEntity(DomainEntity $domainEntity): static
{
if ($this->domainEntities->removeElement($domainEntity)) {
// set the owning side to null (unless already changed)
if ($domainEntity->getDomain() === $this) {
$domainEntity->setDomain(null);
}
}
return $this;
}
2024-07-11 13:15:04 +02:00
public function getStatus(): array
{
return $this->status;
}
public function setStatus(array $status): static
{
$this->status = $status;
return $this;
}
2024-07-11 17:01:16 +02:00
2024-07-11 22:20:20 +02:00
/**
2024-07-12 15:20:24 +02:00
* @return Collection<int, WatchList>
2024-07-11 22:20:20 +02:00
*/
2024-07-12 15:20:24 +02:00
public function getWatchLists(): Collection
2024-07-11 22:20:20 +02:00
{
2024-07-12 15:20:24 +02:00
return $this->watchLists;
2024-07-11 22:20:20 +02:00
}
2024-07-12 15:20:24 +02:00
public function addWatchList(WatchList $watchList): static
2024-07-11 22:20:20 +02:00
{
2024-07-12 15:20:24 +02:00
if (!$this->watchLists->contains($watchList)) {
$this->watchLists->add($watchList);
$watchList->addDomain($this);
2024-07-11 22:20:20 +02:00
}
return $this;
}
2024-07-12 15:20:24 +02:00
public function removeWatchList(WatchList $watchList): static
2024-07-11 22:20:20 +02:00
{
2024-07-12 15:20:24 +02:00
if ($this->watchLists->removeElement($watchList)) {
$watchList->removeDomain($this);
2024-07-11 22:20:20 +02:00
}
return $this;
}
2024-07-11 17:01:16 +02:00
/**
* @return Collection<int, Nameserver>
*/
public function getNameservers(): Collection
{
return $this->nameservers;
}
public function addNameserver(Nameserver $nameserver): static
{
if (!$this->nameservers->contains($nameserver)) {
$this->nameservers->add($nameserver);
}
return $this;
}
public function removeNameserver(Nameserver $nameserver): static
{
$this->nameservers->removeElement($nameserver);
return $this;
}
2024-08-02 23:24:52 +02:00
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updateTimestamps(): void
{
2024-08-02 23:24:52 +02:00
$this->setUpdatedAt(new \DateTimeImmutable('now'));
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}
}
2024-08-02 23:24:52 +02:00
private function setUpdatedAt(?\DateTimeImmutable $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
2024-08-02 23:24:52 +02:00
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
2024-08-02 23:24:52 +02:00
private function setCreatedAt(?\DateTimeImmutable $createdAt): void
{
$this->createdAt = $createdAt;
}
2024-07-19 18:59:21 +02:00
public function getTld(): ?Tld
{
return $this->tld;
}
public function setTld(?Tld $tld): static
{
$this->tld = $tld;
return $this;
}
2024-07-25 16:19:57 +02:00
public function getDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(?bool $deleted): static
{
$this->deleted = $deleted;
return $this;
}
2024-12-07 20:08:29 +01:00
/**
* Determines if a domain name needs special attention.
* These domain names are those whose last event was expiration or deletion.
*
* @throws \Exception
*/
private function isToBeWatchClosely(): bool
{
$status = $this->getStatus();
if ((!empty($status) && count(array_intersect($status, self::IMPORTANT_STATUS))) || $this->getDeleted()) {
return true;
}
/** @var DomainEvent[] $events */
$events = $this->getEvents()
->filter(fn (DomainEvent $e) => !$e->getDeleted() && $e->getDate() <= new \DateTimeImmutable('now'))
2024-12-07 20:08:29 +01:00
->toArray();
usort($events, fn (DomainEvent $e1, DomainEvent $e2) => $e2->getDate() <=> $e1->getDate());
return !empty($events) && in_array($events[0]->getAction(), self::IMPORTANT_EVENTS);
}
/**
* Returns true if one or more of these conditions are met:
* - It has been more than 7 days since the domain name was last updated
* - It has been more than 1 hour and the domain name has statuses that suggest it is not stable
* - It has been more than 1 day and the domain name is blocked in DNS
*
* @throws \Exception
*/
public function isToBeUpdated(bool $fromUser = true): bool
{
return $this->getUpdatedAt()
->diff(new \DateTimeImmutable())->days >= 7
|| (($fromUser || ($this->getUpdatedAt()
->diff(new \DateTimeImmutable())->h * 60 + $this->getUpdatedAt()
->diff(new \DateTimeImmutable())->i) >= 50)
&& $this->isToBeWatchClosely())
2024-12-07 20:08:29 +01:00
|| (count(array_intersect($this->getStatus(), ['auto renew period', 'client hold', 'server hold'])) > 0
&& $this->getUpdatedAt()->diff(new \DateTimeImmutable())->days >= 1);
}
/**
* @return Collection<int, DomainStatus>
*/
public function getDomainStatuses(): Collection
{
return $this->domainStatuses;
}
public function addDomainStatus(DomainStatus $domainStatus): static
{
if (!$this->domainStatuses->contains($domainStatus)) {
$this->domainStatuses->add($domainStatus);
$domainStatus->setDomain($this);
}
return $this;
}
public function removeDomainStatus(DomainStatus $domainStatus): static
{
if ($this->domainStatuses->removeElement($domainStatus)) {
// set the owning side to null (unless already changed)
if ($domainStatus->getDomain() === $this) {
$domainStatus->setDomain(null);
}
}
return $this;
}
2024-07-10 23:30:59 +02:00
}