瀏覽代碼

minox fixes

Olivier Massot 1 年之前
父節點
當前提交
459cf19437

+ 17 - 0
src/ApiResources/Organization/OrganizationCreationRequest.php

@@ -115,6 +115,12 @@ class OrganizationCreationRequest
      */
     private bool $createWebsite = true;
 
+    /**
+     * For testing purposes only
+     * @var bool
+     */
+    private bool $async = true;
+
     public function getId(): int
     {
         return $this->id;
@@ -323,4 +329,15 @@ class OrganizationCreationRequest
         $this->createWebsite = $createWebsite;
         return $this;
     }
+
+    public function isAsync(): bool
+    {
+        return $this->async;
+    }
+
+    public function setAsync(bool $async): self
+    {
+        $this->async = $async;
+        return $this;
+    }
 }

+ 136 - 90
src/Service/Organization/OrganizationFactory.php

@@ -35,6 +35,9 @@ use libphonenumber\PhoneNumberUtil;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\String\ByteString;
+use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
+use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
+use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\Service\Attribute\Required;
 use Throwable;
@@ -74,98 +77,37 @@ class OrganizationFactory
     public function create(OrganizationCreationRequest $organizationCreationRequest): Organization
     {
         $this->logger->info(
-            'Start the creation of a new organization named ' . $organizationCreationRequest->getName()
+            "Start the creation of a new organization named '" . $organizationCreationRequest->getName() . "'"
         );
 
         $this->entityManager->beginTransaction();
 
         try {
+            // On vérifie si cette organisation n'existe pas déjà
             if ($this->isExistingOrganization($organizationCreationRequest)) {
                 throw new \RuntimeException('An organization named ' . $organizationCreationRequest->getName() . ' already exists.');
             }
 
+            // On vérifie la validité et la disponibilité du sous domaine
             $this->validateSubdomain($organizationCreationRequest->getSubdomain());
             $this->logger->info("Subdomain is valid and available : " . $organizationCreationRequest->getSubdomain());
 
-            // Création de l'organisation
-            $organization = $this->makeOrganization($organizationCreationRequest);
-            $this->logger->debug(" - Organization created");
-
-            // Création des Parameters
-            $parameters = $this->makeParameters($organizationCreationRequest);
-            $organization->setParameters($parameters);
-            $this->logger->debug(" - Parameters created");
-
-            // Création des Settings
-            $settings = $this->makeSettings($organizationCreationRequest);
-            $organization->setSettings($settings);
-            $this->logger->debug(" - Settings created");
-
-            // Création de l'adresse postale
-            $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
-            $organization->addOrganizationAddressPostal($organizationAddressPostal);
-            $this->logger->debug(" - OrganizationAddressPostal created");
-
-            // Création du point de contact
-            $contactPoint = $this->makeContactPoint($organizationCreationRequest);
-            $organization->addContactPoint($contactPoint);
-            $this->logger->debug(" - ContactPoint created");
-
-            // Rattachement au réseau
-            $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
-            $organization->addNetworkOrganization($networkOrganization);
-            $this->logger->debug(" - NetworkOrganization created");
-
-            // Créé l'admin
-            $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
-            $organization->addAccess($adminAccess);
-            $this->logger->debug(" - Admin access created");
-
-            // Création des cycles
-            foreach ($this->makeCycles() as $cycle) {
-                $organization->addCycle($cycle);
-            }
-            $this->logger->debug(" - Cycles created");
-
-            // Création du président (si renseigné)
-            $presidentCreationRequest = $organizationCreationRequest->getPresident();
-            if ($presidentCreationRequest !== null) {
-                $presidentAccess = $this->makeAccess($presidentCreationRequest);
-                $organization->addAccess($presidentAccess);
-                $this->logger->debug(" - President access created");
-            }
-
-            // Création du directeur (si renseigné)
-            $directorCreationRequest = $organizationCreationRequest->getDirector();
-            if ($directorCreationRequest !== null) {
-                $directorAccess = $this->makeAccess($directorCreationRequest);
-                $organization->addAccess($directorAccess);
-                $this->logger->debug(" - Director access created");
-            }
-
-            $subdomain = new Subdomain();
-            $subdomain->setSubdomain($organizationCreationRequest->getSubdomain());
-            $subdomain->setOrganization($organization);
-            $subdomain->setActive(true);
-            $this->entityManager->persist($subdomain);
+            // On construit l'organisation et ses relations
+            $organization = $this->makeOrganizationWithRelations($organizationCreationRequest);
+            $this->logger->info("Organization created with all its relations");
 
-            // <--- Pour la rétrocompatibilité avec la v1 ; pourra être supprimé lorsque la migration sera achevée
-            $parameters = $organization->getParameters();
-            $parameters->setSubDomain($organizationCreationRequest->getSubdomain());
-            $parameters->setOtherWebsite('https://' . $organizationCreationRequest->getSubdomain() . '.opentalent.fr');
-            $this->entityManager->persist($parameters);
-            // --->
+            // On persiste et on commit
+            $this->entityManager->persist($organization);
+            $this->entityManager->flush();
 
             // Création de la société Dolibarr
             $dolibarrId = $this->dolibarrApiService->createSociety($organization);
             $this->logger->info("New dolibarr structure created (uid : " . $dolibarrId . ")");
 
-            $this->entityManager->persist($organization);
-            $this->entityManager->flush();
             $this->entityManager->commit();
+            $this->logger->debug(" - New entities committed in DB");
 
-            $this->logger->debug(" - New records commited");
-            $this->logger->info("Organization created in the DB");
+            $this->logger->info("Organization persisted in the DB");
 
         } catch (\Exception $e) {
             $this->logger->critical("An error happened, operation cancelled : " . $e);
@@ -174,28 +116,12 @@ class OrganizationFactory
         }
 
         // Register the subdomain into the BindFile (takes up to 5min to take effect)
-        $this->bindFileService->registerSubdomain($subdomain->getSubdomain());
+        $this->bindFileService->registerSubdomain($organizationCreationRequest->getSubdomain());
         $this->logger->info("Subdomain registered");
 
         // Création du site typo3 (on est obligé d'attendre que l'organisation soit persistée en base)
         if ($organizationCreationRequest->getCreateWebsite()) {
-            $response = $this->typo3Service->createSite($organization->getId());
-            $rootPageUid = json_decode($response->getContent(), true);
-
-            if (
-                $response->getStatusCode() === Response::HTTP_OK ||
-                !$rootPageUid
-            ) {
-                // TODO: revoir l'utilité du champs cmsId
-                $organization->setCmsId($rootPageUid);
-                $this->entityManager->persist($organization);
-                $this->entityManager->flush();
-                $this->logger->info("New typo3 website created (root uid : " . $rootPageUid . ")");
-
-            } else {
-                $this->logger->critical("/!\ A critical error happened while creating the Typo3 website ");
-                $this->logger->debug($response->getContent());
-            }
+            $this->createTypo3Website($organization);
         } else {
             $this->logger->warning("Typo3 website creation was not required");
         }
@@ -242,6 +168,89 @@ class OrganizationFactory
         }
     }
 
