Selaa lähdekoodia

add progress bar to dolibarr sync CLI command

Olivier Massot 3 vuotta sitten
vanhempi
commit
460620eabc

+ 14 - 2
src/Commands/DolibarrSyncCommand.php

@@ -6,6 +6,7 @@ use App\Service\Dolibarr\DolibarrSyncService;
 use Symfony\Component\Console\Attribute\AsCommand;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Command\LockableTrait;
+use Symfony\Component\Console\Helper\ProgressBar;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -50,7 +51,16 @@ class DolibarrSyncCommand extends Command
         $t0 = microtime(true);
         $output->writeln("Scanning...");
 
-        $operations = $this->dolibarrSyncService->scan();
+        $progressBar = new ProgressBar($output, 0);
+        $progressCallback = function($i, $total) use ($progressBar) {
+            if (!$progressBar->getMaxSteps() !== $total) {
+                $progressBar->setMaxSteps($total);
+            }
+            $progressBar->setProgress($i);
+        };
+
+        $operations = $this->dolibarrSyncService->scan($progressCallback);
+
         $t1 = microtime(true);
         $output->writeln("Scan lasted " . ($t1 - $t0) . " sec.");
 
@@ -67,7 +77,9 @@ class DolibarrSyncCommand extends Command
         } else {
             $t0 = microtime(true);
             $output->writeln("Executing...");
-            $operations = $this->dolibarrSyncService->execute($operations);
+
+            $operations = $this->dolibarrSyncService->execute($operations, $progressCallback);
+
             $output->writeln(count($operations) . " operations successfully executed");
 
             $t1 = microtime(true);

+ 19 - 2
src/Service/Dolibarr/DolibarrSyncService.php

@@ -54,10 +54,12 @@ class DolibarrSyncService
      *
      * Returns an array of DolibarrSyncOperations
      *
+     * @var callable | null $progressionCallback A callback method for indicating the current progression of the process;
+     *                                           Shall accept two integer arguments: current progression, and total.
      * @return array<BaseRestOperation>
      * @throws Exception
      */
-    public function scan(): array {
+    public function scan($progressionCallback = null): array {
         $this->logger->info("-- Scan started --");
 
         // Index the dolibarr clients by organization ids
@@ -79,6 +81,7 @@ class DolibarrSyncService
 
         // Loop over the Opentalent organizations, and fill up the operations list
         $operations = [];
+        $i = 0; $total = count($dolibarrClientsIndex);
         foreach ($dolibarrClientsIndex as $organizationId => $dolibarrSociety) {
             $dolibarrSociety = $this->sanitizeDolibarrData($dolibarrSociety);
 
@@ -247,6 +250,12 @@ class DolibarrSyncService
                     );
                 }
             }
+
+            // Next society
+            $i++;
+            if ($progressionCallback !== null) {
+                $progressionCallback($i, $total);
+            }
         }
 
         $this->logger->info('Scan done, ' . count($operations) . ' required operations listed');
@@ -267,15 +276,18 @@ class DolibarrSyncService
      * Returns an array of DolibarrSyncOperations
      *
      * @param array<BaseRestOperation> $operations
+     * @var callable | null $progressionCallback A callback method for indicating the current progression of the process;
+     *                                           Shall accept two integer arguments: current progression, and total.
      * @return array<BaseRestOperation>
      * @throws Exception
      */
-    public function execute(array $operations): array
+    public function execute(array $operations, $progressionCallback = null): array
     {
         $this->logger->info('-- Execution started --');
         $this->logger->info(count($operations) . ' operations pending...');
         $done = 0; $errors = 0; $unknown = 0;
 
+        $i = 0; $total = count($operations);
         foreach ($operations as $operation) {
             if ($operation->getStatus() !== BaseRestOperation::STATUS_READY) {
                 // operation has already been treated
@@ -296,6 +308,11 @@ class DolibarrSyncService
             } else {
                 $unknown++;
             }
+
+            $i++;
+            if ($progressionCallback !== null) {
+                $progressionCallback($i, $total);
+            }
         }
 
         $this->logger->info('Execution ended');