Parcourir la source

refactor and complete api services

Olivier Massot il y a 3 ans
Parent
commit
11dbfcab4b

+ 5 - 1
composer.json

@@ -56,7 +56,11 @@
         "preferred-install": {
             "*": "dist"
         },
-        "sort-packages": true
+        "sort-packages": true,
+        "allow-plugins": {
+            "cyclonedx/cyclonedx-php-composer": true,
+            "symfony/flex": true
+        }
     },
     "autoload": {
         "psr-4": {

+ 50 - 3
src/Service/ApiRequestService.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 namespace App\Service;
 
 use App\Service\Utils\UrlBuilder;
+use HttpException;
 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
@@ -28,6 +29,7 @@ class ApiRequestService
      * @param array $parameters
      * @param array $options
      * @return array
+     * @throws HttpException
      */
     public function getJsonContent(string $path, array $parameters = [], array $options = []): array {
         return json_decode($this->getContent($path, $parameters, $options), true);
@@ -40,12 +42,13 @@ class ApiRequestService
      * @param array $parameters
      * @param array $options
      * @return string
+     * @throws HttpException
      */
     public function getContent(string $path, array $parameters = [], array $options = []): string {
         try {
             return $this->get($path, $parameters, $options)->getContent();
         } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
-            throw new NotFoundHttpException('data not found', $e, 404);
+            throw new HttpException('data not found', $e, 404);
         }
     }
 
@@ -56,11 +59,54 @@ class ApiRequestService
      * @param array $parameters
      * @param array $options
      * @return ResponseInterface
+     * @throws HttpException
      */
     protected function get(string $path, array $parameters = [], array $options = []): ResponseInterface {
         return $this->request('GET', $path, $parameters, $options);
     }
 
+    /**
+     * Sends a POST request and returns the response
+     *
+     * @param string $path
+     * @param array $parameters
+     * @param array $options
+     * @return ResponseInterface
+     * @throws HttpException
+     */
+    public function post(string $path, array $parameters = [], array $options = []): ResponseInterface
+    {
+        return $this->request('POST', $path, $parameters, $options);
+    }
+
+    /**
+     * Sends a PUT request and returns the response
+     *
+     * @param string $path
+     * @param array $parameters
+     * @param array $options
+     * @return ResponseInterface
+     * @throws HttpException
+     */
+    public function put(string $path, array $parameters = [], array $options = []): ResponseInterface
+    {
+        return $this->request('PUT', $path, $parameters, $options);
+    }
+
+    /**
+     * Sends a DELETE request and returns the response
+     *
+     * @param string $path
+     * @param array $parameters
+     * @param array $options
+     * @return ResponseInterface
+     * @throws HttpException
+     */
+    public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface
+    {
+        return $this->request('DELETE', $path, $parameters, $options);
+    }
+
     /**
      * Send an HTTP request to the Dolibarr API,
      * and return the decoded content of the response's body
@@ -70,8 +116,9 @@ class ApiRequestService
      * @param array $parameters
      * @param array $options
      * @return ResponseInterface
+     * @throws HttpException
      */
-    protected function request(
+    public function request(
         string $method,
         string $url,
         array $parameters = [],
@@ -83,7 +130,7 @@ class ApiRequestService
         try {
             return $this->client->request($method, $url, $options);
         } catch (HttpExceptionInterface | TransportExceptionInterface $e) {
-            throw new NotFoundHttpException('fetch error', $e, 500);
+            throw new HttpException('Request error : ', $e, 500);
         }
     }
 }

+ 4 - 4
src/Service/Dolibarr/DolibarrAccountCreator.php

@@ -19,25 +19,25 @@ class DolibarrAccountCreator
     ];
 
     public function __construct(
-        private DolibarrService $dolibarrService,
+        private DolibarrApiService $dolibarrApiService,
     )
     {}
 
     public function getDolibarrAccount(int $id): DolibarrAccount {
 
         // Get dolibarr account (society)
-        $accountData = $this->dolibarrService->getSociety($id);
+        $accountData = $this->dolibarrApiService->getSociety($id);
         $dolibarrAccount = $this->createDolibarrAccount($id, $accountData);
 
         // Get active contract and services
-        $contractData = $this->dolibarrService->getActiveContract($dolibarrAccount->getSocId());
+        $contractData = $this->dolibarrApiService->getActiveContract($dolibarrAccount->getSocId());
         if ($contractData !== null) {
             $contract = $this->createDolibarrContract($contractData);
             $dolibarrAccount->setContract($contract);
         }
 
         // get bills
-        $billsData = $this->dolibarrService->getBills($dolibarrAccount->getSocId());
+        $billsData = $this->dolibarrApiService->getBills($dolibarrAccount->getSocId());
         foreach ($billsData as $billData) {
             $bill = $this->createDolibarrBill($billData);
             $dolibarrAccount->addBill($bill);

+ 6 - 19
src/Service/Dolibarr/DolibarrService.php → src/Service/Dolibarr/DolibarrApiService.php

@@ -4,16 +4,18 @@ declare(strict_types=1);
 namespace App\Service\Dolibarr;
 
 use App\Service\ApiRequestService;
+use HttpException;
 use JetBrains\PhpStorm\Pure;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
+use Symfony\Contracts\HttpClient\ResponseInterface;
 
 /**
  * Service d'appel à l'API dolibarr
  *
  * @see https://prod-erp.2iopenservice.com/api/index.php/explorer/
  */
-class DolibarrService extends ApiRequestService
+class DolibarrApiService extends ApiRequestService
 {
     #[Pure]
     function __construct(HttpClientInterface $dolibarr_client)
@@ -26,6 +28,7 @@ class DolibarrService extends ApiRequestService
      *
      * @param int $organizationId
      * @return array
+     * @throws HttpException
      */
     public function getSociety(int $organizationId): array {
         return $this->getJsonContent("thirdparties" , [ "sqlfilters" => "ref_int=" . $organizationId])[0];
@@ -43,7 +46,7 @@ class DolibarrService extends ApiRequestService
                 "contracts",
                 ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId]
             )[0];
-        } catch (NotFoundHttpException) {
+        } catch (HttpException) {
             // /!\ The dolibarr API will return a 404 error if no contract is found...
             return null;
         }
@@ -60,25 +63,9 @@ class DolibarrService extends ApiRequestService
             return $this->getJsonContent(
                 "invoices",
                 ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId]);
-        } catch (NotFoundHttpException) {
+        } catch (HttpException) {
             // /!\ The dolibarr API will return a 404 error if no invoices are found...
             return [];
         }
     }
