Kaynağa Gözat

add the deleteDolibarrSociety method to DolibarrApiService (ongoing)

Olivier Massot 11 ay önce
ebeveyn
işleme
3c064d0e08

+ 24 - 0
src/Service/Dolibarr/DolibarrApiService.php

@@ -6,9 +6,12 @@ namespace App\Service\Dolibarr;
 
 use App\Entity\Organization\Organization;
 use App\Service\Rest\ApiRequestService;
+use App\Service\Utils\SecurityUtils;
 use JetBrains\PhpStorm\Pure;
+use JsonException;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 /**
@@ -202,4 +205,25 @@ class DolibarrApiService extends ApiRequestService
 
         return json_decode($response->getContent(), true);
     }
+
+    /**
+     * Delete the organization from Dolibarr
+     *
+     * @param int $organizationId
+     * @return void
+     * @throws JsonException
+     * @throws TransportExceptionInterface
+     */
+    public function deleteDolibarrSociety(int $organizationId): void
+    {
+        SecurityUtils::preventIfNotLocalhost();
+
+        $socId = $this->getSociety($organizationId)['id'];
+
+        $res = $this->delete("thirdparties/$socId");
+
+        if ($res->getStatusCode() !== 200) {
+            throw new HttpException($res->getStatusCode(), 'Error while deleting the organization from Dolibarr');
+        }
+    }
 }

+ 3 - 19
src/Service/Organization/OrganizationFactory.php

@@ -32,6 +32,7 @@ use App\Service\Typo3\BindFileService;
 use App\Service\Typo3\SubdomainService;
 use App\Service\Typo3\Typo3Service;
 use App\Service\Utils\DatesUtils;
+use App\Service\Utils\SecurityUtils;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\EntityManagerInterface;
 use Elastica\Param;
@@ -603,7 +604,7 @@ class OrganizationFactory
      */
     public function delete(OrganizationDeletionRequest $organizationDeletionRequest): OrganizationDeletionRequest
     {
-        $this->preventIfNotLocalhost();
+        SecurityUtils::preventIfNotLocalhost();
 
         $organization = $this->organizationRepository->find($organizationDeletionRequest->getOrganizationId());
 
@@ -684,23 +685,6 @@ class OrganizationFactory
         return $organizationDeletionRequest;
     }
 
-    /**
-     * Lève une exception si la méthode a été appelée dans le cadre d'un appel API originaire d'un hôte
-     * différent de localhost.
-     *
-     * @return void
-     */
-    protected function preventIfNotLocalhost(): void
-    {
-        if (
-            $_SERVER &&
-            $_SERVER['APP_ENV'] !== 'docker' &&
-            $_SERVER['SERVER_ADDR'] !== $_SERVER['REMOTE_ADDR']
-        ) {
-            throw new \RuntimeException("This operation is restricted to localhost");
-        }
-    }
-
     /**
      * Supprime tous les Access d'une organisation, ainsi que la Person
      * rattachée (si celle-ci n'est pas liée à d'autres Access)
@@ -749,7 +733,7 @@ class OrganizationFactory
 
     protected function deleteDolibarrSociety(Organization $organization): void
     {
-        // TODO: implement
+        $this->dolibarrApiService->deleteDolibarrSociety($organization->getId());
     }
 
     protected function deleteLocalDirectories(Organization $organization): void

+ 24 - 0
src/Service/Utils/SecurityUtils.php

@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Utils;
+
+class SecurityUtils
+{
+    /**
+     * Lève une exception si la méthode a été appelée dans le cadre d'un appel API originaire d'un hôte
+     * différent de localhost.
+     *
+     * @return void
+     */
+    public static function preventIfNotLocalhost(): void
+    {
+        if (
+            $_SERVER &&
+            $_SERVER['APP_ENV'] !== 'docker' &&
+            $_SERVER['SERVER_ADDR'] !== $_SERVER['REMOTE_ADDR']
+        ) {
+            throw new \RuntimeException("This operation is restricted to localhost");
+        }
+    }
+}