Skip to content

Commit af690b1

Browse files
committed
Aggiunta parte della gestione delle visure
1 parent 63bf6af commit af690b1

15 files changed

Lines changed: 778 additions & 76 deletions

OpenApi.php

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php namespace OpenApi;
2-
if (session_status() == PHP_SESSION_NONE) {session_start();}
2+
33
class OpenApi {
44

55

@@ -9,12 +9,16 @@ class OpenApi {
99
* @param string $apikey ApiKey openapi
1010
* @param mixed $environment='test' uno tra: dev, test (default), production
1111
*/
12-
function __construct(array $scopes, string $username, string $apikey, $environment='test'){
13-
12+
function __construct(array $scopes, string $username, string $apikey, $environment='test', $store = NULL){
13+
if($store == NULL) {
14+
$store = new \OpenApi\classes\utility\sessionStoreToken;
15+
}
1416
$this->cache = new \OpenApi\classes\utility\DummyCache;
17+
$this->store = $store;
1518
$this->header = null;
1619
$this->rawResponse = null;
1720
$realScopes = [];
21+
$domainsRealScopes = [];
1822
$prefix = $environment=="production"?"":$environment.".";
1923
$domains = [];
2024
foreach($scopes as $s){
@@ -28,49 +32,69 @@ function __construct(array $scopes, string $username, string $apikey, $environme
2832
}
2933
if(!in_array($domain, $domains)){
3034
$domains[] = $domain;
35+
$domainsRealScopes[$domain] = [];
3136
}
37+
3238
if(!in_array($realScope,$realScopes)){
3339
$realScopes[] = $realScope;
40+
$domainsRealScopes[$domain][] = $realScope;
3441
}
42+
3543
}
44+
3645
$this->username = $username;
3746
$this->apikey = $apikey;
3847
$this->prefix = $prefix;
3948
$this->scopes = $realScopes;
40-
$token = $this->getToken();
4149

42-
43-
$moduli['ws.ufficiopostale.com'] = "\\OpenApi\\classes\\UfficioPostale";
44-
$nomi['ws.ufficiopostale.com'] = "ufficiopostale";
45-
$moduli['imprese.altravia.com'] = "\\OpenApi\\classes\\Imprese";
46-
$nomi['imprese.altravia.com'] = "imprese";
47-
$clients = [];
50+
$token = $this->getToken();
51+
//var_dump($token);exit;
52+
list($moduli,$nomi) = $this->getListaModuli();
53+
$this->clients = [];
4854
foreach($domains as $d){
4955
if(isset($moduli[$d])){
5056
$modulo = $moduli[$d];
5157
$nome = $nomi[$d];
52-
$this->$nome = new $modulo($token->token, $this->cache);
53-
$clients[] = $this->$nome;
58+
$this->$nome = new $modulo($token->token, $domainsRealScopes[$d], $this->cache, $prefix);
59+
$this->clients[] = $nome;
5460
}
5561
}
5662
}
5763

64+
/**
65+
*
66+
* Restituisce la lista dei moduli disponibili
67+
*
68+
* @return array
69+
*/
70+
private function getListaModuli(){
71+
$moduli = [];
72+
$nomi = [];
73+
$moduli['ws.ufficiopostale.com'] = "\\OpenApi\\classes\\UfficioPostale";
74+
$nomi['ws.ufficiopostale.com'] = "ufficiopostale";
75+
$moduli['imprese.altravia.com'] = "\\OpenApi\\classes\\Imprese";
76+
$nomi['imprese.altravia.com'] = "imprese";
77+
78+
$moduli['visengine2.altravia.com'] = "\\OpenApi\\classes\\VisEngine";
79+
$nomi['visengine2.altravia.com'] = "visengine";
80+
return array($moduli,$nomi);
81+
}
5882

5983
/**
6084
* Imposta la calsse da utilizzare sistema di cache, deve essere una classe che estende
61-
* {@see OpenApi\clasess\utility\DummyCache}
85+
* {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
6286
*
6387
* @param mixed $cacheSys Istanza della classe da usare come sistema di cache
6488
* @return void
6589
*/
6690
function setCacheSystem($cacheSys){
6791
$this->cache = $cacheSys;
6892
foreach($this->clients as $c){
69-
$c->setCacheSystem($cacheSys);
93+
$this->$c->setCacheSystem($cacheSys);
7094
}
7195
}
7296

73-
97+
7498
/**
7599
*
76100
* Restituisce il token attualemnte in sessione, se non presente o non più valido lo rigenera
@@ -81,20 +105,19 @@ function setCacheSystem($cacheSys){
81105
function getToken($force=FALSE){
82106
if(!$force && !$this->isTokenCompatible()){
83107

84-
//TODO: Controllare se il token è ancora valido
85108
if(!$this->mustRfreshToken()){
86-
return $_SESSION['openapi']['token'];
109+
return $this->store->get()['token'];
87110
}
88111
$this->renewToken();
89112

90-
return $_SESSION['openapi']['token'];
113+
return $this->store->get()['token'];
91114
}
92115
if($this->getOldToken()){
93116
if(!$this->mustRfreshToken()){
94-
return $_SESSION['openapi']['token'];
117+
return $this->store->get()['token'];
95118
}
96119
$this->renewToken();
97-
return $_SESSION['openapi']['token'];
120+
return $this->store->get()['token'];
98121
}
99122
return $this->generateNewToken();
100123
}
@@ -106,13 +129,13 @@ function getToken($force=FALSE){
106129
* @return object
107130
*/
108131
private function renewToken(){
109-
$param = ["expire" => 86400, "scopes" => $this->scopes];
132+
$param = ["expire" => time() + 86400, "scopes" => $this->scopes];
110133
//var_dump($param);exit;
111134

112-
$token = $this->connect("token/".$_SESSION['openapi']['token']->token,$param,"PUT");
135+
$token = $this->connect("token/".$this->store->get()['token']->token,$param,"PUT");
113136

114137
if($token == NULL){
115-
throw new \OpenApi\classes\exception\OpenApiTokenException("REnew Token: Connection Error",40001);
138+
throw new \OpenApi\classes\exception\OpenApiTokenException("Renew Token: Connection Error",40001);
116139
}
117140
if($token->success == false){
118141
$message = "REnew Token: unknow error";
@@ -127,7 +150,7 @@ private function renewToken(){
127150
if(isset($token->data) && isset($token->data[0]))
128151
{
129152
$token = $token->data[0];
130-
$_SESSION['openapi']['token'] = $token;
153+
$this->store->get()['token'] = $token;
131154
return $token;
132155
}
133156

@@ -140,7 +163,7 @@ private function renewToken(){
140163
* @return bool
141164
*/
142165
private function mustRfreshToken(){
143-
$token = $_SESSION['openapi']['token'];
166+
$token = $this->store->get()['token'];
144167
$diff = $token->expire-date("U");
145168
if($diff <= 6000){
146169
return TRUE;
@@ -157,8 +180,9 @@ private function mustRfreshToken(){
157180
* @return boolean
158181
*/
159182
function getOldToken(){
160-
$param = ["scopes" => $this->scopes];
183+
$param = ["scope" => $this->scopes];
161184
$token = $this->connect("token",$param,"GET");
185+
162186
$finded_token = NULL;
163187

164188
if($token != NULL && isset($token->data)){
@@ -170,11 +194,12 @@ function getOldToken(){
170194
}
171195

172196
if($finded_token != NULL){
173-
$_SESSION['openapi']['token'] = $finded_token;
174-
$_SESSION['openapi']['apikey'] = $this->apikey;
175-
$_SESSION['openapi']['scopes'] = serialize($this->scopes);
176-
$_SESSION['openapi']['username'] = $this->username;
177-
$_SESSION['openapi']['prefix'] = $this->prefix;
197+
$tostore['token'] = $finded_token;
198+
$tostore['apikey'] = $this->apikey;
199+
$tostore['scopes'] = serialize($this->scopes);
200+
$tostore['username'] = $this->username;
201+
$tostore['prefix'] = $this->prefix;
202+
$this->session->save($tostore);
178203
return TRUE;
179204
}
180205
return FALSE;
@@ -227,11 +252,14 @@ private function generateNewToken(){
227252
$except->setServerResponse($token, $this->header, $this->rawResponse);
228253
throw $except;
229254
}
230-
$_SESSION['openapi']['token'] = $token;
231-
$_SESSION['openapi']['apikey'] = $this->apikey;
232-
$_SESSION['openapi']['scopes'] = serialize($this->scopes);
233-
$_SESSION['openapi']['username'] = $this->username;
234-
$_SESSION['openapi']['prefix'] = $this->prefix;
255+
$tostore['token'] = $token;
256+
$tostore['apikey'] = $this->apikey;
257+
$tostore['scopes'] = serialize($this->scopes);
258+
$tostore['username'] = $this->username;
259+
$tostore['prefix'] = $this->prefix;
260+
261+
$this->store->save($tostore);
262+
235263
return $token;
236264
}
237265

@@ -243,13 +271,13 @@ private function generateNewToken(){
243271
* @return boolean
244272
*/
245273
private function isTokenCompatible() {
246-
if(!isset($_SESSION['openapi'])|| !isset($_SESSION['openapi']['token'])){
274+
if(!$this->store->isset()|| !isset($this->store->get()['token'])){
247275
return TRUE;
248276
}
249-
if($_SESSION['openapi']['prefix'] != $this->prefix || $_SESSION['openapi']['apikey'] != $this->apikey || $_SESSION['openapi']['username'] != $this->username){
277+
if($this->store->get()['prefix'] != $this->prefix || $this->store->get()['apikey'] != $this->apikey || $this->store->get()['username'] != $this->username){
250278
return TRUE;
251279
}
252-
$sessionScopes = unserialize($_SESSION['openapi']['scopes']);
280+
$sessionScopes = unserialize($this->store->get()['scopes']);
253281
if(!is_array($sessionScopes)){
254282
return TRUE;
255283
}
@@ -276,20 +304,21 @@ private function connect(string $endpoint, $param = [], $mode="POST"){
276304
$this->rawResponse = null;
277305
$basePath = "https://".$this->prefix."oauth.altravia.com";
278306
$url = $basePath."/".$endpoint;
279-
307+
if($mode == "GET")
308+
{
309+
$param = http_build_query($param);
310+
$param = preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', $param);
311+
$url .= "?".$param;
312+
}
280313

281314
$ch = curl_init($url);
282315
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
283316
if($mode == "POST" || $mode == "PUT")
284317
{
285318
curl_setopt($ch, CURLOPT_POST, TRUE);
286319
}
287-
if($mode == "GET")
320+
if($mode != "GET")
288321
{
289-
$param = http_build_query($param);
290-
$url .= "?".$param;
291-
292-
}else{
293322
$param = json_encode($param);
294323

295324
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
@@ -298,7 +327,7 @@ private function connect(string $endpoint, $param = [], $mode="POST"){
298327
$baseauth = base64_encode($this->username.":".$this->apikey);
299328
$headers = array(
300329
'Content-Type:application/json',
301-
'Authorization: Basic '. $baseauth // <---
330+
'Authorization: Basic '. $baseauth
302331
);
303332
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
304333

@@ -309,10 +338,10 @@ private function connect(string $endpoint, $param = [], $mode="POST"){
309338
curl_setopt($ch, CURLOPT_HEADER, 1);
310339
$response = curl_exec($ch);
311340
$this->rawResponse = $response;
341+
312342
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
313343
$this->header = substr($response, 0, $header_size);
314344
$return = substr($response, $header_size);
315-
316345
curl_close($ch);
317346
return json_decode($return);
318347
}

classes/Imprese.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
<?php
22
namespace OpenApi\classes;
33
class Imprese extends OpenApiBase {
4+
/**
5+
* @param string $token Il token da utilizzare per il collegamento
6+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
7+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
8+
*/
9+
function __construct(string $token, array $scopes, object $cache, string $prefix){
10+
parent::__construct($token, $scopes, $cache, $prefix);
11+
$this->basePath = "https://imprese.altravia.com";
12+
}
413

14+
/**
15+
*
16+
* Consente di recuperare i dati di una azienda a partire dalla partita IVA
17+
*
18+
* @param string $partitaIva La partita IVa da ricercare
19+
* @param int $ttr Time to Release: per quanti secondi la chiamata resta in cache prima di essere effettuata una seconda volta
20+
*
21+
* @return object
22+
*/
23+
function getByPartitaIva(string $partitaIva, $ttr = 86400){
24+
$partitaIva = trim($partitaIva);
25+
try{
26+
$data = $this->connect("advance/$partitaIva", "GET", [], $ttr);
27+
return $data->data;
28+
}catch (\OpenApi\classes\exception\OpenApiConnectionsException $e){
29+
if($e->getHTTPCode() == 404){
30+
return null;
31+
}
32+
throw $e;
33+
34+
35+
exit;
36+
}
37+
38+
39+
}
40+
41+
/**
42+
*
43+
* Cerca un'azienda o più utilizzando vari parametri
44+
*
45+
* @param string $denominazione Denominazione azienda
46+
* @param string $provincia Provincia
47+
* @param string $partitaIva=NULL Partita IVA
48+
* @param string $codiceFiscale=NULL Codice Fiscale
49+
* @param int $ttr Time to Release: per quanti secondi la chiamata resta in cache prima di essere effettuata una seconda volta
50+
*
51+
* @return array Lista delle aziende individuate
52+
*/
53+
function getBySearch(string $denominazione, string $provincia, $partitaIva= NULL , $codiceFiscale=NULL, $ttr = 86400){
54+
$params=[];
55+
if($denominazione != NULL){
56+
$params['denominazione'] = $denominazione;
57+
}
58+
if($provincia != NULL){
59+
$params['provincia'] = $provincia;
60+
}
61+
if($partitaIva != NULL){
62+
$params['piva'] = $partitaIva;
63+
}
64+
if($codiceFiscale != NULL){
65+
$params['cf'] = $codiceFiscale;
66+
}
67+
68+
$data = $this->connect("advance/$partitaIva", "GET", $params, $ttr);
69+
return $data->data;
70+
}
571
}

0 commit comments

Comments
 (0)