extranet/library/Scores/Ws/Client.php
2017-03-07 14:50:28 +01:00

253 lines
7.7 KiB
PHP

<?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;
/**
* Objet de cache
* @var object
*/
protected $cache;
/**
* Logger
* @var \Monolog\Logger
*/
protected $logger;
/**
* 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)
{
if (Zend_Registry::isRegistered('logger')) {
$this->logger = Zend_Registry::get('logger');
}
// --- Configuration de l'application
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;
}
// --- Configuration du service
$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];
// --- Create WSDL url
$wsdl = $this->url . '/' . $name . '/v' . $version;
if (APPLICATION_ENV == 'development') {
$wsdl.= '?wsdl-auto';
$this->setWsdlCache(WSDL_CACHE_NONE);
} else {
$wsdl.= '?wsdl';
}
$this->setWsdl($wsdl);
if (PHP_SAPI != 'cli' && $user === null) {
$user = new Scores_Utilisateur();
$this->logger->info($user->getPassword());
}
if ($user !== null) {
$this->setHttpLogin($user->getLogin());
$this->setHttpPassword($user->getPassword());
}
// --- Add default options
$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);
// --- Create Cache
$frontend = array( 'lifetime' => 14400, 'automatic_seralization' => true );
$backend = array( 'cache_dir' => $c->profil->path->shared . '/cache' );
$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];
$this->logger->info(print_r($methodConfig,1));
// --- Cache
$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;
}
}
}
}
// --- Cache
if ( $cacheEnable === true ) {
$response = $this->cache->load($cacheId);
if ( $response !== false ) {
return $response;
}
}
// --- Debug
if ( array_key_exists('debug', $methodConfig) ) {
$this->logger->info(__CLASS__.'->'.$name);
$this->logger->info(print_r($arguments,1));
}
try {
$response = parent::__call($name, $arguments);
// --- Debug
if ( array_key_exists('debug', $methodConfig) ) {
$this->logger->info(print_r($response,1));
}
// --- Cache
if ( $cacheEnable === true ) {
$this->cache->save($response->{$name.'Result'}, $cacheId);
}
return $response->{$name.'Result'};
} catch ( SoapFault $fault ) {
// --- Debug
if ( array_key_exists('debug', $methodConfig) ) {
$this->logger->info($fault->faultcode.' - '.$fault->faultstring);
}
// --- Gestion des SOAP fault
if ( array_key_exists('errorMsg', $methodConfig) ) {
if ( in_array($fault->faultcode, $methodConfig['errorMsg']) ) {
$this->logger->info("Exception as error message : ".$fault->faultcode);
throw new Exception($fault->faultstring);
}
}
// --- Logging
if ( array_key_exists('log', $methodConfig) ) {
// --- Envoi email de contexte
if( $methodConfig['log'] == 'mail' && in_array(APPLICATION_ENV, array('production', 'staging')) ) {
$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);
$mail->setFromKey('contact');
$mail->addToKey('supportdev');
$mail->execute();
}
}
return false;
}
}
/**
*
* @param string $url
*/
protected function setUrl($url)
{
$this->url = $url;
}
}