Ver Fonte

dolibarr sync : post MR review fixes

Olivier Massot há 3 anos atrás
pai
commit
1ecfbd2f97

+ 0 - 1
src/Enum/Access/RoleEnum.php

@@ -8,7 +8,6 @@ use MyCLabs\Enum\Enum;
 /**
  * Role
  * @method static ROLE_ADMIN()
- * @method static DIRECTOR()
  */
 class RoleEnum extends Enum
 {

+ 0 - 22
src/Repository/Access/AccessRepository.php

@@ -136,26 +136,4 @@ class AccessRepository extends ServiceEntityRepository implements UserLoaderInte
 
         return $qb->getQuery()->getArrayResult();
     }
-
-    /**
-     * Returns the data needed
-     *
-     * Used by App\Service\Dolibarr\DolibarrSync\DolibarrSyncService
-     *
-     * @param int $accessId
-     * @return array
-     */
-    public function getDolibarrContactData(int $accessId): array
-    {
-        $qb = $this->createQueryBuilder('access');
-        $qb
-            ->select('access.id', 'organization.id as organization_id', 'function_type.mission')
-            ->innerJoin('access.organization', 'organization')
-            ->innerJoin('access.organizationFunction', 'organization_function')
-            ->innerJoin('organization_function.functionType', 'function_type')
-        ;
-        DateConditions::addDateInPeriodCondition($qb, 'organization_function', date('Y-m-d'));
-
-        return $qb->getQuery()->getArrayResult();
-    }
 }

+ 1 - 1
src/Service/Core/AddressPostalUtils.php

@@ -18,7 +18,7 @@ class AddressPostalUtils
             trim($addressPostal->getStreetAddress()),
             trim($addressPostal->getStreetAddressSecond()),
             trim($addressPostal->getStreetAddressThird())
-        ], static function ($x) { return !empty($x); })
+        ], static function ($addressPart) { return !empty($addressPart); })
         );
     }
 }

+ 8 - 8
src/Service/Rest/ApiRequestInterface.php

@@ -20,7 +20,7 @@ interface ApiRequestInterface
      * @return array
      * @throws HttpException
      */
-    function getJsonContent(string $path, array $parameters = [], array $options = []): array;
+    public function getJsonContent(string $path, array $parameters = [], array $options = []): array;
 
     /**
      * Sends a GET request and returns the response's body
@@ -31,7 +31,7 @@ interface ApiRequestInterface
      * @return string
      * @throws HttpException
      */
-    function getContent(string $path, array $parameters = [], array $options = []): string;
+    public function getContent(string $path, array $parameters = [], array $options = []): string;
 
     /**
      * Sends a GET request and returns the response
@@ -42,7 +42,7 @@ interface ApiRequestInterface
      * @return ResponseInterface
      * @throws HttpException
      */
