From 8b1a5b41ef4a0bf1dee256e46765ac8aac9e4558 Mon Sep 17 00:00:00 2001
From: Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
Date: Mon, 7 Feb 2022 16:14:09 +0100
Subject: [PATCH] =?UTF-8?q?Possibilit=C3=A9=20d'ordonner=20les=20imports/s?=
 =?UTF-8?q?ynchros.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CHANGELOG.md                                  |  2 ++
 src/UnicaenDbImport/Config/Config.php         |  8 ++++++
 src/UnicaenDbImport/Domain/Import.php         | 17 +++++++++--
 .../Domain/ImportInterface.php                |  7 +++++
 src/UnicaenDbImport/Domain/Synchro.php        | 21 +++++++++++---
 .../Domain/SynchroInterface.php               | 28 +++++++++++++++++++
 6 files changed, 77 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 094f667..8de3650 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,8 @@ Changelog
 
 4.0.2
 -----
+- Possibilité d'ordonner les imports/synchros.
+- Possibilité d'afficher les logs d'import/synchro en live (avec unicaen/livelog).
 - Possibilité de sélectionner les imports/synchros à lancer.
 - Ajout d'un témoin indiquant si un problème a été rencontré pendant l'exécution.
 - [FIX] ApiService : prise en compte des paramètres de config 'timeout' et 'connect_timeout'.
diff --git a/src/UnicaenDbImport/Config/Config.php b/src/UnicaenDbImport/Config/Config.php
index 9aa3699..aaefe72 100644
--- a/src/UnicaenDbImport/Config/Config.php
+++ b/src/UnicaenDbImport/Config/Config.php
@@ -390,6 +390,10 @@ class Config
      */
     public function setImports(array $imports): self
     {
+        usort($imports, function(ImportInterface $a, ImportInterface $b) {
+            return $a->getOrder() <=> $b->getOrder();
+        });
+
         $this->imports = $imports;
 
         return $this;
@@ -424,6 +428,10 @@ class Config
      */
     public function setSynchros(array $synchros): self
     {
+        usort($synchros, function(SynchroInterface $a, SynchroInterface $b) {
+            return $a->getOrder() <=> $b->getOrder();
+        });
+
         $this->synchros = $synchros;
 
         return $this;
diff --git a/src/UnicaenDbImport/Domain/Import.php b/src/UnicaenDbImport/Domain/Import.php
index 234c506..26c3bba 100644
--- a/src/UnicaenDbImport/Domain/Import.php
+++ b/src/UnicaenDbImport/Domain/Import.php
@@ -42,13 +42,18 @@ class Import implements ImportInterface
      */
     protected function validateConfig()
     {
-        (new AssertionChain($this->config->get($key = 'source'), "Une instance de " . SourceInterface::class . " est requise pour la clé suivante: $key"))
+        (new AssertionChain($this->config->get($key = 'source'), "Une instance de " . SourceInterface::class . " est requise pour la clé suivante: $key (import {$this})"))
             ->notEmpty()
             ->isInstanceOf(SourceInterface::class);
 
-        (new AssertionChain($this->config->get($key = 'destination'), "Une instance de " . DestinationInterface::class . " est requise pour la clé suivante: $key"))
+        (new AssertionChain($this->config->get($key = 'destination'), "Une instance de " . DestinationInterface::class . " est requise pour la clé suivante: $key (import {$this})"))
             ->notEmpty()
             ->isInstanceOf(DestinationInterface::class);
+
+        if ($this->config->offsetExists($key = 'order')) {
+            (new AssertionChain($this->config->get('order'), "Un entier est requis pour la clé suivante: $key (import {$this})"))
+                ->integer();
+        }
     }
 
     /**
@@ -86,4 +91,12 @@ class Import implements ImportInterface
     {
         return $this->config->get('destination');
     }
+
+    /**
+     * @return null|int
+     */
+    public function getOrder(): ?int
+    {
+        return $this->config->get('order');
+    }
 }
\ No newline at end of file
diff --git a/src/UnicaenDbImport/Domain/ImportInterface.php b/src/UnicaenDbImport/Domain/ImportInterface.php
index af84f7c..23619fb 100644
--- a/src/UnicaenDbImport/Domain/ImportInterface.php
+++ b/src/UnicaenDbImport/Domain/ImportInterface.php
@@ -24,4 +24,11 @@ interface ImportInterface
      * @return DestinationInterface
      */
     public function getDestination(): DestinationInterface;
+
+    /**
+     * Retourne le numéro d'ordre éventuel de cet import.
+     *
+     * @return null|int
+     */
+    public function getOrder(): ?int;
 }
