src/Entity/Collaborator.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CollaboratorRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Symfony\Component\Uid\Uuid;
  7. use Doctrine\ORM\Mapping as ORM;
  8. #[ORM\Entity(repositoryClassCollaboratorRepository::class)]
  9. class Collaborator
  10. {
  11.     #[ORM\IdORM\GeneratedValueORM\Column]
  12.     private ?int $id null;
  13.     #[ORM\Column(length100)]
  14.     private string $firstName;
  15.     #[ORM\Column(length100)]
  16.     private string $lastName;
  17.     #[ORM\Column(length255)]
  18.     private string $email;
  19.     #[ORM\Column(length:255)]
  20.     private string $token;
  21.     // Stocke un tableau de couples [email => uuid]
  22.     #[ORM\Column(type'json')]
  23.     private array $clientEmails = [];
  24.     #[ORM\ManyToOne(targetEntityAccountingFirm::class, inversedBy:'collaborators')]
  25.     private AccountingFirm $accountingFirm;
  26.     #[ORM\ManyToMany(targetEntitySatisfactionClientSurvey::class, mappedBy'collaborators')]
  27.     private Collection $surveys;
  28.     private array $stats;
  29.     public function __construct()
  30.     {
  31.         $this->surveys = new ArrayCollection();
  32.         $this->accountingFirm = new AccountingFirm();
  33.     }
  34.     public function getId(): ?int
  35.     {
  36.         return $this->id;
  37.     }
  38.     public function getFirstName(): string
  39.     {
  40.         return $this->firstName;
  41.     }
  42.     public function setFirstName(string $firstName): self
  43.     {
  44.         $this->firstName $firstName;
  45.         return $this;
  46.     }
  47.     public function getLastName(): string
  48.     {
  49.         return $this->lastName;
  50.     }
  51.     public function getFullName(): string
  52.     {
  53.         return $this->firstName ' ' $this->lastName;
  54.     }
  55.     public function setLastName(string $lastName): self
  56.     {
  57.         $this->lastName $lastName;
  58.         return $this;
  59.     }
  60.     public function getEmail(): string
  61.     {
  62.         return $this->email;
  63.     }
  64.     public function setEmail(string $email): self
  65.     {
  66.         $this->email $email;
  67.         return $this;
  68.     }
  69.     public function getClientEmails(): array
  70.     {
  71.         return $this->clientEmails;
  72.     }
  73.     public function setClientEmails(array $clientEmails): self
  74.     {
  75.         $this->clientEmails $clientEmails;
  76.         return $this;
  77.     }
  78.     /**
  79.      * Définit les clients à partir d'une chaîne d'emails séparés par virgule.
  80.      */
  81.     public function setClientEmailsFromRawString(string $rawEmails): self
  82.     {
  83.         $emails array_filter(array_map('trim'explode(','$rawEmails)));
  84.         return $this->setClientEmailsFromArray($emails);
  85.     }
  86.     public function addClientEmailIfNotExists(string $email): self
  87.     {
  88.         if (!$this->hasClientEmail($email)) {
  89.             $this->clientEmails[] = [
  90.                 'email' => $email,
  91.                 'uuid' => Uuid::v4()->toRfc4122()
  92.             ];
  93.         }
  94.         return $this;
  95.     }
  96.     /**
  97.      * Définit les clients à partir d’un tableau d’emails simples (et génère les UUID).
  98.      */
  99.     public function setClientEmailsFromArray(array $emails): self
  100.     {
  101.         $this->clientEmails array_map(function (string $email) {
  102.             return [
  103.                 'email' => $email,
  104.                 'uuid' => Uuid::v4()->toRfc4122()
  105.             ];
  106.         }, array_filter(array_map('trim'$emails)));
  107.         return $this;
  108.     }
  109.     /**
  110.      * Retourne le token UUID associé à une adresse email, ou null si non trouvé.
  111.      */
  112.     public function getTokenForClientEmail(string $email): ?string
  113.     {
  114.         foreach ($this->clientEmails as $entry) {
  115.             if (isset($entry['email']) && $entry['email'] === $email) {
  116.                 return $entry['uuid'] ?? null;
  117.             }
  118.         }
  119.         return null;
  120.     }
  121.     /**
  122.      * @return Collection<int, SatisfactionClientSurvey>
  123.      */
  124.     public function getSurveys(): Collection
  125.     {
  126.         return $this->surveys;
  127.     }
  128.     public function addSurvey(SatisfactionClientSurvey $survey): self
  129.     {
  130.         if (!$this->surveys->contains($survey)) {
  131.             $this->surveys->add($survey);
  132.             $survey->addCollaborator($this);
  133.         }
  134.         return $this;
  135.     }
  136.     public function removeSurvey(SatisfactionClientSurvey $survey): self
  137.     {
  138.         if ($this->surveys->removeElement($survey)) {
  139.             $survey->removeCollaborator($this);
  140.         }
  141.         return $this;
  142.     }
  143.     public function getAccountingFirm(): AccountingFirm
  144.     {
  145.         return $this->accountingFirm;
  146.     }
  147.     public function setAccountingFirm(AccountingFirm $accountingFirm): self
  148.     {
  149.         $this->accountingFirm $accountingFirm;
  150.         return $this;
  151.     }
  152.     public function getStatsByCollaborator(): array
  153.     {
  154.         return $this->stats;
  155.     }
  156.     public function setStatsByCollaborator(array $statsByCollaborator): self
  157.     {
  158.         $this->stats $statsByCollaborator;
  159.         return $this;
  160.     }
  161.     public function getToken(): ?string
  162.     {
  163.         return $this->token;
  164.     }
  165.     public function genToken(): self
  166.     {
  167.         $this->token Uuid::v4()->toRfc4122();
  168.         return $this;
  169.     }
  170.     public function hasClientEmail(string $email): bool
  171.     {
  172.         foreach ($this->clientEmails as $entry) {
  173.             if (isset($entry['email']) && strtolower($entry['email']) === strtolower($email)) {
  174.                 return true;
  175.             }
  176.         }
  177.         return false;
  178.     }
  179. }