Vincent GUFFON 4 年之前
父節點
當前提交
b53acda395

+ 2 - 1
src/Entity/Organization/OrganizationAddressPostal.php

@@ -9,8 +9,8 @@ use App\Entity\Core\AddressPostal;
 use App\Repository\Organization\OrganizationAddressPostalRepository;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
-
 use Symfony\Component\Serializer\Annotation\Groups;
+use App\Validator\Organization as OpentalentAssert;
 
 #[ApiResource(
     collectionOperations: [
@@ -32,6 +32,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
 )]
 #[ORM\Entity(repositoryClass: OrganizationAddressPostalRepository::class)]
 #[OrganizationDefaultValue(fieldName: "organization")]
+#[OpentalentAssert\OrganizationAddressPostal]
 class OrganizationAddressPostal
 {
     #[ORM\Id]

+ 18 - 0
src/Repository/Organization/OrganizationAddressPostalRepository.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace App\Repository\Organization;
 
+use App\Entity\Organization\Organization;
 use App\Entity\Organization\OrganizationAddressPostal;
 use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
 use Doctrine\Persistence\ManagerRegistry;
@@ -19,4 +20,21 @@ class OrganizationAddressPostalRepository extends ServiceEntityRepository
     {
         parent::__construct($registry, OrganizationAddressPostal::class);
     }
+
+    /**
+     * Récupération des adresses postal d'une organization et d'un type précis
+     * @param String $type
+     * @param Organization $organization
+     * @return array|null
+     */
+    public function getByType(String $type, Organization $organization): array | null{
+        return $this->createQueryBuilder('organizationAddressPostal')
+            ->where('organizationAddressPostal.type = :type')
+            ->andWhere('organizationAddressPostal.organization = :organization')
+            ->setParameter('type', $type)
+            ->setParameter('organization', $organization)
+            ->getQuery()
+            ->getResult()
+            ;
+    }
 }

+ 17 - 0
src/Validator/Organization/OrganizationAddressPostal.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Validator\Organization;
+
+use Symfony\Component\Validator\Constraint;
+
+#[\Attribute]
+class OrganizationAddressPostal extends Constraint
+{
+    public $message = '{{ type }}_non_unique';
+
+    public function getTargets()
+    {
+        return self::CLASS_CONSTRAINT;
+    }
+}

+ 37 - 0
src/Validator/Organization/OrganizationAddressPostalValidator.php

@@ -0,0 +1,37 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Validator\Organization;
+
+use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
+use App\Repository\Organization\OrganizationAddressPostalRepository;
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+use App\Entity\Organization\OrganizationAddressPostal;
+
+/**
+ * Classe control qu'une seul et même type d'adresse est autorisé pour les adresses d'organization (à part les "adresses autres")
+ */
+class OrganizationAddressPostalValidator extends ConstraintValidator
+{
+    public function __construct(private OrganizationAddressPostalRepository $organizationAddressPostalRepository){}
+
+    public function validate($value, Constraint $constraint)
+    {
+        /** @var OrganizationAddressPostal $organizationAddressPostal */
+        $organizationAddressPostal = $value;
+
+        // si le type est adresse autre, on valide
+        if($organizationAddressPostal->getType() === AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue())
+            return;
+
+        $addressesByType = $this->organizationAddressPostalRepository->getByType($organizationAddressPostal->getType(), $organizationAddressPostal->getOrganization());
+        //Si le nombre d'adress du type est supérieur à 1, OU si le nombre est égale a 1 ET que l'id de l'adresse n'est pas celui en cours : invalide.
+        if(count($addressesByType) > 1 || (count($addressesByType) === 1 && $addressesByType[0]->getId() !== $organizationAddressPostal->getId())){
+            $this->context->buildViolation($constraint->message)
+                ->setParameter('{{ type }}', $organizationAddressPostal->getType())
+                ->atPath('type')
+                ->addViolation();
+        }
+    }
+}