Files
domain-watchdog/src/Entity/WatchList.php

267 lines
7.5 KiB
PHP
Raw Normal View History

2024-07-11 02:29:08 +02:00
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
2024-07-18 14:01:16 +02:00
use ApiPlatform\Metadata\Delete;
2024-07-18 13:40:49 +02:00
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
2024-08-15 03:04:31 +02:00
use ApiPlatform\Metadata\Put;
2024-07-12 15:20:24 +02:00
use App\Repository\WatchListRepository;
2024-07-11 02:29:08 +02:00
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
2024-08-16 23:23:51 +02:00
use Doctrine\DBAL\Types\Types;
2024-07-11 02:29:08 +02:00
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
2024-07-20 23:14:03 +02:00
use Symfony\Component\Serializer\Attribute\SerializedName;
2024-07-11 02:29:08 +02:00
use Symfony\Component\Uid\Uuid;
2024-07-12 15:20:24 +02:00
#[ORM\Entity(repositoryClass: WatchListRepository::class)]
#[ApiResource(
shortName: 'Watchlist',
2024-07-18 13:40:49 +02:00
operations: [
new GetCollection(
2024-07-27 18:53:07 +02:00
routeName: 'watchlist_get_all_mine',
normalizationContext: ['groups' => 'watchlist:list'],
2024-07-18 13:40:49 +02:00
name: 'get_all_mine',
),
2024-09-09 11:31:33 +02:00
new GetCollection(
uriTemplate: '/tracked',
routeName: 'watchlist_get_tracked_domains',
normalizationContext: ['groups' => [
'domain:list',
'tld:list',
'event:list',
]],
name: 'get_tracked_domains'
),
2024-07-18 13:40:49 +02:00
new Get(
normalizationContext: ['groups' => [
'watchlist:item',
'domain:item',
'event:list',
'domain-entity:entity',
'nameserver-entity:nameserver',
'nameserver-entity:entity',
'tld:item',
],
],
2024-08-04 15:49:38 +02:00
security: 'object.user == user'
2024-07-18 13:40:49 +02:00
),
2024-08-01 14:37:23 +02:00
new Get(
routeName: 'watchlist_calendar',
openapiContext: [
'responses' => [
'200' => [
'description' => 'Watchlist iCalendar',
'content' => [
'text/calendar' => [
'schema' => [
'type' => 'string',
2024-08-02 23:24:52 +02:00
'format' => 'text',
],
],
],
],
],
2024-08-01 14:37:23 +02:00
],
read: false,
deserialize: false,
serialize: false,
name: 'calendar'
),
2024-07-18 13:40:49 +02:00
new Post(
routeName: 'watchlist_create', normalizationContext: ['groups' => 'watchlist:list'],
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'create'
2024-07-18 14:01:16 +02:00
),
2024-08-15 03:04:31 +02:00
new Put(
routeName: 'watchlist_update',
2024-07-18 14:01:16 +02:00
normalizationContext: ['groups' => 'watchlist:item'],
2024-08-15 03:04:31 +02:00
denormalizationContext: ['groups' => ['watchlist:create', 'watchlist:token']],
security: 'object.user == user',
name: 'update'
2024-07-18 14:01:16 +02:00
),
2024-08-16 17:10:59 +02:00
new Delete(
security: 'object.user == user'
),
2024-07-18 13:40:49 +02:00
],
)]
2024-07-12 15:20:24 +02:00
class WatchList
2024-07-11 02:29:08 +02:00
{
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
public ?User $user = null;
2024-07-11 02:29:08 +02:00
#[ORM\Id]
2024-07-26 21:10:06 +02:00
#[ORM\Column(type: 'uuid')]
2024-08-15 03:04:31 +02:00
#[Groups(['watchlist:item', 'watchlist:list', 'watchlist:token'])]
2024-07-11 02:29:08 +02:00
private string $token;
/**
* @var Collection<int, Domain>
*/
2024-07-12 15:20:24 +02:00
#[ORM\ManyToMany(targetEntity: Domain::class, inversedBy: 'watchLists')]
#[ORM\JoinTable(name: 'watch_lists_domains',
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
2024-07-11 02:29:08 +02:00
private Collection $domains;
2024-07-20 23:14:03 +02:00
/**
* @var Collection<int, WatchListTrigger>
*/
#[ORM\OneToMany(targetEntity: WatchListTrigger::class, mappedBy: 'watchList', cascade: ['persist'], orphanRemoval: true)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
2024-08-02 23:24:52 +02:00
#[SerializedName('triggers')]
2024-07-20 23:14:03 +02:00
private Collection $watchListTriggers;
2024-07-30 21:34:48 +02:00
#[ORM\ManyToOne(inversedBy: 'watchLists')]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
2024-07-30 21:34:48 +02:00
private ?Connector $connector = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?string $name = null;
#[ORM\Column]
#[Groups(['watchlist:list', 'watchlist:item'])]
private ?\DateTimeImmutable $createdAt = null;
2024-08-16 23:23:51 +02:00
#[SerializedName('dsn')]
#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: true)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?array $webhookDsn = null;
2024-07-11 02:29:08 +02:00
public function __construct()
{
$this->token = Uuid::v4();
$this->domains = new ArrayCollection();
2024-07-20 23:14:03 +02:00
$this->watchListTriggers = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable('now');
2024-07-11 02:29:08 +02:00
}
public function getToken(): ?string
{
return $this->token;
}
2024-08-15 03:04:31 +02:00
public function setToken(string $token): void
{
$this->token = $token;
}
2024-07-11 02:29:08 +02:00
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
/**
* @return Collection<int, Domain>
*/
public function getDomains(): Collection
{
return $this->domains;
}
public function addDomain(Domain $domain): static
{
if (!$this->domains->contains($domain)) {
$this->domains->add($domain);
}
return $this;
}
public function removeDomain(Domain $domain): static
{
$this->domains->removeElement($domain);
return $this;
}
2024-07-20 23:14:03 +02:00
/**
* @return Collection<int, WatchListTrigger>
*/
public function getWatchListTriggers(): Collection
{
return $this->watchListTriggers;
}
public function addWatchListTrigger(WatchListTrigger $watchListTrigger): static
{
if (!$this->watchListTriggers->contains($watchListTrigger)) {
$this->watchListTriggers->add($watchListTrigger);
$watchListTrigger->setWatchList($this);
}
return $this;
}
public function removeWatchListTrigger(WatchListTrigger $watchListTrigger): static
{
if ($this->watchListTriggers->removeElement($watchListTrigger)) {
// set the owning side to null (unless already changed)
if ($watchListTrigger->getWatchList() === $this) {
$watchListTrigger->setWatchList(null);
}
}
return $this;
}
2024-07-30 21:34:48 +02:00
public function getConnector(): ?Connector
{
return $this->connector;
}
public function setConnector(?Connector $connector): static
{
$this->connector = $connector;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
2024-08-16 23:23:51 +02:00
public function getWebhookDsn(): ?array
{
return $this->webhookDsn;
}
public function setWebhookDsn(?array $webhookDsn): static
{
$this->webhookDsn = $webhookDsn;
return $this;
}
2024-07-11 02:29:08 +02:00
}