Browse Source

subdomain datapersister: call typo3 api with messenger

Olivier Massot 3 years ago
parent
commit
6bacd320b1

+ 1 - 1
config/packages/messenger.yaml

@@ -12,4 +12,4 @@ framework:
         routing:
             # Route your messages to the transports
             'App\Message\Command\Export': async
-            'App\Message\Command\Parameters\AverageChange': async
+            'App\Message\Command\Typo3\Typo3UpdateCommand': async

+ 9 - 4
src/DataPersister/Organization/SubdomainDataPersister.php

@@ -5,10 +5,13 @@ namespace App\DataPersister\Organization;
 
 use App\DataPersister\BaseDataPersister;
 use App\Entity\Organization\Subdomain;
+use App\Message\Command\Export;
+use App\Message\Command\Typo3\Typo3UpdateCommand;
 use App\Service\Organization\Utils;
 use App\Service\Typo3\BindFileService;
 use App\Service\Typo3\Typo3Service;
 use Exception;
+use Symfony\Component\Messenger\MessageBusInterface;
 
 /**
  * Classe ParametersDataPersister qui est un custom dataPersister gérant la resource Parameters
@@ -16,8 +19,8 @@ use Exception;
 class SubdomainDataPersister extends BaseDataPersister
 {
     public function __construct(
-        private Typo3Service $typo3Service,
-        private BindFileService $bindFileService
+        private BindFileService $bindFileService,
+        private MessageBusInterface $messageBus,
     )
     {
     }
@@ -75,11 +78,13 @@ class SubdomainDataPersister extends BaseDataPersister
             $this->entityManager->flush();
         }
 
-        // Update the typo3 website
+        // Update the typo3 website (asynchronously with messenger)
         // /!\ This has to be executed after everything has been persisted
         if ($this->isPutRequest() && $this->previousData()) {
             if ($subdomain->isActive() && !$this->previousData()->isActive()) {
-                $this->typo3Service->updateSite($subdomain->getOrganization()->getId());
+                $this->messageBus->dispatch(
+                    new Typo3UpdateCommand($subdomain->getOrganization()->getId())
+                );
             }
         }
     }

+ 2 - 1
src/Message/Command/Parameters/AverageChange.php

@@ -12,6 +12,7 @@ class AverageChange
     { }
 
     public function getParametersId(){
+        // Todo: penser à ajouter cette commande à la section routing du fichier config/packages/messenger.yaml
         return $this->parametersId;
     }
-}
+}

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

@@ -0,0 +1,31 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Message\Command\Typo3;
+
+/**
+ * Envoi d'une requête à l'api Typo3
+ */
+class Typo3UpdateCommand
+{
+    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/Typo3UpdateCommandHandler.php

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

+ 13 - 12
src/Service/Typo3/Typo3Service.php

@@ -5,11 +5,12 @@ namespace App\Service\Typo3;
 use App\Service\Utils\UrlBuilder;
 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
+use Symfony\Contracts\HttpClient\ResponseInterface;
 
 class Typo3Service
 {
     public function __construct(
-        private HttpClientInterface $typo3_client
+        private HttpClientInterface $typo3_client,
     )
     {}
 
@@ -19,10 +20,10 @@ class Typo3Service
      * @param string $route
      * @param array $parameters
      *
-     * @return \Symfony\Contracts\HttpClient\ResponseInterface
+     * @return ResponseInterface
      * @throws TransportExceptionInterface
      */
-    private function sendCommand(string $route, array $parameters): \Symfony\Contracts\HttpClient\ResponseInterface
+    private function sendCommand(string $route, array $parameters): ResponseInterface
     {
         $url = UrlBuilder::concatParameters('/typo3/index.php?route=' . $route, $parameters);
         return $this->typo3_client->request('GET', $url);
@@ -32,7 +33,7 @@ class Typo3Service
      * Clear the cache of the given organization's website
      * @throws TransportExceptionInterface
      */
-    public function clearSiteCache(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function clearSiteCache(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/clear-cache', ['organization-id' => $organizationId]);
     }
@@ -41,7 +42,7 @@ class Typo3Service
      * Create a new Typo3 website for the given organization
      * @throws TransportExceptionInterface
      */
-    public function createSite(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function createSite(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/create', ['organization-id' => $organizationId]);
     }
@@ -50,7 +51,7 @@ class Typo3Service
      * Update the given organization's website with the Opentalent DB data
      * @throws TransportExceptionInterface
      */
-    public function updateSite(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function updateSite(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/update', ['organization-id' => $organizationId]);
     }
@@ -59,7 +60,7 @@ class Typo3Service
      * Mark the given organization's website as deleted. This can be reverted with 'undeleteSite'
      * @throws TransportExceptionInterface
      */
-    public function deleteSite(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function deleteSite(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/delete', ['organization-id' => $organizationId]);
     }
@@ -68,7 +69,7 @@ class Typo3Service
      * Restore a website that has been deleted with 'deleteSite'
      * @throws TransportExceptionInterface
      */
-    public function undeleteSite(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function undeleteSite(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/undelete', ['organization-id' => $organizationId]);
     }
@@ -77,7 +78,7 @@ class Typo3Service
      * Set a custom domain for the given website
      * @throws TransportExceptionInterface
      */
-    public function setSiteDomain(int $organizationId, string $newDomain, bool $addRedirection=false): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function setSiteDomain(int $organizationId, string $newDomain, bool $addRedirection=false): ResponseInterface
     {
         $params = ['organization-id' => $organizationId, 'domain' => $newDomain];
         if ($addRedirection) {
@@ -90,7 +91,7 @@ class Typo3Service
      * Reset the permissions of the BE users of the given organization's website
      * @throws TransportExceptionInterface
      */
-    public function resetSitePerms(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function resetSitePerms(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/reset-perms', ['organization-id' => $organizationId]);
     }
@@ -99,7 +100,7 @@ class Typo3Service
      * Returns the given organization's website status
      * @throws TransportExceptionInterface
      */
-    public function getSiteStatus(int $organizationId): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function getSiteStatus(int $organizationId): ResponseInterface
     {
         return $this->sendCommand('/otadmin/site/status', ['organization-id' => $organizationId]);
     }
@@ -108,7 +109,7 @@ class Typo3Service
      * Returns the given organization's website status
      * @throws TransportExceptionInterface
      */
-    public function addRedirection(string $fromDomain, string $toDomain): \Symfony\Contracts\HttpClient\ResponseInterface
+    public function addRedirection(string $fromDomain, string $toDomain): ResponseInterface
     {
         return $this->sendCommand(
             '/otadmin/redirect/add',