<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\UserRepository;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
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(repositoryClass: UserRepository::class)]
#[ApiResource(
iri: 'User',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'user:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['user:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'brokers.broker' => 'exact',
'insureds.insured' => 'exact',
'insurers.insurer' => 'exact',
// 'channels.channel' => 'exact',
'roles' => 'partial',
'email' => 'partial',
'name' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'email',
'createdAt',
])]
#[ApiFilter(PropertyFilter::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ApiProperty(iri: 'https://schema.org/identifier')]
#[Groups([
'user:collection:get',
'user:item:get',
])]
private ?UuidInterface $id = null;
/**
* The name of the item.
*
* @see https://schema.org/name
*/
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\Type('string')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post'
])]
private ?string $name = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[ApiProperty(iri: 'https://schema.org/email')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private $email;
#[ORM\Column(type: 'json')]
#[ApiProperty()]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private array $roles = [];
#[ORM\Column(type: 'string')]
private $password;
#[Groups([
'user:collection:post',
'user:item:put',
])]
protected $plainPassword;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $confirmationToken;
// #[ORM\OneToMany(mappedBy: 'user', targetEntity: BrokerUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// #[Groups([
// 'user:collection:post',
// 'user:item:get',
// 'user:item:put',
// ])]
// #[MaxDepth(1)]
// private $brokers;
// #[ORM\OneToMany(mappedBy: 'user', targetEntity: InsuredUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// #[Groups([
// 'user:collection:post',
// 'user:item:get',
// 'user:item:put',
// ])]
// #[MaxDepth(1)]
// private $insureds;
// #[ORM\OneToMany(mappedBy: 'user', targetEntity: InsurerUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// #[Groups([
// 'user:collection:post',
// 'user:item:get',
// 'user:item:put',
// ])]
// #[MaxDepth(1)]
// private $insurers;
// #[ORM\OneToMany(mappedBy: 'user', targetEntity: ChannelUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
// #[Groups([
// 'user:collection:post',
// 'user:item:get',
// 'user:item:put',
// ])]
// #[MaxDepth(1)]
// private $channels;
#[ApiProperty(iri: 'https://schema.org/image')]
#[ORM\ManyToOne(targetEntity: Media::class, cascade: ["persist", "remove"])]
#[ORM\JoinColumn(nullable: true)]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $avatar;
#[ORM\Column(type: 'string', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/desk')]
#[Assert\Type('string')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private ?string $desk = null;
#[ORM\OneToMany(targetEntity: 'App\Entity\Contract', mappedBy: 'broker')]
#[ApiProperty(iri: 'https://schema.org/Contract')]
#[Groups(['user:collection:get', 'user:collection:post', 'user:item:get', 'user:item:put'])]
#[MaxDepth(1)]
private ?Collection $contracts = null;
#[ORM\OneToMany(targetEntity: 'App\Entity\Contract', mappedBy: 'operator')]
#[ApiProperty(iri: 'https://schema.org/Contract')]
#[Groups(['user:collection:get', 'user:collection:post', 'user:item:get', 'user:item:put'])]
#[MaxDepth(1)]
private ?Collection $operations = null;
// #[ORM\OneToMany(targetEntity: 'App\Entity\Lifting', mappedBy: 'operator')]
// #[ApiProperty(iri: 'https://schema.org/Lifting')]
// #[Groups(['user:collection:get', 'user:collection:post', 'user:item:get', 'user:item:put'])]
// #[MaxDepth(1)]
// private ?Collection $liftings = null;
public function __construct()
{
$this->id = Uuid::uuid4();
// $this->brokers = new ArrayCollection();
// $this->insureds = new ArrayCollection();
// $this->insurers = new ArrayCollection();
// $this->channels = new ArrayCollection();
$this->contracts = new ArrayCollection();
$this->operations = new ArrayCollection();
// $this->liftings = 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 getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
// /**
// * @return Collection|BrokerUser[]
// */
// public function getBrokers(): Collection
// {
// return $this->brokers;
// }
// public function addBroker(BrokerUser $broker): self
// {
// if (!$this->brokers->contains($broker)) {
// $this->brokers[] = $broker;
// $broker->setUser($this);
// }
// return $this;
// }
// public function removeBroker(BrokerUser $broker): self
// {
// if ($this->brokers->removeElement($broker)) {
// // set the owning side to null (unless already changed)
// if ($broker->getUser() === $this) {
// $broker->setUser(null);
// }
// }
// return $this;
// }
// /**
// * @return Collection|InsuredUser[]
// */
// public function getInsureds(): Collection
// {
// return $this->insureds;
// }
// public function addInsured(InsuredUser $insured): self
// {
// if (!$this->insureds->contains($insured)) {
// $this->insureds[] = $insured;
// $insured->setUser($this);
// }
// return $this;
// }
// public function removeInsured(InsuredUser $insured): self
// {
// if ($this->insureds->removeElement($insured)) {
// // set the owning side to null (unless already changed)
// if ($insured->getUser() === $this) {
// $insured->setUser(null);
// }
// }
// return $this;
// }
// /**
// * @return Collection|InsurerUser[]
// */
// public function getInsurers(): Collection
// {
// return $this->insurers;
// }
// public function addInsurer(InsurerUser $insurer): self
// {
// if (!$this->insurers->contains($insurer)) {
// $this->insurers[] = $insurer;
// $insurer->setUser($this);
// }
// return $this;
// }
// public function removeInsurer(InsurerUser $insurer): self
// {
// if ($this->insurers->removeElement($insurer)) {
// // set the owning side to null (unless already changed)
// if ($insurer->getUser() === $this) {
// $insurer->setUser(null);
// }
// }
// return $this;
// }
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
// /**
// * @return Collection|ChannelUser[]
// */
// public function getChannels(): Collection
// {
// return $this->channels;
// }
// public function addChannel(ChannelUser $channel): self
// {
// if (!$this->channels->contains($channel)) {
// $this->channels[] = $channel;
// $channel->setUser($this);
// }
// return $this;
// }
// public function removeChannel(ChannelUser $channel): self
// {
// if ($this->channels->removeElement($channel)) {
// // set the owning side to null (unless already changed)
// if ($channel->getUser() === $this) {
// $channel->setUser(null);
// }
// }
// return $this;
// }
public function setAvatar(?Media $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getAvatar(): ?Media
{
return $this->avatar;
}
public function setDesk(?string $desk): void
{
$this->desk = $desk;
}
public function getDesk(): ?string
{
return $this->desk;
}
public function getContracts(): Collection
{
return $this->contracts;
}
public function addContract(Contract $contract): self
{
if (!$this->contracts->contains($contract)) {
$this->contracts[] = $contract;
$contract->setBroker($this);
}
return $this;
}
public function removeContract(Contract $contract): self
{
if ($this->contracts->removeElement($contract)) {
if ($contract->getBroker() === $this) {
$contract->setBroker(null);
}
}
return $this;
}
public function getOperations(): Collection
{
return $this->operations;
}
public function addOperation(Contract $operation): self
{
if (!$this->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->setOperator($this);
}
return $this;
}
public function removeOperation(Contract $operation): self
{
if ($this->operations->removeElement($operation)) {
if ($operation->getOperator() === $this) {
$operation->setOperator(null);
}
}
return $this;
}
// public function getLiftings(): Collection
// {
// return $this->liftings;
// }
// public function addLifting(Lifting $lifting): self
// {
// if (!$this->liftings->contains($lifting)) {
// $this->liftings[] = $lifting;
// $lifting->setOperator($this);
// }
// return $this;
// }
// public function removeLifting(Lifting $lifting): self
// {
// if ($this->liftings->removeElement($lifting)) {
// if ($lifting->getOperator() === $this) {
// $lifting->setOperator(null);
// }
// }
// return $this;
// }
}