-
-    /**
-     *
-     * @param $organization
-     * @return void
-     */
-    public function createSociety($organization): void
-    {
-        $body = sprintf(
-            '{"name":"%s","client":"2","code_client":"-1","ref_int":"%s","import_key":"crm"}',
-            $organization->getName(),
-            $organization->getId()
-        );
-
-        $this->request('POST', "api/index.php/thirdparties", [], ['body' => $body]);
-    }
 }

+ 8 - 0
src/Service/Dolibarr/DolibarrSync/DolibarrSyncJob.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Service\Dolibarr\DolibarrSync;
+
+class DolibarrSyncJob
+{
+
+}

+ 8 - 0
src/Service/Dolibarr/DolibarrSync/DolibarrSyncOperation.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Service\Dolibarr\DolibarrSync;
+
+class DolibarrSyncOperation
+{
+
+}

+ 30 - 0
src/Service/Dolibarr/DolibarrSync/DolibarrSyncService.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Service\Dolibarr;
+
+use App\Repository\Organization\OrganizationRepository;
+
+/**
+ * Push the data from the Opentalent DB into the Dolibarr DB, trough both applications
+ * REST APIs.
+ *
+ * ** /!\ This sync is and must stay one-sided: opentalent data => dolibarr db **
+ */
+class DolibarrSyncService
+{
+    public function __construct(
+        private OrganizationRepository $organizationRepository
+    ) {}
+
+    /**
+     * Performs a scan, comparing data from the Opentalent DB and the data returned
+     * by the Dolibarr API
+     *
+     * Returns an array of DolibarrSyncOperation
+     */
+    public function scan() {
+        foreach ($this->organizationRepository->getAll() as $organization) {
+
+        }
+    }
+}

+ 7 - 7
tests/Service/Dolibarr/DolibarrAccountCreatorTest.php

@@ -3,20 +3,20 @@
 namespace App\Tests\Service\Dolibarr;
 
 use App\Service\Dolibarr\DolibarrAccountCreator;
