diff --git a/src/UnicaenSynchro/Controller/SynchronisationConsoleController.php b/src/UnicaenSynchro/Controller/SynchronisationConsoleController.php index ea4d1c009d81b92f69377c3720ec347bfd542718..1be2b615d1f2ab8e90fcc9e7a936da6a2d1d311d 100644 --- a/src/UnicaenSynchro/Controller/SynchronisationConsoleController.php +++ b/src/UnicaenSynchro/Controller/SynchronisationConsoleController.php @@ -5,17 +5,18 @@ namespace UnicaenSynchro\Controller; use Unicaen\Console\Controller\AbstractConsoleController; use UnicaenSynchro\Service\Synchronisation\SynchronisationServiceAwareTrait; -class SynchronisationConsoleController extends AbstractConsoleController { +class SynchronisationConsoleController extends AbstractConsoleController +{ use SynchronisationServiceAwareTrait; private array $configs; - public function setConfigs(array $configs) : void + public function setConfigs(array $configs): void { $this->configs = $configs; } - public function synchroniserAllAction() : string + public function synchroniserAllAction(): string { // todo utiliser l'ordre pour executer les synchros dans le bon sens $jobs = $this->configs; @@ -26,7 +27,7 @@ class SynchronisationConsoleController extends AbstractConsoleController { return "done!\n"; } - public function synchroniserAction() : string + public function synchroniserAction(): string { $request = $this->getRequest(); $name = $request->getParam('name'); diff --git a/src/UnicaenSynchro/Service/Synchronisation/SynchronisationService.php b/src/UnicaenSynchro/Service/Synchronisation/SynchronisationService.php index 22bd7febd394a25dbdc55301c6bb5b7cac9f5e92..008eb24efb6548a66d8f9f7561342e4b4d272ccb 100644 --- a/src/UnicaenSynchro/Service/Synchronisation/SynchronisationService.php +++ b/src/UnicaenSynchro/Service/Synchronisation/SynchronisationService.php @@ -3,6 +3,7 @@ namespace UnicaenSynchro\Service\Synchronisation; use DateTime; +use Exception; use UnicaenSynchro\Service\SqlHelper\SqlHelperServiceAwareTrait; class SynchronisationService { @@ -47,89 +48,107 @@ class SynchronisationService { public function synchronise(string $name) : string { - echo "Synchronisation [".$name."]\n"; flush(); - $debut = new DateTime(); - echo "Debut: ".$debut->format('d/m/y H:i:s:u')."\n"; flush(); - - $correspondance = $this->getFromConfig($name, 'correspondance'); - $orm_source = $this->entityManagers[$this->getFromConfig($name, 'orm_source')]; - $orm_destination = $this->entityManagers[$this->getFromConfig($name, 'orm_destination')]; - $table_source = $this->getFromConfig($name, 'table_source'); - $table_destination = $this->getFromConfig($name, 'table_destination'); - $id_source = $this->getFromConfig($name, 'id'); - $source = $this->getFromConfig($name, 'source'); - $id_destination = $correspondance[$id_source]; - - $data_source = $this->getSqlHelperService()->fetch($orm_source, $table_source, $correspondance, 'source', $id_source); - echo count($data_source). " entrées dans les données sources.\n"; flush(); - $data_destination = $this->getSqlHelperService()->fetch($orm_destination, $table_destination, $correspondance, 'destination', $id_destination); - $data_destination_on = []; $data_destination_off = []; - foreach ($data_destination as $item) { - if ($item['deleted_on'] !== null) $data_destination_off[] = $item; else $data_destination_on[] = $item; - } - echo count($data_destination_on) . "(~". count($data_destination_off).")". " entrées dans les données cibles actives.\n"; flush(); - - $read = new DateTime(); - echo "Lecture: ".$read->format('d/m/y H:i:s:u'). "(" . ($read->diff($debut))->format('%H:%m:%s:%F').")\n"; flush(); - - //check for removal - $nbRetrait = 0; -// $texte_retrait = ""; - foreach ($data_destination as $id => $item) { - if ($item['deleted_on'] === null AND !isset($data_source[$id])) { - $nbRetrait++; -// $texte_retrait .= "Retrait de ".$id." des données destination.\n"; - $this->getSqlHelperService()->delete($orm_destination, $table_destination, $id); + try { + echo "Synchronisation [" . $name . "]\n"; + flush(); + $debut = new DateTime(); + echo "Debut: " . $debut->format('d/m/y H:i:s:u') . "\n"; + flush(); + + $correspondance = $this->getFromConfig($name, 'correspondance'); + $orm_source = $this->entityManagers[$this->getFromConfig($name, 'orm_source')]; + $orm_destination = $this->entityManagers[$this->getFromConfig($name, 'orm_destination')]; + $table_source = $this->getFromConfig($name, 'table_source'); + $table_destination = $this->getFromConfig($name, 'table_destination'); + $id_source = $this->getFromConfig($name, 'id'); + $source = $this->getFromConfig($name, 'source'); + $id_destination = $correspondance[$id_source]; + + $data_source = $this->getSqlHelperService()->fetch($orm_source, $table_source, $correspondance, 'source', $id_source); + echo count($data_source) . " entrées dans les données sources.\n"; + flush(); + $data_destination = $this->getSqlHelperService()->fetch($orm_destination, $table_destination, $correspondance, 'destination', $id_destination); + $data_destination_on = []; + $data_destination_off = []; + foreach ($data_destination as $item) { + if ($item['deleted_on'] !== null) $data_destination_off[] = $item; else $data_destination_on[] = $item; + } + echo count($data_destination_on) . "(~" . count($data_destination_off) . ")" . " entrées dans les données cibles actives.\n"; + flush(); + + $read = new DateTime(); + echo "Lecture: " . $read->format('d/m/y H:i:s:u') . "(" . ($read->diff($debut))->format('%H:%m:%s:%F') . ")\n"; + flush(); + + //check for removal + $nbRetrait = 0; + // $texte_retrait = ""; + foreach ($data_destination as $id => $item) { + if ($item['deleted_on'] === null and !isset($data_source[$id])) { + $nbRetrait++; + // $texte_retrait .= "Retrait de ".$id." des données destination.\n"; + $this->getSqlHelperService()->delete($orm_destination, $table_destination, $id); + } } - } - echo "#Retrait: ".$nbRetrait."\n"; flush(); -// $log .= $texte_retrait; - - //check for adding - $nbAjout = 0; -// $texte_ajout = ""; - foreach ($data_source as $id => $item) { - if (!isset($data_destination[$id])) { - $nbAjout++; -// $texte_ajout .= "Ajout de ".$id." des données sources.\n"; - $this->getSqlHelperService()->insert($orm_destination, $table_destination, $item, $correspondance, $source); + echo "#Retrait: " . $nbRetrait . "\n"; + flush(); + // $log .= $texte_retrait; + + //check for adding + $nbAjout = 0; + // $texte_ajout = ""; + foreach ($data_source as $id => $item) { + if (!isset($data_destination[$id])) { + $nbAjout++; + // $texte_ajout .= "Ajout de ".$id." des données sources.\n"; + $this->getSqlHelperService()->insert($orm_destination, $table_destination, $item, $correspondance, $source); + } } - } - echo "#Ajout: ".$nbAjout."\n"; flush(); -// $log .= $texte_ajout; - - //check for restauration - $nbRestauration = 0; -// $texte_restauration = ""; - foreach ($data_source as $id => $item) { - if (isset($data_destination[$id]) AND $data_destination[$id]["deleted_on"] !== null) { - $nbRestauration++; -// $texte_restauration .= "Restauration de ".$id." des données destinations.\n"; - $this->getSqlHelperService()->restore($orm_destination, $table_destination, $id); + echo "#Ajout: " . $nbAjout . "\n"; + flush(); + // $log .= $texte_ajout; + + //check for restauration + $nbRestauration = 0; + // $texte_restauration = ""; + foreach ($data_source as $id => $item) { + if (isset($data_destination[$id]) and $data_destination[$id]["deleted_on"] !== null) { + $nbRestauration++; + // $texte_restauration .= "Restauration de ".$id." des données destinations.\n"; + $this->getSqlHelperService()->restore($orm_destination, $table_destination, $id); + } } - } - echo "#Restauration: ".$nbRestauration."\n"; flush(); -// $log .= $texte_restauration; - - //check for modification - $nbModification = 0; -// $texte_modication = ""; - foreach ($data_source as $id => $item) { - if (isset($data_destination[$id]) AND $this->checkDifferences($item, $data_destination[$id], $correspondance, $source)) { - $nbModification++; -// $texte_modication .= "Modif de ".$id." des données sources.\n"; - $this->getSqlHelperService()->update($orm_destination, $table_destination, $item, $correspondance, $id, $source); + echo "#Restauration: " . $nbRestauration . "\n"; + flush(); + // $log .= $texte_restauration; + + //check for modification + $nbModification = 0; + // $texte_modication = ""; + foreach ($data_source as $id => $item) { + if (isset($data_destination[$id]) and $this->checkDifferences($item, $data_destination[$id], $correspondance, $source)) { + $nbModification++; + // $texte_modication .= "Modif de ".$id." des données sources.\n"; + $this->getSqlHelperService()->update($orm_destination, $table_destination, $item, $correspondance, $id, $source); + } } + echo "#Modification: " . $nbModification . "\n"; + flush(); + // $log .= $texte_modication; + + $fin = new DateTime(); + echo "Fin: " . $fin->format('d/m/y H:i:s:u') . "\n"; + + echo "Durée de la synchronisation: " . ($fin->diff($debut))->format('%H:%m:%s:%F') . "\n"; + + return ""; + } catch (Exception $e) { + do { + echo "\033[31m".$e->getMessage() . "\033[0m\n"; + $e = $e->getPrevious(); + } while ($e !== null); + die(); } - echo "#Modification: ".$nbModification."\n"; flush(); -// $log .= $texte_modication; - - $fin = new DateTime(); - echo "Fin: ".$fin->format('d/m/y H:i:s:u')."\n"; - - echo "Durée de la synchronisation: " . ($fin->diff($debut))->format('%H:%m:%s:%F') . "\n"; - - return ""; } } \ No newline at end of file