<?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\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
#[ORM\Entity]
#[ApiResource(
iri: 'Cargo',
itemOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST') or
is_granted('ROLE_BROKER') or
is_granted('ROLE_INTERN')
",
'normalization_context' => [
'groups' => 'cargo:item:get',
'enable_max_depth' => true
]
],
'put' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST') or
is_granted('ROLE_BROKER')
",
'normalization_context' => [
'groups' => 'cargo:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'cargo:item:put',
'enable_max_depth' => true
],
],
'delete' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST') or
is_granted('ROLE_BROKER')
",
],
],
collectionOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST') or
is_granted('ROLE_BROKER') or
is_granted('ROLE_INTERN')
",
'normalization_context' => [
'groups' => [
'cargo:collection:get',
'createdAt'
],
'enable_max_depth' => true
],
],
'post' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATOR') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST') or
is_granted('ROLE_BROKER')
",
'normalization_context' => [
'groups' => 'cargo:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'cargo:collection:post',
'enable_max_depth' => true
],
],
],
)]
#[ApiFilter(
SearchFilter::class,
properties: [
'name' => 'partial',
'cargoType' => 'exact',
'subtype' => 'exact',
'createdAt' => 'start'
],
)]
#[ApiFilter(
OrderFilter::class,
properties: [
'name',
'cargoType',
'subtype',
'createdAt'
],
)]
#[ApiFilter(PropertyFilter::class)]
class Cargo
{
use TimestampableEntity;
public const CARGO_TYPE_LIQUID = 'Liquid';
public const CARGO_TYPE_DRY = 'Dry';
public const CARGO_TYPES = [
self::CARGO_TYPE_LIQUID,
self::CARGO_TYPE_DRY,
];
public const CARGO_SUBTYPE_LIQUID = 'Bulk Cargo';
public const CARGO_SUBTYPE_DRY = 'Bagged Cargo';
public const CARGO_SUBTYPE_GENERAL = 'General Cargo';
public const CARGO_SUBTYPE_LIQUID_CPP = 'Liquid Cargo / CPP - Clean Petroleum Products';
public const CARGO_SUBTYPE_LIQUID_CHEMICAL = 'Liquid Cargo / Chemical';
public const CARGO_SUBTYPE_LIQUID_DPP = 'Liquid Cargo / DPP - Dirty Petroleum Products';
public const CARGO_SUBTYPES = [
self::CARGO_SUBTYPE_LIQUID,
self::CARGO_SUBTYPE_DRY,
self::CARGO_SUBTYPE_GENERAL,
self::CARGO_SUBTYPE_LIQUID_CPP,
self::CARGO_SUBTYPE_LIQUID_CHEMICAL,
self::CARGO_SUBTYPE_LIQUID_DPP,
];
#[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([
'cargo:collection:get',
'cargo:collection:post',
'cargo:item:get',
'cargo:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post',
'market_rumor:item:get',
'market_rumor:item:put',
'market_rumor:collection:get',
'market_rumor:collection:post'
])]
private ?string $name = null;
#[ORM\Column(type: 'string', nullable: false)]
#[Assert\NotNull]
#[Assert\Type('string')]
#[Assert\Choice(
choices: self::CARGO_TYPES,
message: 'The type is not valid.'
)]
#[ApiProperty()]
#[Groups([
'cargo:collection:get',
'cargo:collection:post',
'cargo:item:get',
'cargo:item:put'
])]
private ?string $cargoType = null;
#[ORM\Column(type: 'string', nullable: true)]
##[Assert\NotNull]
##[Assert\Type('string')]
#[Assert\Choice(
choices: self::CARGO_SUBTYPES,
message: 'The subtype is not valid.'
)]
#[ApiProperty()]
#[Groups([
'cargo:collection:get',
'cargo:collection:post',
'cargo:item:get',
'cargo:item:put'
])]
private ?string $subtype = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty()]
#[Groups([
// 'cargo:collection:get',
'cargo:collection:post',
'cargo:item:get',
'cargo:item:put'
])]
private ?string $observations = null;
#[ORM\OneToMany(
targetEntity: ContractCargo::class,
mappedBy: 'cargo'
)]
// #[ApiProperty()]
// #[Groups([
// 'cargo:collection:get',
// 'cargo:collection:post',
// 'cargo:item:get',
// 'cargo:item:put'
// ])]
// #[MaxDepth(1)]
private ?Collection $contractCargos = null;
#[ORM\OneToMany(
targetEntity: MarketRumorCargo::class,
mappedBy: 'cargo'
)]
// #[ApiProperty()]
// #[Groups([
// 'cargo:collection:get',
// 'cargo:collection:post',
// 'cargo:item:get',
// 'cargo:item:put'
// ])]
// #[MaxDepth(1)]
private ?Collection $marketRumorCargos = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->contractCargos = new ArrayCollection();
$this->marketRumorCargos = new ArrayCollection();
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setSubtype(?string $subtype): void
{
$this->subtype = $subtype;
}
public function getSubtype(): ?string
{
return $this->subtype;
}
public function setCargoType(?string $cargoType): void
{
$this->cargoType = $cargoType;
}
public function getCargoType(): ?string
{
return $this->cargoType;
}
public function setObservations(?string $observations): void
{
$this->observations = $observations;
}
public function getObservations(): ?string
{
return $this->observations;
}
public function addContractCargo(ContractCargo $contractCargo): void
{
$this->contractCargos[] = $contractCargo;
}
public function removeContractCargo(ContractCargo $contractCargo): void
{
$this->contractCargos->removeElement($contractCargo);
}
public function getContractCargos(): Collection
{
return $this->contractCargos;
}
public function addMarketRumorCargo(MarketRumorCargo $marketRumorCargo): void
{
$this->marketRumorCargos[] = $marketRumorCargo;
}
public function removeMarketRumorCargo(MarketRumorCargo $marketRumorCargo): void
{
$this->marketRumorCargos->removeElement($marketRumorCargo);
}
public function getMarketRumorCargos(): Collection
{
return $this->marketRumorCargos;
}
}