+    /**
+     * Créé une nouvelle instance d'organisation, et toutes les instances liées (paramètres, contact, adresses, ...),
+     * selon le contenu de la requête de création.
+     *
+     * @param OrganizationCreationRequest $organizationCreationRequest
+     * @return Organization
+     * @throws Throwable
+     */
+    protected function makeOrganizationWithRelations(
+        OrganizationCreationRequest $organizationCreationRequest
+    ): Organization
+    {
+        // Création de l'organisation
+        $organization = $this->makeOrganization($organizationCreationRequest);
+        $this->logger->debug(" - Organization created");
+
+        // Création des Parameters
+        $parameters = $this->makeParameters($organizationCreationRequest);
+        $organization->setParameters($parameters);
+        $this->logger->debug(" - Parameters created");
+
+        // Création des Settings
+        $settings = $this->makeSettings($organizationCreationRequest);
+        $organization->setSettings($settings);
+        $this->logger->debug(" - Settings created");
+
+        // Création de l'adresse postale
+        $organizationAddressPostal = $this->makePostalAddress($organizationCreationRequest);
+        $organization->addOrganizationAddressPostal($organizationAddressPostal);
+        $this->logger->debug(" - OrganizationAddressPostal created");
+
+        // Création du point de contact
+        $contactPoint = $this->makeContactPoint($organizationCreationRequest);
+        $organization->addContactPoint($contactPoint);
+        $this->logger->debug(" - ContactPoint created");
+
+        // Rattachement au réseau
+        $networkOrganization = $this->makeNetworkOrganization($organizationCreationRequest);
+        $organization->addNetworkOrganization($networkOrganization);
+        $this->logger->debug(" - NetworkOrganization created");
+
+        // Créé l'admin
+        $adminAccess = $this->makeAdminAccess($organizationCreationRequest);
+        $organization->addAccess($adminAccess);
+        $this->logger->debug(" - Admin access created");
+
+        // Création des cycles
+        foreach ($this->makeCycles() as $cycle) {
+            $organization->addCycle($cycle);
+        }
+        $this->logger->debug(" - Cycles created");
+
+        // Création du président (si renseigné)
+        $presidentCreationRequest = $organizationCreationRequest->getPresident();
+        if ($presidentCreationRequest !== null) {
+            $presidentAccess = $this->makeAccess($presidentCreationRequest);
+            $organization->addAccess($presidentAccess);
+            $this->logger->debug(" - President access created");
+        }
+
+        // Création du directeur (si renseigné)
+        $directorCreationRequest = $organizationCreationRequest->getDirector();
+        if ($directorCreationRequest !== null) {
+            $directorAccess = $this->makeAccess($directorCreationRequest);
+            $organization->addAccess($directorAccess);
+            $this->logger->debug(" - Director access created");
+        }
+
+        $subdomain = $this->makeSubdomain($organizationCreationRequest);
+        $subdomain->setOrganization($organization);
+
+        // <--- Pour la rétrocompatibilité avec la v1 ; pourra être supprimé lorsque la migration sera achevée
+        $parameters = $organization->getParameters();
+        $parameters->setSubDomain($organizationCreationRequest->getSubdomain());
+        $parameters->setOtherWebsite('https://' . $organizationCreationRequest->getSubdomain() . '.opentalent.fr');
+        $this->entityManager->persist($parameters);
+        // --->
+        $this->logger->debug(" - Subdomain created");
+
+        return $organization;
+    }
+
+
     /**
      * Créé une nouvelle instance d'organisation
      *
@@ -520,4 +529,41 @@ class OrganizationFactory
         $this->entityManager->persist($contactPoint);
         return $contactPoint;
     }
+
+    protected function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain
+    {
+        $subdomain = new Subdomain();
+        $subdomain->setSubdomain($organizationCreationRequest->getSubdomain());
+        $subdomain->setActive(true);
+        $this->entityManager->persist($subdomain);
+
+        return $subdomain;
+    }
+
+    /**
+     * Créé le site Typo3 et retourne l'id de la page racine du site nouvellement créé, ou null en cas d'erreur
+     *
+     * @throws RedirectionExceptionInterface
+     * @throws ClientExceptionInterface
+     * @throws TransportExceptionInterface
+     * @throws ServerExceptionInterface
+     */
+    protected function createTypo3Website(Organization $organization): ?int {
+        $response = $this->typo3Service->createSite($organization->getId());
+        $rootPageUid = json_decode($response->getContent(), true);
+
+        if ($response->getStatusCode() === Response::HTTP_OK && $rootPageUid > 0) {
+            // TODO: revoir l'utilité du champs cmsId
+            $organization->setCmsId($rootPageUid);
+            $this->entityManager->persist($organization);
+            $this->entityManager->flush();
+
+            return $rootPageUid;
+        } else {
+            $this->logger->critical("/!\ A critical error happened while creating the Typo3 website ");
+            $this->logger->debug($response->getContent());
+        }
+
+        return null;
+    }
 }

