Olivier Massot 1 год назад
Родитель
Сommit
3e3e5c26ac
1 измененных файлов с 23 добавлено и 2 удалено
  1. 23 2
      src/Service/Organization/OrganizationFactory.php

+ 23 - 2
src/Service/Organization/OrganizationFactory.php

@@ -20,6 +20,7 @@ use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Education\CycleEnum;
 use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
+use App\Enum\Organization\SettingsProductEnum;
 use App\Enum\Person\AddressPostalPersonTypeEnum;
 use App\Repository\Core\CountryRepository;
 use App\Repository\Organization\OrganizationRepository;
@@ -34,7 +35,6 @@ use Elastica\Param;
 use libphonenumber\NumberParseException;
 use libphonenumber\PhoneNumberUtil;
 use Psr\Log\LoggerInterface;
-use SebastianBergmann\Type\RuntimeException;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\String\ByteString;
 use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
@@ -314,13 +314,17 @@ class OrganizationFactory
      */
     protected function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
     {
+        $country = $this->countryRepository->find($organizationCreationRequest->getCountryId());
+        if (!$country) {
+            throw new \RuntimeException('No country found for id ' . $organizationCreationRequest->getCountryId());
+        }
+
         $addressPostal = new AddressPostal();
         $addressPostal->setStreetAddress($organizationCreationRequest->getStreetAddress1());
         $addressPostal->setStreetAddressSecond($organizationCreationRequest->getStreetAddress2());
         $addressPostal->setStreetAddressThird($organizationCreationRequest->getStreetAddress3());
         $addressPostal->setPostalCode($organizationCreationRequest->getPostalCode());
         $addressPostal->setAddressCity($organizationCreationRequest->getCity());
-        $country = $this->countryRepository->find($organizationCreationRequest->getCountryId());
         $addressPostal->setAddressCountry($country);
         $this->entityManager->persist($addressPostal);
 
@@ -343,6 +347,9 @@ class OrganizationFactory
     protected function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
     {
         $phoneUtil = PhoneNumberUtil::getInstance();
+        if (!$phoneUtil->isPossibleNumber($organizationCreationRequest->getPhoneNumber())) {
+            throw new \RuntimeException("Phone number is invalid or missing");
+        }
         $phoneNumber = $phoneUtil->parse($organizationCreationRequest->getPhoneNumber());
 
         $contactPoint = new ContactPoint();
@@ -370,6 +377,13 @@ class OrganizationFactory
             throw new \RuntimeException('No parent organization found for id ' . $organizationCreationRequest->getParentId());
         }
 
+        if ($parent->getSettings()->getProduct() !== SettingsProductEnum::MANAGER) {
+            throw new \RuntimeException(
+                "Parent organization must have the product 'manager' (actual product: " .
+                $organizationCreationRequest->getParentId() . ")"
+            );
+        }
+
         $networkOrganization = $this->organizationUtils->getActiveNetworkOrganization($parent);
         if (!$networkOrganization) {
             throw new \RuntimeException('No network found for parent ' . $organizationCreationRequest->getParentId());
@@ -382,6 +396,13 @@ class OrganizationFactory
             if (!preg_match("/FR\d{12}/", $organizationCreationRequest->getIdentifier())) {
                 throw new \RuntimeException("CMF identifier is missing or invalid.");
             }
+            if ($this
+                ->organizationRepository
+                ->count(
+                    ['identifier' => $organizationCreationRequest->getIdentifier()]
+                ) > 0) {
+                throw new \RuntimeException("CMF identifier is already registered : '" . $organizationCreationRequest->getIdentifier() ."'");
+            }
         }
 
         $networkOrganization = new NetworkOrganization();