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

149 lines
4.2 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;
2024-07-18 14:01:16 +02:00
use ApiPlatform\Metadata\Patch;
2024-07-18 13:40:49 +02:00
use ApiPlatform\Metadata\Post;
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;
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(
routeName: 'watchlist_get_all_mine', normalizationContext: ['groups' => 'watchlist:list'],
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'watchlist:item']
),
new Post(
routeName: 'watchlist_create', normalizationContext: ['groups' => 'watchlist:list'],
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'create'
2024-07-18 14:01:16 +02:00
),
new Patch(
normalizationContext: ['groups' => 'watchlist:item'],
denormalizationContext: ['groups' => 'watchlist:update']
),
new Delete()
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\Id]
#[ORM\Column(length: 36)]
2024-07-18 13:40:49 +02:00
#[Groups(['watchlist:item', 'watchlist:list'])]
2024-07-11 02:29:08 +02:00
private string $token;
2024-07-12 15:20:24 +02:00
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
2024-07-11 02:29:08 +02:00
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
/**
* @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')],
2024-07-13 12:49:11 +02:00
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name')])]
2024-07-18 14:01:16 +02:00
#[Groups(['watchlist:item', 'watchlist:create', 'watchlist:update'])]
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:item', 'watchlist:create', 'watchlist:update'])]
#[SerializedName("triggers")]
private Collection $watchListTriggers;
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();
2024-07-11 02:29:08 +02:00
}
public function getToken(): ?string
{
return $this->token;
}
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-11 02:29:08 +02:00
}