-    function get(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function get(string $path, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
      * Sends a POST request and returns the response
@@ -53,7 +53,7 @@ interface ApiRequestInterface
      * @return ResponseInterface
      * @throws HttpException
      */
-    function post(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function post(string $path, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
      * Sends a PUT request and returns the response
@@ -64,7 +64,7 @@ interface ApiRequestInterface
      * @return ResponseInterface
      * @throws HttpException
      */
-    function put(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function put(string $path, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
      * Sends a DELETE request and returns the response
@@ -75,10 +75,10 @@ interface ApiRequestInterface
      * @return ResponseInterface
      * @throws HttpException
      */
-    function delete(string $path, array $parameters = [], array $options = []): ResponseInterface;
+    public function delete(string $path, array $parameters = [], array $options = []): ResponseInterface;
 
     /**
-     * Send an HTTP request to the Dolibarr API,
+     * Send an HTTP request to a REST API,
      * and return the decoded content of the response's body
      *
      * @param string $method
@@ -88,7 +88,7 @@ interface ApiRequestInterface
      * @return ResponseInterface
      * @throws HttpException
      */
-    function request(
+    public function request(
         string $method,
         string $url,
         array  $parameters = [],

+ 4 - 4
src/Service/Rest/ApiRequestService.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace App\Service\Rest;
 
-use App\Service\Rest\Operation\BaseRestOperation;
 use App\Service\Utils\UrlBuilder;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@@ -19,7 +18,7 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
  */
 class ApiRequestService implements ApiRequestInterface
 {
-    function __construct(
+    public function __construct(
         protected HttpClientInterface $client
     ) {}
 
@@ -30,9 +29,10 @@ class ApiRequestService implements ApiRequestInterface
      * @param array $options
      * @return array
      * @throws HttpException
+     * @throws \JsonException
      */
     public function getJsonContent(string $path, array $parameters = [], array $options = []): array {
-        return json_decode($this->getContent($path, $parameters, $options), true);
+        return json_decode($this->getContent($path, $parameters, $options), true, 512, JSON_THROW_ON_ERROR);
     }
 
     /**
@@ -108,7 +108,7 @@ class ApiRequestService implements ApiRequestInterface
     }
 
     /**
-     * Send an HTTP request to the Dolibarr API,
+     * Send an HTTP request to a REST API,
      * and return the decoded content of the response's body
      *
      * @param string $method

+ 29 - 20
src/Service/Rest/Operation/BaseRestOperation.php

@@ -26,29 +26,16 @@ abstract class BaseRestOperation
     public const STATUS_ERROR = 3;
 
     protected int $status = self::STATUS_READY;
-    protected string $label;
-    protected string $method;
-    protected string $path;
-    protected array $parameters;
-    protected array $options;
-    protected array $initialData;
     protected string $errorMessage = "";
 
     public function __construct(
-        string $label,
-        string $method,
-        string $path,
-        array  $initialData = [],
-        array  $parameters = [],
-        array  $options = []
-    ) {
-        $this->label = $label;
-        $this->method = $method;
-        $this->path = $path;
-        $this->initialData = $initialData;
-        $this->parameters = $parameters;
-        $this->options = $options;
-    }
+        protected string $label,
+        protected string $method,
+        protected string $path,
+        protected array  $initialData = [],
+        protected array  $parameters = [],
+        protected array  $options = []
+    ) {}
 
     /**
      * Execute the operation and update its status according to the result
@@ -161,4 +148,26 @@ abstract class BaseRestOperation
     public function __toString(): string {
         return $this->getMethod() . " " . $this->getPath();
     }
+
+    protected static function getRecursiveChangeLog(array $initialData, array $newData, string $prefix = ""): array {
+        $messages = [];
+        foreach ($newData as $field => $newValue) {
+            $fieldLabel = $prefix ? $prefix . '.' . $field : $field;
+
+            if (is_array($newValue)) {
+                array_push(
+                    $messages,
+                    ...self::getRecursiveChangeLog(
+                    $initialData[$field] ?? [],
+                    $newValue,
+                    $fieldLabel)
+                );
+            } else if (!array_key_exists($field, $initialData)) {
+                $messages[] = $fieldLabel . ' : (new) => `' . $newValue . '`';
+            } else if ($newValue !== $initialData[$field]) {
+                $messages[] = $fieldLabel . ' : `' . $initialData[$field] . '` => `' . $newValue . '`';
+            }
+        }
+        return $messages;
+    }
 }

+ 22 - 18
src/Service/Rest/Operation/CreateOperation.php

@@ -10,13 +10,23 @@ use JetBrains\PhpStorm\Pure;
  */
 class CreateOperation extends BaseRestOperation
 {
-    protected string $entity;
-    protected array $data;
-
+    /**
+     * @param string $label A label for the operation
+     * @param string $entityName The name of the entity to update. This will be used in the path of the request.
+     * @param array $data The data to update, will be post as Json within the request.
+     * @param array $parameters
+     * @param array $options
+     */
     #[Pure]
-    public function __construct(string $label, string $entityName, array $data, array $parameters = [], array $options = []) {
-        $this->data = $data;
+    public function __construct(
+        protected string $label,
+        protected string $entityName,
+        protected array $data,
+        protected array $parameters = [],
+        protected array $options = []
+    ) {
         $options['json'] = $this->data;
+
         parent::__construct(
             $label,
             'POST',
@@ -25,15 +35,14 @@ class CreateOperation extends BaseRestOperation
             $parameters,
             $options
         );
-        $this->entity = $entityName;
     }
 
     /**
      * @return string
      */
-    public function getEntity(): string
+    public function getEntityName(): string
     {
-        return $this->entity;
+        return $this->entityName;
     }
 
     /**
@@ -51,17 +60,12 @@ class CreateOperation extends BaseRestOperation
      */
     public function getChangeLog(): array {
         $messages = [
-            '[POST ' . $this->entity . ']'
+            '[POST ' . $this->entityName . ']'
         ];
-        foreach ($this->data as $field => $newValue) {
-            if (is_array($newValue)) {
-                foreach ($newValue as $subField => $newSubValue) {
-                    $messages[] = $field . '.' . $subField . ' : `' . $newSubValue . '`';
-                }
-            } else {
-                $messages[] = $field . ' : `' . $newValue . '`';
-            }
-        }
+        array_push(
+            $messages,
+            ...self::getRecursiveChangeLog([], $this->data)
+        );
         return $messages;
     }
 }

+ 17 - 12
src/Service/Rest/Operation/DeleteOperation.php

@@ -10,13 +10,21 @@ use JetBrains\PhpStorm\Pure;
  */
 class DeleteOperation extends BaseRestOperation
 {
-    protected string $entity;
-    protected int $id;
-
+    /**
+     * @param string $label A label for the operation
+     * @param string $entityName The name of the entity to update. This will be used in the path of the request.
+     * @param int $id
+     * @param array $initialData The data of the existing object, before the update
+     * @param array $options
+     */
     #[Pure]
-    public function __construct(string $label, string $entityName, array $initialData, array $options = []) {
-        $id = (int)$initialData['id'];
-
+    public function __construct(
+        protected string $label,
+        protected string $entityName,
+        protected int $id,
+        protected array $initialData = [],
+        protected array $options = []
+    ) {
         parent::__construct(
             $label,
             'DELETE',
@@ -25,17 +33,14 @@ class DeleteOperation extends BaseRestOperation
             [],
             $options
         );
-
-        $this->entity = $entityName;
-        $this->id = $id;
     }
 
     /**
      * @return string
      */
-    public function getEntity(): string
+    public function getEntityName(): string
     {
-        return $this->entity;
+        return $this->entityName;
     }
 
     protected function getExpectedResult(): ?array {
@@ -49,7 +54,7 @@ class DeleteOperation extends BaseRestOperation
      */
     public function getChangeLog(): array {
         return [
-            '[DELETE ' . $this->entity . '/' . $this->id . ']'
+            '[DELETE ' . $this->entityName . '/' . $this->id . ']'
         ];
     }
 }

+ 16 - 36
src/Service/Rest/Operation/UpdateOperation.php

@@ -4,23 +4,12 @@ declare(strict_types=1);
 namespace App\Service\Rest\Operation;
 
 use JetBrains\PhpStorm\Pure;
-use Symfony\Component\HttpKernel\Exception\HttpException;
-use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
-use Symfony\Contracts\HttpClient\ResponseInterface;
 
 /**
  * A single update operation (a PUT request)
  */
 class UpdateOperation extends BaseRestOperation
 {
-    protected string $entity;
-    protected int $id;
-    protected array $data;
-
     /**
      * @param string $label A label for the operation
      * @param string $entityName The name of the entity to update. This will be used in the path of the request.
@@ -31,8 +20,15 @@ class UpdateOperation extends BaseRestOperation
      * @param array $options
      */
     #[Pure]
-    public function __construct(string $label, string $entityName, int $id, array $data, array $initialData = [], array $parameters = [], array $options = []) {
-        $this->data = $data;
+    public function __construct(
+        protected string $label,
+        protected string $entityName,
+        protected int $id,
+        protected array $data,
+        protected array $initialData = [],
+        protected array $parameters = [],
+        protected array $options = []
+    ) {
         $options['json'] = $this->data;
 
         parent::__construct(
@@ -43,17 +39,14 @@ class UpdateOperation extends BaseRestOperation
             $parameters,
             $options
         );
-
-        $this->entity = $entityName;
-        $this->id = $id;
     }
 
     /**
      * @return string
      */
-    public function getEntity(): string
+    public function getEntityName(): string
     {
-        return $this->entity;
+        return $this->entityName;
     }
 
     /**
@@ -71,25 +64,12 @@ class UpdateOperation extends BaseRestOperation
      */
     public function getChangeLog(): array {
         $messages = [
-            '[PUT ' . $this->entity . '/' . $this->id . ']'
+            '[PUT ' . $this->entityName . '/' . $this->id . ']'
         ];
-        foreach ($this->data as $field => $newValue) {
-            if (!array_key_exists($field, $this->initialData)) {
-                $messages[] = $field . '.' . $field . ' : ? => `' . $newValue . '`';
-            } else if (is_array($newValue)) {
-                foreach ($newValue as $subField => $newSubValue) {
-                    if (!array_key_exists($subField, $this->initialData[$field])) {
-                        $messages[] = $field . '.' . $subField . ' : (new sub-key) `' . $newSubValue . '`';
-                    }
-                    else if ($newSubValue !== $this->initialData[$field][$subField]) {
-                        $messages[] = $field . '.' . $subField . ' : `'. $this->initialData[$field][$subField] . '` => `' . $newSubValue . '`';
-                    }
-                }
-            } else if ($newValue !== $this->initialData[$field]) {
-                $messages[] = $field . ' : `' . $this->initialData[$field] . '` => `' . $newValue . '`';
-            }
-        }
+        array_push(
+            $messages,
+            ...self::getRecursiveChangeLog($this->initialData, $this->data)
+        );
         return $messages;
     }
-
 }

+ 10 - 10
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -299,16 +299,16 @@ class DolibarrSyncServiceTest extends TestCase
         $this->assertEquals(
             [
                 '[POST contacts]',
-                'civility_code : ``',
-                'lastname : `Watson`',
-                'firstname : `John`',
-                'email : ``',
-                'phone_pro : ``',
-                'phone_mobile : ``',
-                'poste : ``',
-                'statut : `1`',
-                'array_options.options_2iopen_person_id : `1000`',
-                'socid : `1726`'
+                'civility_code : (new) => ``',
+                'lastname : (new) => `Watson`',
+                'firstname : (new) => `John`',
+                'email : (new) => ``',
+                'phone_pro : (new) => ``',
+                'phone_mobile : (new) => ``',
+                'poste : (new) => ``',
+                'statut : (new) => `1`',
+                'array_options.options_2iopen_person_id : (new) => `1000`',
+                'socid : (new) => `1726`'
             ],
             $operations[2]->getChangeLog()
         );

+ 5 - 5
tests/Service/Rest/Operation/CreateOperationTest.php

@@ -13,7 +13,7 @@ class CreateOperationTest extends TestCase
         );
 
         $this->assertEquals('POST', $operation->getMethod());
-        $this->assertEquals('dinosaur', $operation->getEntity());
+        $this->assertEquals('dinosaur', $operation->getEntityName());
         $this->assertEquals('dinosaur', $operation->getPath());
         $this->assertEquals(['name' => 'denver'], $operation->getData());
 
@@ -30,10 +30,10 @@ class CreateOperationTest extends TestCase
         $this->assertEquals(
             [
                 '[POST dinosaur]',
-                'name : `denver`',
-                'color : `green`',
-                'objects.glasses : `pink`',
-                'objects.guitar : `electric`',
+                'name : (new) => `denver`',
+                'color : (new) => `green`',
+                'objects.glasses : (new) => `pink`',
+                'objects.guitar : (new) => `electric`',
             ],
             $operation->getChangeLog()
         );

+ 3 - 3
tests/Service/Rest/Operation/DeleteOperationTest.php

@@ -10,11 +10,11 @@ class DeleteOperationTest extends TestCase
         $operation = new DeleteOperation(
             'Delete a dinosaur',
             'dinosaur',
-            ['id' => 1]
+            1
         );
 
         $this->assertEquals('DELETE', $operation->getMethod());
-        $this->assertEquals('dinosaur', $operation->getEntity());
+        $this->assertEquals('dinosaur', $operation->getEntityName());
         $this->assertEquals('dinosaur/1', $operation->getPath());
 
         $this->assertEquals('DELETE dinosaur/1', (string)$operation);
@@ -24,7 +24,7 @@ class DeleteOperationTest extends TestCase
         $operation = new DeleteOperation(
             'Delete a dinosaur',
             'dinosaur',
-            ['id' => 1]
+            1
         );
 
         $this->assertEquals(

+ 5 - 3
tests/Service/Rest/Operation/UpdateOperationTest.php

@@ -16,7 +16,7 @@ class UpdateOperationTest extends TestCase
         );
 
         $this->assertEquals('PUT', $operation->getMethod());
-        $this->assertEquals('dinosaur', $operation->getEntity());
+        $this->assertEquals('dinosaur', $operation->getEntityName());
         $this->assertEquals('dinosaur/1', $operation->getPath());
         $this->assertEquals(['weight' => 1600], $operation->getInitialData());
         $this->assertEquals(['weight' => 1800], $operation->getData());
@@ -29,8 +29,8 @@ class UpdateOperationTest extends TestCase
             'Update a dinosaur',
             'dinosaur',
             1,
-            ['weight' => 1800, 'attrs' => ['vision' => 'movement-based', 'teeth' => '99']],
-            ['weight' => 1600, 'attrs' => ['vision' => 'movement-based', 'teeth' => '100']]
+            ['weight' => 1800, 'attrs' => ['vision' => 'movement-based', 'teeth' => '99', 'adn' => ['1' => 'b'], 'color' => 'green']],
+            ['weight' => 1600, 'attrs' => ['vision' => 'movement-based', 'teeth' => '100', 'adn' => ['1' => 'a']]]
         );
 
         $this->assertEquals(
@@ -38,6 +38,8 @@ class UpdateOperationTest extends TestCase
                 '[PUT dinosaur/1]',
                 'weight : `1600` => `1800`',
                 'attrs.teeth : `100` => `99`',
+                'attrs.adn.1 : `a` => `b`',
+                'attrs.color : (new) => `green`',
             ],
             $operation->getChangeLog()
         );