<?php
namespace App\Entity;
use App\Repository\CollaboratorRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CollaboratorRepository::class)]
#[ORM\UniqueConstraint(name: "association_table", columns: ['company_id', 'user_id'])]
#[UniqueEntity(fields: ['company', 'user'], message: 'This user is already in this company.', errorPath: 'user')]
class Collaborator
{
const ROLE_COLLABORATOR = "collaborator";
const ROLE_MANAGER = "manager";
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(cascade: ["persist"], inversedBy: 'collaborators')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:getCollaborators',
'read:company:collection:get'
])]
private ?User $user = null;
#[ORM\ManyToOne(cascade: ["persist"], inversedBy: 'collaborators')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me'
])]
private ?Company $company = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getCompanies',
'read:company:collection:getCollaborators'
])]
private ?string $position = null;
#[ORM\Column(nullable: true)]
private array $roles = [];
#[ORM\Column(nullable: true)]
private ?bool $owner = null;
public function __construct()
{
$this->roles = [self::ROLE_COLLABORATOR];
$this->owner = false;
}
public static function getRoleList(): array
{
return [
self::ROLE_COLLABORATOR => "Collaborateur",
self::ROLE_MANAGER => "Gérant",
];
}
public function __toString(): string
{
return $this->user->getShortname().' ('.$this->company->getName().')';
}
public static function getRolesList()
{
return [
self::ROLE_COLLABORATOR => 'Collaborateur',
self::ROLE_MANAGER => 'Responsable',
];
}
public function getRoleData(): array
{
if ($this->owner) {
return [
'label' => 'Propriétaire',
'class' => 'warning',
];
}
if (in_array(self::ROLE_MANAGER, $this->roles)) {
return [
'label' => 'Responsable',
'class' => 'primary',
];
}
return [
'label' => 'Collaborateur',
'class' => 'info',
];
}
public function isManager(): bool
{
return in_array(self::ROLE_MANAGER, $this->roles);
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getPosition(): ?string
{
return $this->position;
}
public function setPosition(?string $position): self
{
$this->position = $position;
return $this;
}
#[Groups([
'read:user:item:me',
'read:company:collection:getCollaborators',
'read:company:collection:get'
])]
public function getRoles(): array
{
$roles = $this->roles;
if ($this->owner && !in_array(self::ROLE_MANAGER, $roles)) {
$roles[] = self::ROLE_MANAGER;
}
return $roles;
}
public function setRoles(?array $roles): self
{
$this->roles = $roles;
return $this;
}
public function isOwner(): ?bool
{
return $this->owner;
}
public function setOwner(?bool $owner): static
{
$this->owner = $owner;
return $this;
}
}