extranet/library/Scores/Ws/Client.php

223 lines
6.0 KiB
PHP
Raw Normal View History

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;
protected $cache;
/**
* 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)
{
//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();
}
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' => 28800,
'automatic_seralization' => true
);
$backend = array(
'cache_dir' => APPLICATION_PATH . '/../data/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");
}
//@todo : gestion des paramètres envoyés sous forme d'array
if( is_array($arguments) ) {
}
$methodConfig = $this->config[$name];
//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) ) {
Zend_Registry::get('firebug')->info(__CLASS__.'->'.$name);
2014-12-12 11:04:17 +01:00
Zend_Registry::get('firebug')->info($arguments);
2014-12-09 12:28:12 +01:00
}
try {
$response = parent::__call($name, $arguments);
//Debug
if ( array_key_exists('debug', $methodConfig) ) {
Zend_Registry::get('firebug')->info($response);
}
//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) ) {
Zend_Registry::get('firebug')->info($fault->faultcode.' - '.$fault->faultstring);
}
//Gestion des SOAP fault
if ( array_key_exists('errorMsg', $methodConfig) ) {
2014-12-12 11:04:17 +01:00
if ( in_array($fault->faultcode, $methodConfig['errorMsg']) ) {
//throw new Exception($fault->faultstring, 'MSG');
throw new Exception($fault->faultstring);
2014-12-09 12:28:12 +01:00
}
}
//Logging
if ( array_key_exists('log', $methodConfig) ) {
}
return false;
}
}
/**
*
* @param unknown $url
*/
protected function setUrl($url)
{
$this->url = $url;
}
}