/** * Importe tous les hotels SEH modifiés depuis $sinceDate. * Utilise le webservice Artsys 'flux/modified' pour obtenir la liste des hotels modifiés puis appelle la fonction importAction pour chacun des hotels. * * @param string $sinceDate Date au format YYYY-MM-DD * * @return mixed En mode console retourne une chaine vide, en mode Web retourne un tableau avec les messages à afficher dans le template * * @Route("/modified/{sinceDate}", name="modifiedArtsysHotels") * @Template() */ public function modifiedHotelsAction($sinceDate, $log = false) { if (PHP_SAPI == 'cli') { $this->cli = true; } // Récupération du DataSource $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('BigfootImportBundle:DataSource')->findOneby(array('slug' => $this->container->getParameter('artsys_bundle.datasource_slug'))); // Initialisation du client $detailsClient = new Client(); $detailsClient->init($entity->getProtocol(), $entity->getDomain(), $entity->getPort()); $detailsClient->setAuth($entity->getUsername(), $entity->getPassword()); // Récupération des Ids hotel $xml = $detailsClient->get('flux/modified?since=' . $sinceDate, false); $progress = false; if ($log) { $progressFile = sprintf('%s/../web/uploads/import/progress-%s', $this->get('kernel')->getRootDir(), $sinceDate); if (file_exists($progressFile) and $progress = file_get_contents($progressFile)) { if (strpos($xml, sprintf('codeAdherent="%s"', $progress)) === false) { unlink($progressFile); $progress = false; } } } // Création de l'objet XML $hotels = simplexml_load_string($xml); $nbHotel = count($hotels); if ($nbHotel == 0) { $this->printMessage(sprintf("Aucun hotel modifié depuis le %s.\n", $sinceDate)); } else { $this->printMessage(sprintf("Import de %s hotel(s).\n", $nbHotel)); if ($log) { $this->logger->info(sprintf('>>>> Importing %s hotels (from %s)', $nbHotel, $sinceDate)); } } $averageTime = 0; $nbDone = 0; $import = !$progress; foreach ($hotels as $hotel) { $hotelArtsysId = $hotel->xpath('@codeAdherent'); $hotelArtsysId = (string) $hotelArtsysId[0]; if (!$log or $import or $progress == $hotelArtsysId) { $hotelPath = $hotel->xpath('@codeAdherent'); $codeAdherent = (string) $hotelPath[0]; if ($codeAdherent != "") { try { $this->printMessage(sprintf("Import de l'hôtel avec le code adhérent : %s.\n", $codeAdherent)); $time = microtime(true); $this->importAction($codeAdherent, $log); $time = microtime(true) - $time; $averageTime += $time; if ($log) { $this->logger->info(sprintf('Imported hotel %s (%s / %s) successfully in %s seconds total (%s seconds average total)', $codeAdherent, $nbDone + 1, $nbHotel, $time, $averageTime / ($nbDone + 1))); } } catch (\Exception $e) { $time = microtime(true) - $time; $averageTime += $time; if ($log) { $this->logger->error(sprintf('Error while importing hotel %s (%s) in %s seconds (%s seconds average)', $codeAdherent, $e->getMessage(), $time, $averageTime / ($nbDone + 1))); } $this->printMessage(sprintf("Erreur sur l'import de l'hôtel avec le code adhérent %s.\n", $codeAdherent)); } $nbDone++; } if ($log) { $import = true; if (!file_exists($progressFile)) { $fs = $this->get('filesystem'); $fs->mkdir(dirname($progressFile)); $fs->touch($progressFile); } file_put_contents($progressFile, $hotelArtsysId); } } } if ($log && $nbHotel > 0) { $this->logger->info(sprintf('>>>> Imported %s hotels in %s seconds (average : %s)', $nbHotel, $averageTime, $averageTime / $nbHotel)); unlink($progressFile); } if (!$this->cli) { return array('messages' => $this->messages); } return ""; }
/** * @return Client */ public function getArtsysClient() { $em = $this->em; /** @var DataSourceRepository $dataSourceRepo */ $dataSourceRepo = $em->getRepository('BigfootImportBundle:DataSource'); /** @var DataSource $dataSource */ $dataSource = $dataSourceRepo->findOneby(array('slug' => $this->dataSourceSlug)); $detailsClient = new Client(); $detailsClient->init($dataSource->getProtocol(), $dataSource->getDomain(), $dataSource->getPort()); $detailsClient->setAuth($dataSource->getUsername(), $dataSource->getPassword()); return $detailsClient; }