+ 12 - 6
src/State/Processor/Organization/OrganizationCreationRequestProcessor.php

@@ -9,13 +9,15 @@ use ApiPlatform\Metadata\Post;
 use ApiPlatform\State\ProcessorInterface;
 use App\ApiResources\Organization\OrganizationCreationRequest;
 use App\Message\Command\OrganizationCreationCommand;
+use App\Service\Organization\OrganizationFactory;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Messenger\MessageBusInterface;
 
 class OrganizationCreationRequestProcessor implements ProcessorInterface
 {
     public function __construct(
-        private readonly MessageBusInterface $messageBus
+        private readonly MessageBusInterface $messageBus,
+        private readonly OrganizationFactory $organizationFactory,
     ) {}
 
     /**
@@ -31,11 +33,15 @@ class OrganizationCreationRequestProcessor implements ProcessorInterface
             throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
         }
 
-        // Send the export request to Messenger (@see App\Message\Handler\OrganizationCreationHandler)
-        $this->messageBus->dispatch(
-            new OrganizationCreationCommand($organizationCreationRequest)
-        );
-
+        if ($organizationCreationRequest->isAsync()) {
+            // Send the export request to Messenger (@see App\Message\Handler\OrganizationCreationHandler)
+            $this->messageBus->dispatch(
+                new OrganizationCreationCommand($organizationCreationRequest)
+            );
+        } else {
+            // For testing purposes only
+            $this->organizationFactory->create($organizationCreationRequest);
+        }
         return $organizationCreationRequest;
     }
 }