2014-03-14 10:42:09 +00:00

160 lines
3.8 KiB
PHP

<?php
/**
* Load User Info
*/
require_once 'Scores/Utilisateur.php';
/**
* Distribute Scores Webservice
*/
class Scores_Ws
{
/**
* User login
* @var string
*/
protected $login = null;
/**
* Password
* @var string
*/
protected $password = null;
/**
* Enable/Disable Cache
* @var boolean
*/
protected $cache = true;
/**
* Enable/Disable cache writing
* Override the cache flag
* @var boolean
*/
protected $cacheWrite = true;
/**
* Number of response
* @var int
*/
protected $nbReponses = 20;
protected $obj = null;
/**
* Scores_Ws
* @param string $login
* @param string $password
*/
public function __construct($login = null, $password = null)
{
if ( !empty($login) && !empty($password) ){
$this->login = $login;
$this->password = $password;
} else {
$user = new Scores_Utilisateur();
$this->login = $user->getLogin();
$this->password = $user->getPassword();
$this->nbReponses = $user->getNbRep();
if ( $user->checkModeEdition() ) {
//Disable cache
$this->cache = false;
//Don't write cache
if ( APPLICATION_ENV == 'staging' ) {
$this->cacheWrite = false;
}
}
}
}
/**
* Distribute call on each separate class for each service
* Schema for $name is {Class}_{Method}
* @param string $name
* @param array $args
* @return mixed
*/
public function __call($name, $args)
{
$response = false;
$pos = strpos($name, '_');
$className = substr($name, 0, $pos);
$methodName = substr($name, $pos+1);
$objR = new ReflectionClass('Scores_Ws_'.$className);
$this->obj = $objR->newInstance($methodName);
$this->obj->setSoapClientOption('login', $this->login);
$this->obj->setSoapClientOption('password', $this->password);
//Check cache
if ($this->cacheWrite && $this->obj->getCache()) {
$filename = $this->obj->getFilename();
$cache = new Cache($filename);
if ($cache->exist() && $this->cacheEnable ){
$response = $cache->getBlock();
}
}
//Execute the request
else {
call_user_func_array(array($this->obj, $methodName), $args);
if ( !$this->obj->isError() || !$this->obj->isMessage() ) {
$response = $obj->getSoapResponse();
//Put in cache the response
if ($this->cacheWrite && $obj->getCache()) {
$cache->deletefile();
$cache->setBlock($responses);
}
}
}
return $response;
}
/**
* Type du retour
* @return string
* ERR or MSG
*/
public function getResponseType()
{
if ( $this->obj->isError() ) {
return 'ERR';
} elseif ( $this->obj->isMessage() ) {
return 'MSG';
}
return null;
}
/**
* Message de retour pour affichage
* @return string
*/
public function getResponseMsg()
{
return $this->obj->getMessage();
}
/**
* Retourne les éléments pour debuggage
* @return object
*/
public function getError()
{
$error = new stdClass();
$error->service = $this->obj->getServiceName();
$error->method = $this->obj->getMethodName();
//Request Parameter
$error->args = $this->obj->getParams();
$error->faultCode = $this->obj->getFaultCode();
$error->faultMessage = $this->obj->getMessage();
return $error;
}
}