Skip to content
Snippets Groups Projects
Commit 5dfca641 authored by Arnaud Pauget's avatar Arnaud Pauget
Browse files

feat () : add files about Maarch RM Rest Client

parent dc88bd3d
No related branches found
No related tags found
No related merge requests found
Pipeline #8282 failed
<?php
class MaarchREST extends SAEConnecteur {
private $curlWrapperFactory;
private $url;
private $token;
/** @var DonneesFormulaire */
private $connecteur_config;
public function __construct(CurlWrapperFactory $curlWrapperFactory,\Monolog\Logger $logger){
$this->curlWrapperFactory = $curlWrapperFactory;
}
public function setConnecteurConfig(DonneesFormulaire $donneesFormulaire) {
$this->url = $donneesFormulaire->get('url');
$this->token = $donneesFormulaire->get('token');
$this->connecteur_config = $donneesFormulaire;
}
/**
* @param $bordereauSEDA
* @param $archivePath
* @param string $file_type
* @param string $archive_file_name
* @return bool
* @throws Exception
*/
public function sendArchive($bordereauSEDA,$archivePath,$file_type="TARGZ",$archive_file_name="archive.tar.gz") {
$tmpFile = new TmpFile();
$bordereau_file = $tmpFile->create();
file_put_contents($bordereau_file, $bordereauSEDA);
try {
$this->callSedaMessage($bordereau_file, $archivePath);
} catch (Exception $e){
$tmpFile->delete($bordereau_file);
throw $e;
}
$tmpFile->delete($bordereau_file);
return true;
}
/**
* @param $seda_message_path
* @param $attachments_path
* @return bool|mixed
* @throws Exception
*/
public function callSedaMessage($seda_message_path, $attachments_path){
$curlWrapper = $this->curlWrapperFactory->getInstance();
$bodyPackage = $this->createObjectPackage($seda_message_path, $attachments_path);
$curlWrapper->setJsonPostData($bodyPackage);
return $this->getWS('/medona/archiveTransfer', "application/json", $curlWrapper);
}
public function createObjectPackage($seda_message_path, $attachments_path) {
$tmpFolder = dirname($attachments_path);
mkdir($tmpFolder."/tmpFiles");
try {
$phar = new PharData($attachments_path);
$phar->extractTo($tmpFolder."/tmpFiles", null, true);
} catch (Exception $e) {
return ($e);
}
$bodyPackage = [];
$bodyPackage['messageFile'] = base64_encode(file_get_contents($seda_message_path));
$bodyPackage['format'] = "seda";
$bodyPackage['attachments'] = [];
foreach(scandir($tmpFolder."/tmpFiles") as $filename) {
if ($filename != ".." && $filename != ".") {
$attachment = new stdClass();
$attachment->filename = $filename;
$attachment->data = base64_encode(file_get_contents($tmpFolder."/tmpFiles/".$filename));
unlink($tmpFolder."/tmpFiles/".$filename);
array_push($bodyPackage['attachments'], $attachment);
}
}
rmdir($tmpFolder."/tmpFiles");
return $bodyPackage;
}
public function getErrorString($number){
return "Erreur non identifié";
}
/**
* @param $id_transfert
* @return string Le contenu du fichier XML contenant l'accusé de reception
* @throws Exception
*/
public function getAcuseReception($id_transfert) {
if (! $id_transfert){
throw new UnrecoverableException("L'identifiant du transfert n'a pas été trouvé");
}
$messageInformations = $this->getWS(
"/medona/message/reference?reference="
.urlencode($id_transfert."_Ack"),
"application/json"
);
if ($messageInformations) {
$zipMessage = $this->getWS(
"/medona/message/".$messageInformations['messageId']."/Export",
"application/zip"
);
} else {
throw new UnrecoverableException("Le paquet du bordereau identifié par ".$messageInformations['messageId']." n'a pas pu êtr récuperé.");
}
$tmpFile = new TmpFile();
$zipTempFile = $tmpFile->create();
file_put_contents($zipTempFile, $zipMessage);
// get the absolute path to $file
$path = pathinfo(realpath($zipTempFile), PATHINFO_DIRNAME);
try {
shell_exec("7z x $zipTempFile -o$path *.xml");
} catch (Exception $e) {
return($e);
}
$fullFilePath = $path."/".$messageInformations['messageId'].".xml";
if (!file_exists($fullFilePath)) {
return "Problème lors de la lecture du fichier $fullFilePath.";
}
$xmlContent = file_get_contents($fullFilePath);
unlink($zipTempFile);
unlink($fullFilePath);
return $xmlContent;
}
/**
* @param $id_transfert
* @return bool|mixed
* @throws Exception
*/
public function getReply($id_transfert) {
if (! $id_transfert){
throw new UnrecoverableException("L'identifiant du transfert n'a pas été trouvé");
}
$messageInformations = $this->getWS(
"/medona/message/reference?reference="
.urlencode($id_transfert),
"application/json"
);
if ($messageInformations['status'] != "processed") {
return "Le bordereau dont la référence est $id_transfert n'a pas encore été traité dans le SAE Maarch RM.";
}
return $messageInformations;
}
public function getURL($cote) {
if (empty($this->url)){
return $cote;
}
$tab = parse_url($this->url);
return "{$tab['scheme']}://{$tab['host']}/archives/viewByArchiveIdentifier/$cote";
}
/**
* @param $url
* @param string $accept
* @return bool|mixed
* @throws Exception
*/
private function getWS($url,$accept = "application/json",CurlWrapper $curlWrapper = null){
if (! $curlWrapper){
$curlWrapper = $this->curlWrapperFactory->getInstance();
}
$curlWrapper->addHeader("Accept",$accept);
$curlWrapper->addHeader("Cookie","LAABS-AUTH=".$this->token);
$curlWrapper->addHeader("User-Agent","service");
$curlWrapper->dontVerifySSLCACert();
$result = $curlWrapper->get($this->url.$url);
if (! $result){
throw new Exception($curlWrapper->getLastError());
}
$http_code = $curlWrapper->getHTTPCode();
if ($http_code != 200){
throw new Exception("$result - code d'erreur HTTP : $http_code");
}
$old_result = $result;
if ($accept == "application/json"){
$result = json_decode($result,true);
}
if (! $result){
throw new Exception(
"Le serveur Maarch n'a pas renvoyé une réponse compréhensible - problème de configuration ? : $old_result"
);
}
return $result;
}
/**
* @return bool|mixed
* @throws Exception
*/
public function getVersion(){
return $this->getWS('/versions');
}
/**
* @return bool|mixed
* @throws Exception
*/
public function ping(){
return $this->getWS('/ping');
}
}
\ No newline at end of file
<?php
class MaarchRestVersion extends ActionExecutor {
public function go(){
/** @var AsalaeREST $asalae */
$maarchSae = $this->getMyConnecteur();
$message = $maarchSae->getVersion();
$this->setLastMessage("Connexion réussie: ".json_encode($message));
return true;
}
}
\ No newline at end of file
nom: Maarch Rest
type: SAE
description: Connecteur pour le SAE Maarch RM
formulaire:
page0:
url:
name: "URL de l'instance Maarch RM"
token:
name: Jeton URL Encodé du comtpe de service
originating_agency:
name: Identifiant service versant
action:
version:
name: Tester la connexion (authentification)
action-class: MaarchRestVersion
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment