src/Entity/Adherent/Annuaire/MagasinContact.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Adherent\Annuaire;
  3. use App\Entity\Adherent\Magasin;
  4. use App\Entity\ContactThemeCanal;
  5. use App\Repository\Adherent\Annuaire\MagasinContactRepository;
  6. use App\Service\PhoneNumberService;
  7. use DateTimeImmutable;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use JetBrains\PhpStorm\NoReturn;
  12. use phpDocumentor\Reflection\Types\Integer;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  16. #[ORM\Entity(repositoryClassMagasinContactRepository::class)]
  17. #[ORM\UniqueConstraint(name'unique_contact_qualif_value'fields: ['magasin''value''qualifier'])]
  18. #[ORM\HasLifecycleCallbacks]
  19. class MagasinContact
  20. {
  21.     #[ORM\Id]
  22.     #[ORM\GeneratedValue]
  23.     #[ORM\Column(type'integer')]
  24.     #[Groups(groups: ['contact_history'])]
  25.     private int $id;
  26.     #[ORM\ManyToOne(targetEntityMagasin::class, inversedBy'magasinContacts')]
  27.     #[ORM\JoinColumn(name'magasin'referencedColumnName'k_adherent'nullablefalse)]
  28.     #[Groups(groups: ['contact_history'])]
  29.     private ?Magasin $magasin;
  30.     #[ORM\Column(type'datetime_immutable')]
  31.     #[Groups(groups: ['contact_history'])]
  32.     private DateTimeImmutable $createdAt;
  33.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  34.     #[Groups(groups: ['contact_history'])]
  35.     private ?DateTimeImmutable $updatedAt null;
  36.     #[ORM\Column(length255)]
  37.     #[Groups(groups: ['contact_history'])]
  38.     private ?string $value null;
  39.     #[ORM\Column(length255nullabletrue)]
  40.     #[Groups(groups: ['contact_history'])]
  41.     private ?string $name null;
  42.     #[ORM\ManyToOne(cascade: ['persist'], inversedBy'magasinContacts')]
  43.     #[ORM\JoinColumn(nullablefalse)]
  44.     #[Groups(groups: ['contact_history'])]
  45.     private ?MagasinContactQualifier $qualifier null;
  46.     #[ORM\ManyToMany(targetEntityContactThemeCanal::class, inversedBy'magasinContacts')]
  47.     #[Groups(groups: ['contact_history'])]
  48.     private Collection $canaux;
  49.     #[ORM\Column(type'integer'length255nullabletrue)]
  50.     #[Groups(groups: ['contact_history'])]
  51.     private ?int $kProgress null;
  52.     #[ORM\Column(type'integer'length255nullabletrue)]
  53.     #[Groups(groups: ['contact_history'])]
  54.     private ?int $idAnnuaireProgress;
  55.     #[ORM\Column(type'boolean')]
  56.     #[Groups(groups: ['contact_history'])]
  57.     private bool $principal false;
  58.     private bool $existInProgress false;
  59.     public function __construct()
  60.     {
  61.         $this->createdAt = new DateTimeImmutable("now");
  62.         $this->canaux = new ArrayCollection();
  63.         $this->idAnnuaireProgress = -1;
  64.     }
  65.     #[Assert\Callback]
  66.      public function validate(ExecutionContextInterface $context$payload): void
  67.     {
  68.         $phoneNumberService = new PhoneNumberService();
  69.         if ($this->magasin->allowExistMagasinContact($this)) {
  70.             $context->buildViolation("Cette valeur existe déjà")->atPath('value')->addViolation();
  71.         }
  72.         // Il y a des canaux qui ne peuvent avoir qu'un seul contact rattaché. Ex : Un seul téléphone peut-être affiché sur la fiche OPC.
  73.         foreach ($this->canaux as $canal) {
  74.             if (!$canal->isMultiple()) {
  75.                 $contactsMag $this->magasin->getMagasinContacts();
  76.                 foreach ($contactsMag as $contact) {
  77.                     if ($contact !== $this && $contact->hasCanal($canal->getIdentifier())) {
  78.                         $context->buildViolation("Le canal est déjà utilisé par un autre contact et ne peux contenir plusieurs contacts.")->atPath('canaux')->addViolation();
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         if (!$this->qualifier->isMultiple() && $this->magasin->hasMagasinContact($this$this->qualifier)) {
  84.             $context->buildViolation("Ce qualifiant est déjà utilisé pour ce magasin et ne peux pas contenir plusieurs valeurs.")->atPath('qualifier')->addViolation();
  85.         }
  86.         if ($this->qualifier->getType() === "TEL") {
  87.             $cleanedNumber $phoneNumberService->validateAndFormatPhoneNumber($this->valuestrtoupper($this->magasin->getKPays()));
  88.             if (is_null($cleanedNumber)) {
  89.                 $context->buildViolation("Le numéro de téléphone renseigné n'est pas valide.")->atPath('value')->addViolation();
  90.             } else {
  91.                 $cleanedNumber preg_replace('/\s+/'''strtolower($cleanedNumber));
  92.                 $this->value strtolower($cleanedNumber);
  93.             }
  94.         } else {
  95.             $cleanedMail preg_replace('/\s+/'''strtolower($this->value));
  96.             $this->value strtolower($cleanedMail);
  97.             if (!filter_var($cleanedMailFILTER_VALIDATE_EMAIL)) {
  98.                 $context->buildViolation("Le mail renseigné n'est pas valide.")->atPath('value')->addViolation();
  99.             }
  100.         }
  101.     }
  102.     #[ORM\PreUpdate]
  103.     public function onPreUpdateEntity(): void
  104.     {
  105.         $this->updatedAt = new DateTimeImmutable("now");
  106.     }
  107.     public function getId(): ?int
  108.     {
  109.         return $this->id;
  110.     }
  111.     public function getMagasin(): ?Magasin
  112.     {
  113.         return $this->magasin;
  114.     }
  115.     public function setMagasin(?Magasin $magasin): static
  116.     {
  117.         $this->magasin $magasin;
  118.         return $this;
  119.     }
  120.     public function getValue(): ?string
  121.     {
  122.         return $this->value;
  123.     }
  124.     public function setValue(?string $value): static
  125.     {
  126.         $this->value $value;
  127.         return $this;
  128.     }
  129.     public function getCreatedAt(): DateTimeImmutable
  130.     {
  131.         return $this->createdAt;
  132.     }
  133.     public function setCreatedAt(DateTimeImmutable $createdAt): void
  134.     {
  135.         $this->createdAt $createdAt;
  136.     }
  137.     public function getUpdatedAt(): ?DateTimeImmutable
  138.     {
  139.         return $this->updatedAt;
  140.     }
  141.     public function setUpdatedAt(?DateTimeImmutable $updatedAt): void
  142.     {
  143.         $this->updatedAt $updatedAt;
  144.     }
  145.     public function getQualifier(): ?MagasinContactQualifier
  146.     {
  147.         return $this->qualifier;
  148.     }
  149.     public function setQualifier(?MagasinContactQualifier $qualifier): static
  150.     {
  151.         $this->qualifier $qualifier;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @return Collection<int, ContactThemeCanal>
  156.      */
  157.     public function getCanaux(): Collection
  158.     {
  159.         return $this->canaux;
  160.     }
  161.     public function getCanauxByTheme()
  162.     {
  163.         $data = [];
  164.         foreach ($this->canaux as $canal) {
  165.             $data[strtoupper($canal->getContactTheme()->getType())][] = $canal;
  166.         }
  167.         return $data;
  168.     }
  169.     public function addCanaux(ContactThemeCanal $canaux): static
  170.     {
  171.         if (!$this->canaux->contains($canaux)) {
  172.             $this->canaux->add($canaux);
  173.         }
  174.         return $this;
  175.     }
  176.     public function removeCanaux(ContactThemeCanal $canaux): static
  177.     {
  178.         $this->canaux->removeElement($canaux);
  179.         return $this;
  180.     }
  181.     public function hasCanal($canalIdentifier): bool
  182.     {
  183.         return array_reduce($this->canaux->toArray(), fn($carry$contactCanal) => $carry || ($contactCanal->getIdentifier() === $canalIdentifier), false);
  184.     }
  185.     public function getName(): ?string
  186.     {
  187.         return $this->name;
  188.     }
  189.     public function setName(string $name): MagasinContact
  190.     {
  191.         $this->name $name;
  192.         return $this;
  193.     }
  194.     public function getKProgress(): ?int
  195.     {
  196.         return $this->kProgress;
  197.     }
  198.     public function setKProgress(int|bool $kProgress): void
  199.     {
  200.         $this->kProgress $kProgress;
  201.     }
  202.     public function getExistInProgress()
  203.     {
  204.         return $this->existInProgress;
  205.     }
  206.     public function setExistInProgress(bool $exist): void
  207.     {
  208.         $this->existInProgress $exist;
  209.     }
  210.     public function isPrincipal(): bool
  211.     {
  212.         return $this->principal;
  213.     }
  214.     public function setPrincipal(bool $principal): void
  215.     {
  216.         $this->principal $principal;
  217.     }
  218.     public function getIdAnnuaireProgress(): ?int
  219.     {
  220.         return $this->idAnnuaireProgress;
  221.     }
  222.     public function setIdAnnuaireProgress(int|null $idAnnuaireProgress): void
  223.     {
  224.         $this->idAnnuaireProgress $idAnnuaireProgress;
  225.     }
  226.     public function toArray(): array
  227.     {
  228.         return [
  229.             'id' => $this->id,
  230.             'value' => $this->value,
  231.             'qualifier' => [
  232.                 'category' => $this->qualifier->getCategory(),
  233.                 'type' => $this->qualifier->getType(),
  234.                 'label' => $this->qualifier->getLabel()
  235.             ],
  236.             'principal' => $this->isPrincipal(),
  237.             'name' => $this->name
  238.         ];
  239.     }
  240. }