Browse Source

various fixes

Olivier Massot 3 years ago
parent
commit
154d26e8ec

+ 4 - 1
src/Commands/DolibarrSyncCommand.php

@@ -80,7 +80,10 @@ class DolibarrSyncCommand extends Command
 
             $operations = $this->dolibarrSyncService->execute($operations, $progressCallback);
 
-            $output->writeln(count($operations) . " operations successfully executed");
+            $successes = count(array_filter($operations, function ($o) { return $o->getStatus() === $o::STATUS_DONE; } ));
+            $errors = count(array_filter($operations, function ($o) { return $o->getStatus() === $o::STATUS_ERROR; } ));
+            $output->writeln($successes . " operations successfully executed");
+            $output->writeln($errors . " errors");
 
             $t1 = microtime(true);
             $output->writeln("Execution lasted " . ($t1 - $t0) . " sec.");

+ 14 - 11
src/Service/Dolibarr/DolibarrSyncService.php

@@ -298,6 +298,7 @@ class DolibarrSyncService
 
             if ($operation->getStatus() === BaseRestOperation::STATUS_ERROR) {
                 $this->logger->error('Error while executing operation : ' . $operation);
+                $this->logger->error(implode("\n", $operation->getChangeLog()));
                 $this->logger->error($operation->getErrorMessage());
                 $errors++;
             } elseif ($operation->getStatus() === BaseRestOperation::STATUS_DONE) {
@@ -587,7 +588,7 @@ class DolibarrSyncService
         );
 
         if (strlen($poste) > 80) {
-            $poste = substr($poste, 0, 77) . '...';
+            $poste = mb_substr($poste, 0, 77, "utf-8") . '...';
         }
         return $poste;
     }
@@ -600,13 +601,19 @@ class DolibarrSyncService
      */
     protected static function formatPhoneNumber(PhoneNumber $phoneNumber): string {
         $phoneUtil = PhoneNumberUtil::getInstance();
-        return $phoneUtil->format($phoneNumber, PhoneNumberFormat::INTERNATIONAL);
+        return str_replace(
+            ' ',
+            '',
+            $phoneUtil->format($phoneNumber, PhoneNumberFormat::INTERNATIONAL)
+        );
     }
 
     /**
      * Returns an array containing the keys/values from the newData array
      * which are absent or different from $initialData
      *
+     * /!\ Sub-arrays shall stay complete and must not be filtered
+     *
      * Because for some fields the dolibarr api returns empty strings even when field is null in DB,
      * we have to consider null and empty-string as equals. As far as we know, this causes no loss of information.
      *
@@ -618,15 +625,11 @@ class DolibarrSyncService
     {
         $result = [];
         foreach ($newData as $field => $value) {
-            if (is_array($value)) {
-                $filteredValue = self::filterDiff($initialData[$field] ?? [], $value);
-                if (!empty($filteredValue)) {
-                    $result[$field] = $filteredValue;
-                }
-            } else {
-                if (($value ?? '') !== ($initialData[$field] ?? '') || !array_key_exists($field, $initialData)) {
-                    $result[$field] = $value;
-                }
+            if (
+                ($value ?? '') !== ($initialData[$field] ?? '') ||
+                !array_key_exists($field, $initialData)
+            ) {
+                $result[$field] = $value;
             }
         }
         return $result;

+ 10 - 1
src/Service/Rest/Operation/BaseRestOperation.php

@@ -6,6 +6,8 @@ namespace App\Service\Rest\Operation;
 use App\Service\Rest\ApiRequestInterface;
 use App\Service\Rest\ApiRequestService;
 use JetBrains\PhpStorm\Pure;
+use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
+use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -62,7 +64,14 @@ abstract class BaseRestOperation
                 return;
             }
             $this->status = self::STATUS_DONE;
-        } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
+        } catch (
+            HttpException |
+            ClientExceptionInterface |
+            TransportExceptionInterface |
+            RedirectionExceptionInterface |
+            ServerExceptionInterface |
+            InvalidArgumentException
+        $e) {
             $this->status = self::STATUS_ERROR;
             $this->errorMessage = '' . $e;
         }

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

@@ -277,7 +277,7 @@ class DolibarrSyncServiceTest extends TestCase
                 'zip : `74300` => `250 329`',
                 'town : `CLUSES` => `Londres`',
                 'email : `` => `email@email.com`',
-                'phone : `+33678403010` => `+33 1 02 03 04 05`',
+                'phone : `+33678403010` => `+33102030405`',
                 'parent : `` => `5086`',
                 "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 3\nNombre d'accès admin : 1`"
             ],
@@ -467,7 +467,7 @@ class DolibarrSyncServiceTest extends TestCase
         $syncService = $this->newDolibarrSyncService();
 
         $this->assertEquals(
-            '+33 1 61 62 63 65',
+            '+33161626365',
             $syncService->getOrganizationPhone($organization)
         );
     }
@@ -499,7 +499,7 @@ class DolibarrSyncServiceTest extends TestCase
         $syncService = $this->newDolibarrSyncService();
 
         $this->assertEquals(
-            '+33 6 61 62 63 65',
+            '+33661626365',
             $syncService->getOrganizationPhone($organization)
         );
     }
@@ -668,14 +668,14 @@ class DolibarrSyncServiceTest extends TestCase
         $phoneUtil = PhoneNumberUtil::getInstance();
         $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
         $this->assertEquals(
-            '+33 1 02 03 04 05',
+            '+33102030405',
             TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
         );
     }
 
     public function testFilterDiff() {
         $this->assertEquals(
-            ['b' => -2, 'c' => ['e' => ['f' => -5]], 'g' => 7],
+            ['b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
             TestableDolibarrSyncService::filterDiff(
                 ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
                 ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],