\ No newline at end of file
diff --git a/src/UnicaenDbImport/Domain/Synchro.php b/src/UnicaenDbImport/Domain/Synchro.php
index cb4efb1..2f729f6 100644
--- a/src/UnicaenDbImport/Domain/Synchro.php
+++ b/src/UnicaenDbImport/Domain/Synchro.php
@@ -49,20 +49,25 @@ class Synchro implements SynchroInterface
      */
     protected function validateConfig()
     {
-        (new AssertionChain($this->config->get($key = 'source'), "Une instance de " . SourceInterface::class . " est requise pour la clé suivante: $key"))
+        (new AssertionChain($this->config->get($key = 'source'), "Une instance de " . SourceInterface::class . " est requise pour la clé suivante: $key (synchro {$this})"))
             ->notEmpty()
             ->isInstanceOf(SourceInterface::class);
 
-        (new AssertionChain($this->config->get($key = 'destination'), "Une instance de " . DestinationInterface::class . " est requise pour la clé suivante: $key"))
+        (new AssertionChain($this->config->get($key = 'destination'), "Une instance de " . DestinationInterface::class . " est requise pour la clé suivante: $key (synchro {$this})"))
             ->notEmpty()
             ->isInstanceOf(DestinationInterface::class);
 
         if ($this->config->offsetExists($key = 'intermediate_table')) {
-            $intermediateTable = $this->config->get($key = 'intermediate_table');
-            (new AssertionChain($intermediateTable, "Une string non vide est requise pour la clé facultative suivante: $key"))
+            $intermediateTable = $this->config->get($key);
+            (new AssertionChain($intermediateTable, "Une string non vide est requise pour la clé facultative suivante: $key (synchro {$this})"))
                 ->notEmpty()
                 ->string();
         }
+
+        if ($this->config->offsetExists($key = 'order')) {
+            (new AssertionChain($this->config->get('order'), "Un entier est requis pour la clé suivante: $key (synchro {$this})"))
+                ->integer();
+        }
     }
 
     /**
@@ -101,6 +106,14 @@ class Synchro implements SynchroInterface
         return $this->config->get('destination');
     }
 
+    /**
+     * @return null|int
+     */
+    public function getOrder(): ?int
+    {
+        return $this->config->get('order');
+    }
+
     /**
      * @inheritDoc
      */
diff --git a/src/UnicaenDbImport/Domain/SynchroInterface.php b/src/UnicaenDbImport/Domain/SynchroInterface.php
index f1ed332..f844678 100644
--- a/src/UnicaenDbImport/Domain/SynchroInterface.php
+++ b/src/UnicaenDbImport/Domain/SynchroInterface.php
@@ -4,6 +4,34 @@ namespace UnicaenDbImport\Domain;
 
 interface SynchroInterface extends ImportInterface
 {
+    /**
+     * Retourne le petit nom de cette synchro.
+     *
+     * @return string
+     */
+    public function getName(): string;
+
+    /**
+     * Retourne l'instance de la source de cette synchro.
+     *
+     * @return SourceInterface
+     */
+    public function getSource(): SourceInterface;
+
+    /**
+     * Retourne l'instance de la destination de cette synchro.
+     *
+     * @return DestinationInterface
+     */
+    public function getDestination(): DestinationInterface;
+
+    /**
+     * Retourne le numéro d'ordre éventuel de cette synchro.
+     *
+     * @return null|int
+     */
+    public function getOrder(): ?int;
+
     /**
      * Indique si cette synchro nécessite l'utilisation d'une table intermédiaire.
      *
-- 
GitLab