Skip to content

Commit

Permalink
getExportFiles no longer returns Finder (path instead)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGranger committed Nov 6, 2023
1 parent 68dc03d commit 536daf2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 50 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"require": {
"php": "^8.0",
"guzzlehttp/guzzle-services": "^1.3",
"symfony/finder": "^5.4.0",
"justinrainbow/json-schema": "^5.2",
"apidae-tourisme/apidae-sit-schemas": "^3.1"
},
Expand Down
52 changes: 11 additions & 41 deletions examples/export.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

$config = [];
require __DIR__ . "/requires.inc.php";

Expand All @@ -12,21 +9,21 @@
* In a classical situation, Apidae should send an http request after every new export :
* - Nightly, daily, weekly or monthly depending on the project configuration
* - or every time user click on "Export exceptionnel", so this can happen anytime in a day : https://base.apidae-tourisme.com/diffuser/projet/XXXX#tab-operations-exceptionnelles
*
*
* The notification is an http call on an url on your project configured in "Configuration technique" on your project :
* https://base.apidae-tourisme.com/diffuser/projet/XXXX#tab-configuration-technique
*
*
* When you receive the notification, just store it anywhere (database...) :
* you should probably not make any treatment on the notification (because it's an http call that can have a short timeout).
*
*
* In most cases, a cron is supposed to check if there is a new notification that require something on your side (extraction, indexation...)
* This cron can be daily (prefer something after 6am GMT) or more frequent if you want to catch the exceptionnal exports that can happen during the day (manually triggered)
*
*
* After your export/indexation, you have to notify Apidae that everything is correct on your side :
* $client->confirmExport(['hash' => $exportNotification['urlConfirmation']]);
* This will be displayed in Apidae so your client will know that the job is done on your side :
* https://base.apidae-tourisme.com/diffuser/projet/XXXX#tab-dernieres-generations : "Etat : Généré et intégré"
*
*
*/

/*
Expand All @@ -44,7 +41,7 @@
"urlConfirmation" => "https://api.apidae-tourisme.com/api/v002/export/confirmation?hash=672_20150106-1344_V4BjvT",
"ponctuel" => "true",
"urlRecuperation" => "https://export.apidae-tourisme.com/exports/672_20150106-1344_V4BjvT.zip",
);
);
*/


Expand Down Expand Up @@ -75,40 +72,13 @@

/**
* Download and extract zip to temp directory ($config['exportDir'] in config.inc.php)
* 06/11/2023 : getExportFiles ne renvoie plus un Symfony\Component\Finder\Finder
* Par ailleurs cet exemple n'est pas bon parce qu'on ne doit jamais traiter les indexation en même temps que la confirmation de réception
*/
/** @var Finder $exportFiles */
$exportFiles = false;
try {
$exportFiles = $client->getExportFiles(['url' => $exportNotification['urlRecuperation']]);
} catch (Exception $e) {
echo $e->getMessage();
}
if (!$exportFiles instanceof Finder) {
'$exportFiles is not an instance of Finder : process stopped';
die();
}

/**
* Indexation process : we have all data we need in $exportFiles
*/
foreach ($exportFiles->name('objets_modifies-*') as $file) {

/** @var SplFileInfo $file */
echo $file->getRealpath() . "\n";
$exportPath = $client->getExportFiles(['url' => $exportNotification['urlRecuperation']]) ;
// $finder = new Finder() ;
// $finder->files()->in($exportPath) ;

/** @var array $json */
$json = json_decode($file->getContents(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
echo 'File content is not a valid JSON String' . "\n";
continue;
}
/**
* Do whatever you need to do with the datas (store in DB...)
* In this example we only display json content
*/
echo json_encode($json, JSON_PRETTY_PRINT) . "\n";
}

/**
* After everything is done, you can confirmExport to change the export message of the project on Apidae from "Généré" to "Généré et intégré"
Expand Down
12 changes: 4 additions & 8 deletions src/Traits/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

use Exception;
use ZipArchive;
use Symfony\Component\Finder\Finder;

trait Export
{
/**
* Download and read zip export
* Download, unzip and return export path containing files as a string
*
* @param array<mixed> $params
* @return \Symfony\Component\Finder\Finder
* @throws \InvalidArgumentException
* @throws \Exception
*/
public function getExportFiles(array $params): Finder
public function getExportFiles(array $params): string
{
$client = $this->getHttpClient();

Expand Down Expand Up @@ -56,7 +54,7 @@ public function getExportFiles(array $params): Finder
$this->handleHttpError($e);
}

$zip = new ZipArchive;
$zip = new ZipArchive();
$res = $zip->open($zipFullPath);
if ($res !== true) {
throw new Exception('Invalid zip file');
Expand All @@ -65,9 +63,7 @@ public function getExportFiles(array $params): Finder
$zip->extractTo($exportFullPath);
$zip->close();

$finder = new Finder();
$finder->in($exportFullPath);
return $finder;
return $exportFullPath ;
}

/**
Expand Down

0 comments on commit 536daf2

Please sign in to comment.