2014-12-09 12:28:12 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Configuration
|
|
|
|
* URL : http://wse.scores-decisions.com
|
|
|
|
* ServiceName : entreprise
|
|
|
|
* ServiceVersion : 0.8
|
|
|
|
*
|
|
|
|
* SoapClient wsdl = URL + ServiceName + ServiceVersion + ?wsdl
|
|
|
|
*
|
|
|
|
* Configuration des paramètres de l'appel
|
|
|
|
* Appel Soap
|
|
|
|
* Gestion des erreurs
|
|
|
|
* Mise en cache
|
|
|
|
*
|
|
|
|
* Client ( name, version ) extends Zend_Soap_Client
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Client/Entreprise08
|
|
|
|
* Client/Gestion03
|
|
|
|
* Client/Gestion04
|
|
|
|
*
|
|
|
|
* Config ServiceName-Version
|
|
|
|
* methode
|
|
|
|
* parametres
|
|
|
|
* cache
|
|
|
|
* log => firebug, file, email
|
|
|
|
* error [
|
|
|
|
* code error => return (message, false), stop (true, false)
|
|
|
|
* ]
|
|
|
|
* arguments
|
|
|
|
* name => null, defaultvalue
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Interface qui déclare les méthodes d'appel
|
|
|
|
*
|
|
|
|
* Méthodes protégés pour les opérations webservice
|
|
|
|
* Paramètres de l'opération
|
|
|
|
* Paramètres spécifique - Mise en cache
|
|
|
|
* Gestion des erreurs
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Scores_Ws_Client extends Zend_Soap_Client
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Configuration des méthodes du service
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebService Url - Add a configuration key in application.ini
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $url = null;
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
/**
|
|
|
|
* Objet de cache
|
|
|
|
* @var object
|
|
|
|
*/
|
2014-12-09 12:28:12 +01:00
|
|
|
protected $cache;
|
|
|
|
|
2017-02-13 14:08:23 +01:00
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
* @var \Monolog\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2014-12-09 12:28:12 +01:00
|
|
|
/**
|
|
|
|
* Créer l'environnement nécessaire pour le chargement du webservice
|
|
|
|
* @param string $name
|
|
|
|
* Nom du service
|
|
|
|
* @param string $version
|
|
|
|
* Représente la version du service
|
|
|
|
* @param string $user
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function __construct($name, $version, $user = null)
|
|
|
|
{
|
2017-02-13 14:08:23 +01:00
|
|
|
if (Zend_Registry::isRegistered('logger')) {
|
|
|
|
$this->logger = Zend_Registry::get('logger');
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Configuration de l'application
|
2014-12-09 12:28:12 +01:00
|
|
|
if (Zend_Registry::isRegistered('config')) {
|
|
|
|
$c = Zend_Registry::get('config');
|
|
|
|
$this->url = $c->profil->webservice->url;
|
|
|
|
} else {
|
|
|
|
$c = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');
|
|
|
|
$this->url = $c->profil->webservice->url;
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Configuration du service
|
2014-12-09 12:28:12 +01:00
|
|
|
$config = include __DIR__ . '/Client/' . ucfirst($name) . '.php';
|
|
|
|
if ($config === false) {
|
|
|
|
throw new Exception('Impossible de charger la configuration du service');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_key_exists($version, $config)) {
|
|
|
|
throw new Exception('Version du service inexistante');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config = $config[$version];
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Create WSDL url
|
2014-12-09 12:28:12 +01:00
|
|
|
$wsdl = $this->url . '/' . $name . '/v' . $version;
|
|
|
|
if (APPLICATION_ENV == 'development') {
|
|
|
|
$wsdl.= '?wsdl-auto';
|
|
|
|
$this->setWsdlCache(WSDL_CACHE_NONE);
|
|
|
|
} else {
|
|
|
|
$wsdl.= '?wsdl';
|
|
|
|
}
|
|
|
|
$this->setWsdl($wsdl);
|
|
|
|
|
2015-01-13 20:59:31 +01:00
|
|
|
if (PHP_SAPI != 'cli' && $user === null) {
|
2014-12-09 12:28:12 +01:00
|
|
|
$user = new Scores_Utilisateur();
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info($user->getPassword());
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($user !== null) {
|
|
|
|
$this->setHttpLogin($user->getLogin());
|
|
|
|
$this->setHttpPassword($user->getPassword());
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Add default options
|
2014-12-09 12:28:12 +01:00
|
|
|
$options = array(
|
|
|
|
'features' => SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS,
|
|
|
|
'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
|
|
|
|
//'trace' => true,
|
|
|
|
'encoding' => 'utf-8',
|
|
|
|
);
|
|
|
|
$this->setOptions($options);
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Create Cache
|
2016-07-11 14:20:18 +02:00
|
|
|
$frontend = array( 'lifetime' => 14400, 'automatic_seralization' => true );
|
|
|
|
$backend = array( 'cache_dir' => $c->profil->path->shared . '/temp/cache' );
|
2014-12-09 12:28:12 +01:00
|
|
|
$this->cache = Zend_Cache::factory('Core', 'File', $frontend, $backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see Zend_Soap_Client::__call()
|
|
|
|
*/
|
|
|
|
public function __call($name, $arguments)
|
|
|
|
{
|
|
|
|
if ( !array_key_exists($name, $this->config) ) {
|
|
|
|
throw new Exception("Method $name not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
$methodConfig = $this->config[$name];
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info($methodConfig);
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Cache
|
2014-12-09 12:28:12 +01:00
|
|
|
$cacheEnable = false;
|
|
|
|
if ( array_key_exists('cache', $methodConfig) ) {
|
|
|
|
if ( $methodConfig['cache'] === true ) {
|
|
|
|
$cacheEnable = true;
|
|
|
|
$cacheId = $name;
|
|
|
|
if ( count($arguments) > 0 ){
|
|
|
|
foreach ($arguments as $item) {
|
|
|
|
$cacheId.= $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Cache
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( $cacheEnable === true ) {
|
|
|
|
$response = $this->cache->load($cacheId);
|
|
|
|
if ( $response !== false ) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Debug
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( array_key_exists('debug', $methodConfig) ) {
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info(__CLASS__.'->'.$name);
|
|
|
|
$this->logger->info($arguments);
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$response = parent::__call($name, $arguments);
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Debug
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( array_key_exists('debug', $methodConfig) ) {
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info($response);
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Cache
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( $cacheEnable === true ) {
|
|
|
|
$this->cache->save($response->{$name.'Result'}, $cacheId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response->{$name.'Result'};
|
|
|
|
|
|
|
|
} catch ( SoapFault $fault ) {
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Debug
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( array_key_exists('debug', $methodConfig) ) {
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info($fault->faultcode.' - '.$fault->faultstring);
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Gestion des SOAP fault
|
2014-12-09 12:28:12 +01:00
|
|
|
if ( array_key_exists('errorMsg', $methodConfig) ) {
|
2014-12-12 11:04:17 +01:00
|
|
|
if ( in_array($fault->faultcode, $methodConfig['errorMsg']) ) {
|
2017-02-13 14:08:23 +01:00
|
|
|
$this->logger->info("Exception as error message : ".$fault->faultcode);
|
2014-12-12 11:04:17 +01:00
|
|
|
throw new Exception($fault->faultstring);
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 09:18:25 +02:00
|
|
|
// --- Logging
|
|
|
|
if ( array_key_exists('log', $methodConfig) ) {
|
|
|
|
// --- Envoi email de contexte
|
2016-07-11 14:20:18 +02:00
|
|
|
if( $methodConfig['log'] == 'mail' && in_array(APPLICATION_ENV, array('production', 'staging')) ) {
|
2015-07-15 09:18:25 +02:00
|
|
|
$message = '';
|
|
|
|
$message.= 'Erreur SOAP - Code : '.$fault->faultcode.' - Message : '.$fault->faultstring;
|
|
|
|
$message.= ' - Utilisateur : '.$this->getHttpLogin();
|
|
|
|
$message.= "\n\n";
|
|
|
|
$message.= "Method : ".$name.", File :".$fault->getFile().", Ligne : ".$fault->getLine();
|
|
|
|
$message.= "\n\n";
|
|
|
|
$message.= "Detail :\n".$fault->getTraceAsString();
|
|
|
|
$message.= "\n\n";
|
|
|
|
if ( $controller = Zend_Controller_Front::getInstance() ) {
|
|
|
|
$message.= "Request Parameters :\n ".print_r($controller->getRequest()->getParams(), true);
|
|
|
|
$message.= "\n\n";
|
|
|
|
}
|
|
|
|
$message.= "Referer : ".$_SERVER['HTTP_REFERER']."\n\n";
|
|
|
|
$message.= "Requete :\n ".$requete."\n";
|
|
|
|
$message.= "Reponse :\n ".$reponse."\n";
|
|
|
|
|
|
|
|
$c = Zend_Registry::get('config');
|
|
|
|
$mail = new Scores_Mail_Method();
|
|
|
|
$mail->setSubject('[ERREUR SOAP] - '.$c->profil->server->name.' -'.date('Ymd'));
|
|
|
|
$mail->setBodyTextC($message);
|
2017-01-31 10:50:52 +01:00
|
|
|
$mail->setFromKey('contact');
|
2015-07-15 09:18:25 +02:00
|
|
|
$mail->addToKey('supportdev');
|
|
|
|
$mail->execute();
|
|
|
|
}
|
2014-12-09 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-07-15 09:18:25 +02:00
|
|
|
* @param string $url
|
2014-12-09 12:28:12 +01:00
|
|
|
*/
|
|
|
|
protected function setUrl($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|