Explorar o código

async update typo3 website after parameters modification

Olivier Massot %!s(int64=3) %!d(string=hai) anos
pai
achega
669825b963

+ 2 - 0
config/packages/messenger.yaml

@@ -13,3 +13,5 @@ framework:
             # Route your messages to the transports
             'App\Message\Command\Export': async
             'App\Message\Command\Typo3\Typo3UpdateCommand': async
+            'App\Message\Command\Typo3\Typo3DeleteCommand': async
+            'App\Message\Command\Typo3\Typo3UndeleteCommand': async

+ 28 - 7
src/DataPersister/Organization/ParametersDataPersister.php

@@ -6,6 +6,9 @@ namespace App\DataPersister\Organization;
 use App\DataPersister\BaseDataPersister;
 use App\Entity\Organization\Parameters;
 use App\Message\Command\Parameters\AverageChange;
+use App\Message\Command\Typo3\Typo3DeleteCommand;
+use App\Message\Command\Typo3\Typo3UndeleteCommand;
+use App\Message\Command\Typo3\Typo3UpdateCommand;
 use App\Service\OnChange\Organization\OnParametersChange;
 use App\Service\Typo3\Typo3Service;
 use Exception;
@@ -19,7 +22,9 @@ class ParametersDataPersister extends BaseDataPersister
     public function __construct(
         private MessageBusInterface $messageBus,
         private OnParametersChange $onParametersChange,
-        private Typo3Service $typo3Service
+        private Typo3Service $typo3Service,
+        private \App\Service\Network\Utils $networkUtils
+
     )
     {}
 
@@ -50,6 +55,14 @@ class ParametersDataPersister extends BaseDataPersister
                 $this->previousData()->getMusicalDate()
             );
         }
+
+        // Une structure CMF n'a pas le droit de désactiver son site typo3
+        if (
+            $parameters->getDesactivateOpentalentSiteWeb() === true &&
+            $this->networkUtils->isCMFAndActiveNow($parameters->getOrganization())
+        ) {
+            throw new \RuntimeException('This structure is currently active in the CMF network, the website can not be disabled.');
+        }
     }
 
     /**
@@ -59,20 +72,22 @@ class ParametersDataPersister extends BaseDataPersister
         //La note maximale du suivi pédagogique change
         if(
             $this->previousData() &&
-            $this->previousData()->getAverage() != $parameters->getAverage()
+            $this->previousData()->getAverage() !== $parameters->getAverage()
         ){
             $this->messageBus->dispatch(
                 new AverageChange($parameters->getId())
             );
         }
 
-        // Le custom domain a été modifié
+        // Le customDomain a été modifié, on met à jour le site typo3 (s'il est actif)
         if(
             $this->previousData() &&
             !$parameters->getDesactivateOpentalentSiteWeb() &&
             $this->previousData()->getCustomDomain() !== $parameters->getCustomDomain()
         ){
-            $this->typo3Service->updateSite($parameters->getOrganization()->getId());
+            $this->messageBus->dispatch(
+                new Typo3UpdateCommand($parameters->getOrganization()->getId())
+            );
         }
 
         // Le site web opentalent a été désactivé / réactivé
@@ -81,10 +96,16 @@ class ParametersDataPersister extends BaseDataPersister
             $this->previousData()->getDesactivateOpentalentSiteWeb() !== $parameters->getDesactivateOpentalentSiteWeb()
         ){
             if ($parameters->getDesactivateOpentalentSiteWeb()) {
-                $this->typo3Service->deleteSite($parameters->getOrganization()->getId());
+                $this->messageBus->dispatch(
+                    new Typo3DeleteCommand($parameters->getOrganization()->getId())
+                );
             } else {
-                $this->typo3Service->undeleteSite($parameters->getOrganization()->getId());
-                $this->typo3Service->updateSite($parameters->getOrganization()->getId());
+                $this->messageBus->dispatch(
+                    new Typo3UndeleteCommand($parameters->getOrganization()->getId())
+                );
+                $this->messageBus->dispatch(
+                    new Typo3UpdateCommand($parameters->getOrganization()->getId())
+                );
             }
         }
     }

+ 31 - 0
src/Message/Command/Typo3/Typo3DeleteCommand.php

@@ -0,0 +1,31 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Message\Command\Typo3;
+
+/**
+ * Envoi d'une requête à l'api Typo3
+ */
+class Typo3DeleteCommand
+{
+    public function __construct(
+        private int $organizationId
+    )
+    {}
+
+    /**
+     * @return int
+     */
+    public function getOrganizationId(): int
+    {
+        return $this->organizationId;
+    }
+
+    /**
+     * @param int $organizationId
+     */
+    public function setOrganizationId(int $organizationId): void
+    {
+        $this->organizationId = $organizationId;
+    }
+}

+ 31 - 0
src/Message/Command/Typo3/Typo3UndeleteCommand.php

@@ -0,0 +1,31 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Message\Command\Typo3;
+
+/**
+ * Envoi d'une requête à l'api Typo3
+ */
+class Typo3UndeleteCommand
+{
+    public function __construct(
+        private int $organizationId
+    )
+    {}
+
+    /**
+     * @return int
+     */
+    public function getOrganizationId(): int
+    {
+        return $this->organizationId;
+    }
+
+    /**
+     * @param int $organizationId
+     */
+    public function setOrganizationId(int $organizationId): void
+    {
+        $this->organizationId = $organizationId;
+    }
+}

+ 21 - 0
src/Message/Handler/Typo3/Typo3DeleteCommandHandler.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Message\Handler\Typo3;
+
+use App\Message\Command\Typo3\Typo3DeleteCommand;
+use App\Service\Typo3\Typo3Service;
+use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
+
+class Typo3DeleteCommandHandler implements MessageHandlerInterface
+{
+    public function __construct(
+        private Typo3Service $typo3Service
+    ) {}
+
+    /**
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
+     */
+    public function __invoke(Typo3DeleteCommand $command) {
+        $this->typo3Service->deleteSite($command->getOrganizationId());
+    }
+}

+ 21 - 0
src/Message/Handler/Typo3/Typo3UndeleteCommandHandler.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Message\Handler\Typo3;
+
+use App\Message\Command\Typo3\Typo3UndeleteCommand;
+use App\Service\Typo3\Typo3Service;
+use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
+
+class Typo3UndeleteCommandHandler implements MessageHandlerInterface
+{
+    public function __construct(
+        private Typo3Service $typo3Service
+    ) {}
+
+    /**
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
+     */
+    public function __invoke(Typo3UndeleteCommand $command) {
+        $this->typo3Service->undeleteSite($command->getOrganizationId());
+    }
+}

+ 4 - 1
src/Service/Network/Utils.php

@@ -15,6 +15,9 @@ use App\Tests\Service\Network\UtilsTest;
  */
 class Utils
 {
+    // TODO: question - est-ce qu'on ne passerait pas toutes ces méthodes en static?
+    // TODO: question - est-ce qu'on ne ferait pas mieux de renommer les différentes classes Utils avec des noms
+    //                  différents? comme NetworkUtils ou OrganizationUtils? pour éviter les confusions?
     /**
      * Test si l'organisation appartient au réseau de la CMF
      * @param Organization $organization
@@ -63,4 +66,4 @@ class Utils
     public function doesNetworkOrganizationIsActiveNow(NetworkOrganization $networksOrganization): bool{
         return DatesUtils::isIntervalIsValidNow($networksOrganization->getStartDate(), $networksOrganization->getEndDate());
     }
-}
+}