<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
#[ORM\Entity]
#[ApiResource(
iri: 'Checklist',
itemOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR')
",
'normalization_context' => [
'groups' => 'checklist:item:get',
'enable_max_depth' => true
]
],
'put' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR')
",
'normalization_context' => [
'groups' => 'checklist:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'checklist:item:put',
'enable_max_depth' => true
],
],
'delete' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR')
",
],
],
collectionOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR')
",
'normalization_context' => [
'groups' => [
'checklist:collection:get',
'createdAt'
],
'enable_max_depth' => true,
],
],
'post' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR')
",
'normalization_context' => [
'groups' => 'checklist:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'checklist:collection:post',
'enable_max_depth' => true
],
],
],
)]
#[ApiFilter(
SearchFilter::class,
properties: [
'checklistName' => 'partial',
'createdAt' => 'start'
],
)]
#[ApiFilter(
OrderFilter::class,
properties: [
'checklistName',
'createdAt'
],
)]
#[ApiFilter(PropertyFilter::class)]
class Checklist
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
private ?UuidInterface $id = null;
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty()]
#[Assert\NotBlank]
#[Assert\Type('string')]
#[Groups([
'checklist:collection:get',
'checklist:collection:post',
'checklist:item:get',
'checklist:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post'
])]
private ?string $checklistName = null;
#[ORM\OneToMany(
targetEntity: Task::class,
mappedBy: 'checklist',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[ORM\OrderBy(['taskIndex' => 'ASC'])]
#[Groups([
'checklist:collection:get',
'checklist:collection:post',
'checklist:item:get',
'checklist:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post'
])]
#[MaxDepth(1)]
private ?Collection $tasks = null;
#[ApiProperty()]
#[Groups(['checklist:collection:get'])]
private ?int $taskCount = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty()]
##[Assert\Type('string')]
#[Groups([
'checklist:collection:get',
'checklist:collection:post',
'checklist:item:get',
'checklist:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post'
])]
private ?string $notes = null;
#[ORM\OneToMany(
targetEntity: Contract::class,
mappedBy: 'checklist'
)]
private ?Collection $contracts = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->tasks = new ArrayCollection();
$this->contracts = new ArrayCollection();
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function setChecklistName(?string $checklistName): void
{
$this->checklistName = $checklistName;
}
public function getChecklistName(): ?string
{
return $this->checklistName;
}
public function setNotes(?string $notes): void
{
$this->notes = $notes;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function addTask(Task $task): void
{
$task->setChecklist($this);
$this->tasks[] = $task;
}
public function removeTask(Task $task): void
{
$this->tasks->removeElement($task);
}
public function getTasks(): Collection
{
return $this->tasks;
}
public function getTaskCount(): ?int
{
return $this->tasks->count();
}
public function getContracts(): Collection
{
return $this->contracts;
}
public function addContract(Contract $contract): self
{
if (!$this->contracts->contains($contract)) {
$this->contracts[] = $contract;
$contract->setChecklist($this);
}
return $this;
}
public function removeContract(Contract $contract): self
{
if ($this->contracts->removeElement($contract)) {
if ($contract->getChecklist() === $this) {
$contract->setChecklist(null);
}
}
return $this;
}
}