Procházet zdrojové kódy

split the organization creation function

Olivier Massot před 1 rokem
rodič
revize
0ad6c1b91e
1 změnil soubory, kde provedl 188 přidání a 65 odebrání
  1. 188 65
      src/Service/Organization/OrganizationFactory.php

+ 188 - 65
src/Service/Organization/OrganizationFactory.php

@@ -3,6 +3,7 @@
 namespace App\Service\Organization;
 
 use App\ApiResources\Organization\OrganizationCreationRequest;
+use App\ApiResources\Organization\OrganizationMemberCreationRequest;
 use App\Entity\Access\Access;
 use App\Entity\Core\AddressPostal;
 use App\Entity\Core\ContactPoint;
@@ -24,6 +25,7 @@ use App\Service\Typo3\BindFileService;
 use App\Service\Typo3\SubdomainService;
 use App\Service\Typo3\Typo3Service;
 use App\Service\Utils\DatesUtils;
+use Elastica\Param;
 use libphonenumber\PhoneNumber;
 use Symfony\Component\String\ByteString;
 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@@ -50,22 +52,130 @@ class OrganizationFactory
      * @throws Throwable
      */
     public function create(OrganizationCreationRequest $organizationCreationRequest): Organization
+    {
+        // Création de l'organisation
+        $organization = $this->makeOrganization($organizationCreationRequest);
+
+        // Création des Parameters
+        $parameters = $this->makeParameters($organizationCreationRequest);
+        $organization->setParameters($parameters);
+
+        // Création des Settings
+        $settings = $this->makeSettings($organizationCreationRequest);
+        $organization->setSettings($settings);
+
+        // Création de l'adresse postale
+        $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
+        $organization->addOrganizationAddressPostal($organizationAddressPostal);
+
+        // Création du point de contact
+        $contactPoint = $this->makeContactPoint($organizationCreationRequest);
+        $organization->addContactPoint($contactPoint);
+
+        // Rattachement au réseau
+        $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
+        $organization->addNetworkOrganization($networkOrganization);
+
+        // Créé l'admin
+        $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
+        $adminAccess->setOrganization($organization);
+
+        // Création des cycles
+        foreach ($this->makeCycles() as $cycle) {
+            $cycle->setOrganization($organization);
+            $organization->addCycle($cycle);
+        }
+
+        // Création du sous domaine
+        $this->subdomainService->addNewSubdomain(
+            $organization,
+            $organizationCreationRequest->getSubdomain(),
+            true
+        );
+
+        // Création du président (si renseigné)
+        $presidentCreationRequest = $organizationCreationRequest->getPresident();
+        if ($presidentCreationRequest !== null) {
+            $presidentAccess = $this->makeAccess($presidentCreationRequest);
+            $organization->addAccess($presidentAccess);
+        }
+
+        // Création du directeur (si renseigné)
+        $directorCreationRequest = $organizationCreationRequest->getDirector();
+        if ($directorCreationRequest !== null) {
+            $directorAccess = $this->makeAccess($directorCreationRequest);
+            $organization->addAccess($directorAccess);
+        }
+
+        // Création du site typo3
+        if ($organizationCreationRequest->getCreateWebsite()) {
+            $this->typo3Service->createSite($organization->getId());
+        }
+
+        // Création de la société Dolibarr
+        $this->dolibarrApiService->createSociety($organization);
+
+        // Mise à jour du fichier Bind
+        $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
+
+        return $organization;
+    }
+
+    /**
+     * Créé une nouvelle instance d'organisation
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest
+     * @return Organization
+     */
+    protected function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization
     {
         // Création de l'organisation
         $organization = new Organization();
         $organization->setName($organizationCreationRequest->getName());
         $organization->setLegalStatus($organizationCreationRequest->getLegalStatus());
 
-        // Création des Parameters
+        return $organization;
+    }
+
+    /**
+     * Create a new Parameters object from the data in an OrganizationCreationRequest
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The organization creation request
+     * @return Parameters The created Parameters object
+     * @throws TransportExceptionInterface If there is an error with the transport
+     * @throws Throwable If there is an error
+     */
+    protected function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters
+    {
         $parameters = new Parameters();
-        $organization->setParameters($parameters);
 
-        // Création des Settings
+        return $parameters;
+    }
+
+    /**
+     * Creates a new instance of the Settings class based on the given OrganizationCreationRequest object.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
+     *
+     * @return Settings The newly created instance of the Settings class.
+     */
+    protected function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings
+    {
         $settings = new Settings();
         $settings->setProduct($organizationCreationRequest->getProduct());
-        $organization->setSettings($settings);
 
-        // Création de l'adresse postale
+        return $settings;
+    }
+
+    /**
+     * Creates a new instance of the OrganizationAddressPostal class based on the given OrganizationCreationRequest object.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
+     *
+     * @return OrganizationAddressPostal The newly created instance of the OrganizationAddressPostal class.
+     */
+    protected function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal
+    {
         $addressPostal = new AddressPostal();
         $addressPostal->setStreetAddress($organizationCreationRequest->getStreetAddress1());
         $addressPostal->setStreetAddressSecond($organizationCreationRequest->getStreetAddress2());
@@ -79,9 +189,19 @@ class OrganizationFactory
         $organizationAddressPostal = new OrganizationAddressPostal();
         $organizationAddressPostal->setAddressPostal($addressPostal);
         $organizationAddressPostal->setType(AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE);
-        $organization->addOrganizationAddressPostal($organizationAddressPostal);
 
-        // Création du point de contact
+        return $organizationAddressPostal;
+    }
+
+    /**
+     * Creates a new instance of the ContactPoint class based on the given OrganizationCreationRequest object.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
+     *
+     * @return ContactPoint The newly created instance of the ContactPoint class.
+     */
+    protected function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint
+    {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->unserialize($organizationCreationRequest->getPhoneNumber());
 
@@ -89,9 +209,21 @@ class OrganizationFactory
         $contactPoint->setContactType(ContactPointTypeEnum::PRINCIPAL);
         $contactPoint->setEmail($organizationCreationRequest->getEmail());
         $contactPoint->setTelphone($phoneNumber);
-        $organization->addContactPoint($contactPoint);
 
-        // Rattachement au réseau
+        return $contactPoint;
+    }
+
+    /**
+     * Creates a new instance of the NetworkOrganization class based on the given OrganizationCreationRequest object.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
+     *
+     * @return NetworkOrganization The newly created instance of the NetworkOrganization class.
+     *
+     * @throws \RuntimeException if no parent organization is found for the given parent ID or if no network is found for the given network ID.
+     */
+    protected function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization
+    {
         $parent = $this->organizationRepository->find($organizationCreationRequest->getParentId());
         if (!$parent) {
             throw new \RuntimeException('No parent organization found for id ' . $organizationCreationRequest->getParentId());
@@ -106,9 +238,19 @@ class OrganizationFactory
         $networkOrganization->setParent($parent);
         $networkOrganization->setNetwork($network);
         $networkOrganization->setStartDate(DatesUtils::new());
-        $organization->addNetworkOrganization($networkOrganization);
 
-        // Créé l'admin
+        return $networkOrganization;
+    }
+
+    /**
+     * Creates a new instance of the Access class with admin access based on the given OrganizationCreationRequest object.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest The OrganizationCreationRequest object containing the required data.
+     *
+     * @return Access The newly created instance of the Access class with admin access.
+     */
+    protected function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access
+    {
         $admin = new Person();
         $admin->setUsername('admin' . strtolower($organizationCreationRequest->getSubdomain()));
         $randomString = ByteString::fromRandom(32)->toString();
@@ -117,9 +259,17 @@ class OrganizationFactory
         $adminAccess = new Access();
         $adminAccess->setAdminAccess(true);
         $adminAccess->setPerson($admin);
-        $adminAccess->setOrganization($organization);
 
-        // Création des cycles
+        return $adminAccess;
+    }
+
+    /**
+     * Creates an array of Cycle objects based on a predefined set of data.
+     *
+     * @return Cycle[] An array of Cycle objects.
+     */
+    protected function makeCycles(): array
+    {
         $cyclesData = [
             ['Cycle initiation', 10, CycleEnum::INITIATION_CYCLE],
             ['Cycle 1', 20, CycleEnum::CYCLE_1],
@@ -129,71 +279,44 @@ class OrganizationFactory
             ['Hors cycle', 60, CycleEnum::OUT_CYCLE],
         ];
 
+        $cycles = [];
+
         foreach ($cyclesData as $cycleData) {
             $cycle = new Cycle();
             $cycle->setLabel($cycleData[0]);
             $cycle->setOrder($cycleData[1]);
             $cycle->setCycleEnum($cycleData[2]);
             $cycle->setIsSystem(false);
-            $cycle->setOrganization($organization);
-            $organization->addCycle($cycle);
-        }
-
-        // Création du sous domaine
-        $this->subdomainService->addNewSubdomain(
-            $organization,
-            $organizationCreationRequest->getSubdomain(),
-            true
-        );
-
-        // Création du président (si renseigné)
-        $presidentCreationRequest = $organizationCreationRequest->getPresident();
-        if ($presidentCreationRequest !== null) {
-            $president = new Person();
-
-            $president->setUsername(
-                strtolower(str_replace(' ', '-', $presidentCreationRequest->getName()))
-            );
-            $president->setPassword(ByteString::fromRandom(32)->toString());
-
-            $president->setGender($presidentCreationRequest->getGender());
-            $president->setName($presidentCreationRequest->getName());
-            $president->setGivenName($presidentCreationRequest->getGivenName());
-
-            $presidentAccess = new Access();
-            $presidentAccess->setPerson($president);
-            $organization->addAccess($presidentAccess);
+            $cycles[] = $cycle;
         }
 
-        // Création du directeur (si renseigné)
-        if ($organizationCreationRequest->getDirector() !== null) {
-            $director = new Person();
+        return $cycles;
+    }
 
-            $director->setUsername(
-                strtolower(str_replace(' ', '-', $presidentCreationRequest->getName()))
-            );
-            $director->setPassword(ByteString::fromRandom(32)->toString());
+    /**
+     * Creates an Access object based on the given OrganizationMemberCreationRequest.
+     *
+     * @param OrganizationMemberCreationRequest $organizationMemberCreationRequest The request object containing the necessary data for creating an Access object.
+     * @return Access The created Access object.
+     */
+    protected function makeAccess(OrganizationMemberCreationRequest $organizationMemberCreationRequest): Access
+    {
+        $president = new Person();
 
-            $director->setGender($presidentCreationRequest->getGender());
-            $director->setName($presidentCreationRequest->getName());
-            $director->setGivenName($presidentCreationRequest->getGivenName());
+        $president->setUsername(
+            strtolower(str_replace(' ', '-', $organizationMemberCreationRequest->getName()))
+        );
+        $president->setPassword(ByteString::fromRandom(32)->toString());
 
-            $directorAccess = new Access();
-            $directorAccess->setPerson($director);
-            $organization->addAccess($directorAccess);
-        }
+        $president->setGender($organizationMemberCreationRequest->getGender());
+        $president->setName($organizationMemberCreationRequest->getName());
+        $president->setGivenName($organizationMemberCreationRequest->getGivenName());
 
-        // Création du site typo3
-        if ($organizationCreationRequest->getCreateWebsite()) {
-            $this->typo3Service->createSite($organization->getId());
-        }
+        $presidentAccess = new Access();
+        $presidentAccess->setPerson($president);
 
-        // Création de la société Dolibarr
-        $this->dolibarrApiService->createSociety($organization);
+        return $presidentAccess;
+    }
 
-        // Mise à jour du fichier Bind
-        $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
 
-        return $organization;
-    }
 }