120 lines
3.9 KiB
PHP
120 lines
3.9 KiB
PHP
<?php
|
|
|
|
class App_Service_ExternalApiService
|
|
{
|
|
|
|
const API_TYPE_CHANGE_ORDER_STATUS = 'change_order_status';
|
|
|
|
protected $_httpClient;
|
|
protected $_configs;
|
|
protected $_ipManager;
|
|
protected $_genericModel;
|
|
|
|
protected $_endPoint;
|
|
protected $_authToken;
|
|
|
|
public function __construct()
|
|
{
|
|
|
|
$this->_configs = Zend_Registry::get('config');
|
|
$this->_ipManager = new App_Service_IpManager();
|
|
$this->_genericModel = new Application_Model_Generic();
|
|
|
|
$this->_httpClient = new Zend_Http_Client();
|
|
$this->_httpClient->setConfig(array(
|
|
'adapter' => 'Zend_Http_Client_Adapter_Curl',
|
|
'maxredirects' => 0,
|
|
'timeout' => 60,
|
|
'ssltransport' => 'ssl'
|
|
));
|
|
|
|
}
|
|
|
|
public function setConnectionParams($codiceAttivita, $tipoApi)
|
|
{
|
|
|
|
//Recupero se presenti i dati per la comunicazione con un eventuale endpoint
|
|
$api = $this->_genericModel->getFiltered('ExternalApi', array('CodiceAttivita = ?' => $codiceAttivita, 'TipoApi = ?' => $tipoApi, 'Attivo = ?' => 1));
|
|
|
|
if (!$api) {
|
|
$this->_authToken = null;
|
|
$this->_endPoint = null;
|
|
} else {
|
|
$this->_authToken = $api['AuthToken'];
|
|
$this->_endPoint = $api['Endpoint'];
|
|
}
|
|
|
|
}
|
|
|
|
public function checkConnectionParams()
|
|
{
|
|
if (!$this->_authToken || !$this->_endPoint) {
|
|
throw new App_Controller_Errors_WebServiceError('Parametri per la connessione non impostati correttamente', App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
}
|
|
|
|
public function postCall($dati = array())
|
|
{
|
|
|
|
$this->checkConnectionParams();
|
|
|
|
$this->_httpClient->setMethod('POST');
|
|
$this->_httpClient->setUri($this->_endPoint);
|
|
$this->_httpClient->setRawData(json_encode($dati), 'application/json;charset=utf-8');
|
|
|
|
return $this->executeCall();
|
|
}
|
|
|
|
public function putCall($dati = array())
|
|
{
|
|
|
|
$this->checkConnectionParams();
|
|
|
|
$this->_httpClient->setMethod('PUT');
|
|
$this->_httpClient->setUri($this->_endPoint);
|
|
$this->_httpClient->setRawData(json_encode($dati), 'application/json;charset=utf-8');
|
|
|
|
return $this->executeCall();
|
|
}
|
|
|
|
private function executeCall()
|
|
{
|
|
|
|
$this->_httpClient->setHeaders('Authorization', 'Secret ' . $this->_authToken);
|
|
|
|
if ($this->_ipManager->allowedDevMode()) {
|
|
$this->_httpClient->setHeaders('X-As-Dev-On', '1');
|
|
$this->_httpClient->setHeaders('X-As-Dev-Pw', 'JackDev2590');
|
|
}
|
|
|
|
$httpResponse = $this->_httpClient->request();
|
|
$httpResponse->getStatus();
|
|
|
|
$headerResponse = $httpResponse->getHeaders();
|
|
|
|
// Zend_Registry::get('log')->info(print_r($this->_httpClient->getLastRequest(),true));
|
|
// Zend_Registry::get('log')->info(print_r($this->_httpClient->getLastResponse(),true));
|
|
|
|
// Zend_Registry::get('log')->info(print_r($headerResponse,true));
|
|
// Zend_Registry::get('log')->info(print_r($httpResponse->getStatus(),true));
|
|
|
|
if (preg_match('/application\/json|json/', $headerResponse['Content-type'])) {
|
|
$httpResponseBody = json_decode($httpResponse->getBody(), 1);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
throw new App_Controller_Errors_WebServiceError("Errore di decodifica json per la chiamata: " . $this->_httpClient->getUri(), App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
if ($httpResponse->getStatus() >= 400 && $httpResponse->getStatus() < 500) {
|
|
throw new App_Controller_Errors_WebServiceError($httpResponseBody['message'], App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
} else {
|
|
$httpResponseBody = $httpResponse->getBody();
|
|
}
|
|
|
|
return $httpResponseBody;
|
|
}
|
|
|
|
}
|