-use App\Service\Dolibarr\DolibarrService;
+use App\Service\Dolibarr\DolibarrApiService;
 use PHPUnit\Framework\TestCase;
 
 class DolibarrAccountCreatorTest extends TestCase
 {
-    private DolibarrService $dolibarrService;
+    private DolibarrApiService $dolibarrApiService;
     private DolibarrAccountCreator $dolibarrAccountCreator;
 
     public function setUp(): void {
-        $this->dolibarrService = $this->getMockBuilder(DolibarrService::class)
+        $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
             ->disableOriginalConstructor()
             ->getMock();
 
-        $this->dolibarrAccountCreator = new DolibarrAccountCreator($this->dolibarrService);
+        $this->dolibarrAccountCreator = new DolibarrAccountCreator($this->dolibarrApiService);
     }
 
     private function getJsonContentFromFixture(string $filename): array {
@@ -25,7 +25,7 @@ class DolibarrAccountCreatorTest extends TestCase
     }
 
     public function testGetDolibarrAccount() {
-        $this->dolibarrService
+        $this->dolibarrApiService
             ->expects($this->once())
             ->method('getSociety')
             ->with(1)
@@ -33,7 +33,7 @@ class DolibarrAccountCreatorTest extends TestCase
                 $this->getJsonContentFromFixture('thirdparty.json')[0]
             );
 
-        $this->dolibarrService
+        $this->dolibarrApiService
             ->expects($this->once())
             ->method('getActiveContract')
             ->with(1726)
@@ -41,7 +41,7 @@ class DolibarrAccountCreatorTest extends TestCase
                 $this->getJsonContentFromFixture('contract.json')[0]
             );
 
-        $this->dolibarrService
+        $this->dolibarrApiService
             ->expects($this->once())
             ->method('getBills')
             ->with(1726)

+ 11 - 11
tests/Service/Dolibarr/DolibarrServiceTest.php → tests/Service/Dolibarr/DolibarrApiServiceTest.php

@@ -2,22 +2,22 @@
 
 namespace App\Tests\Service\Dolibarr;
 
-use App\Service\Dolibarr\DolibarrService;
+use App\Service\Dolibarr\DolibarrApiService;
 use PHPUnit\Framework\TestCase;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 use Symfony\Contracts\HttpClient\ResponseInterface;
 
-class DolibarrServiceTest extends TestCase
+class DolibarrApiServiceTest extends TestCase
 {
     private HttpClientInterface $client;
-    private DolibarrService $dolibarrService;
+    private DolibarrApiService $dolibarrApiService;
 
     public function setUp(): void {
         $this->client = $this->getMockBuilder(HttpClientInterface::class)
             ->disableOriginalConstructor()
             ->getMock();
 
-        $this->dolibarrService = new DolibarrService($this->client);
+        $this->dolibarrApiService = new DolibarrApiService($this->client);
     }
 
     private function getContentFromFixture(string $filename): string {
@@ -26,7 +26,7 @@ class DolibarrServiceTest extends TestCase
     }
 
     /**
-     * @see DolibarrService::getSociety()
+     * @see DolibarrApiService::getSociety()
      */
     public function testGetSociety(): void {
         $responseContent = $this->getContentFromFixture('thirdparty.json');
@@ -42,7 +42,7 @@ class DolibarrServiceTest extends TestCase
             ->with("GET", "thirdparties?sqlfilters=ref_int%3D1")
             ->willReturn($response);
 
-        $data = $this->dolibarrService->getSociety(1);
+        $data = $this->dolibarrApiService->getSociety(1);
 
         $this->assertEquals(
             $data['id'],
@@ -51,7 +51,7 @@ class DolibarrServiceTest extends TestCase
     }
 
     /**
-     * @see DolibarrService::getActiveContract()
+     * @see DolibarrApiService::getActiveContract()
      */
     public function testGetActiveContract(): void {
         $responseContent = $this->getContentFromFixture('contract.json');
@@ -64,17 +64,17 @@ class DolibarrServiceTest extends TestCase
         $this->client
             ->expects($this->once())
             ->method('request')
-            ->with("GET", "contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids%3D1")
+            ->with("GET", "contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids=1")
             ->willReturn($response);
 
         $this->assertEquals(
-            $this->dolibarrService->getActiveContract(1)['socid'],
+            $this->dolibarrApiService->getActiveContract(1)['socid'],
             43
         );
     }
 
     /**
-     * @see DolibarrService::getBills()
+     * @see DolibarrApiService::getBills()
      */
     public function testGetBills(): void {
         $responseContent = $this->getContentFromFixture('bills.json');
@@ -91,7 +91,7 @@ class DolibarrServiceTest extends TestCase
             ->willReturn($response);
 
         $this->assertEquals(
-            $this->dolibarrService->getBills(1)[2]['socid'],
+            $this->dolibarrApiService->getBills(1)[2]['socid'],
             284
         );
     }