web/lib/Zend/Service/DeveloperGarden/Client/ClientAbstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service
       
    17  * @subpackage DeveloperGarden
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: ClientAbstract.php 22662 2010-07-24 17:37:36Z mabe $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_DeveloperGarden_Client_Soap
       
    25  */
       
    26 require_once 'Zend/Service/DeveloperGarden/Client/Soap.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Service_DeveloperGarden_Credential
       
    30  */
       
    31 require_once 'Zend/Service/DeveloperGarden/Credential.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Service_DeveloperGarden_SecurityTokenServer
       
    35  */
       
    36 require_once 'Zend/Service/DeveloperGarden/SecurityTokenServer.php';
       
    37 
       
    38 /**
       
    39  * @category   Zend
       
    40  * @package    Zend_Service
       
    41  * @subpackage DeveloperGarden
       
    42  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    43  * @author     Marco Kaiser
       
    44  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    45  */
       
    46 abstract class Zend_Service_DeveloperGarden_Client_ClientAbstract
       
    47 {
       
    48     /**
       
    49      * constants for using with the odg api
       
    50      */
       
    51     const ENV_PRODUCTION = 1; // Production Environment
       
    52     const ENV_SANDBOX    = 2; // Sandbox Environment, limited access to the api
       
    53     const ENV_MOCK       = 3; // Api calls are without any functionality
       
    54 
       
    55     const PARTICIPANT_MUTE_OFF = 0; // removes mute from participant in a conference
       
    56     const PARTICIPANT_MUTE_ON  = 1; // mute participant in a conference
       
    57     const PARTICIPANT_RECALL   = 2; // recalls the participant in a conference
       
    58 
       
    59     /**
       
    60      * array of all possible env types
       
    61      *
       
    62      * @var int
       
    63      */
       
    64     static protected $_consts = null;
       
    65 
       
    66     /**
       
    67      * Available options
       
    68      *
       
    69      * @var array available options
       
    70      */
       
    71     protected $_options = array();
       
    72 
       
    73     /**
       
    74      * The service id to generate the auth service token
       
    75      *
       
    76      * @var string
       
    77      */
       
    78     protected $_serviceAuthId = 'https://odg.t-online.de';
       
    79 
       
    80     /**
       
    81      * Variable that holds the Zend_Service_DeveloperGarden env value
       
    82      *
       
    83      * @var int
       
    84      */
       
    85     protected $_serviceEnvironment = Zend_Service_DeveloperGarden_Client_ClientAbstract::ENV_PRODUCTION;
       
    86 
       
    87     /**
       
    88      * wsdl file
       
    89      *
       
    90      * @var string
       
    91      */
       
    92     protected $_wsdlFile = null;
       
    93 
       
    94     /**
       
    95      * the local wsdlFile
       
    96      *
       
    97      * @var string
       
    98      */
       
    99     protected $_wsdlFileLocal = null;
       
   100 
       
   101     /**
       
   102      * should we use the local wsdl file?
       
   103      *
       
   104      * @var boolean
       
   105      */
       
   106     protected $_useLocalWsdl = true;
       
   107 
       
   108     /**
       
   109      * class with credentials
       
   110      *
       
   111      * @var Zend_Service_DeveloperGarden_Credential
       
   112      */
       
   113     protected $_credential = null;
       
   114 
       
   115     /**
       
   116      * The internal Soap Client
       
   117      *
       
   118      * @var Zend_Soap_Client
       
   119      */
       
   120     protected $_soapClient = null;
       
   121 
       
   122     /**
       
   123      * array with options for classmapping
       
   124      *
       
   125      * @var array
       
   126      */
       
   127     protected $_classMap = array();
       
   128 
       
   129     /**
       
   130      * constructor
       
   131      *
       
   132      * @param array $options Associative array of options
       
   133      */
       
   134     public function __construct(array $options = array())
       
   135     {
       
   136         $this->_credential = new Zend_Service_DeveloperGarden_Credential();
       
   137 
       
   138         while (list($name, $value) = each($options)) {
       
   139             switch (ucfirst($name)) {
       
   140                 case 'Username' :
       
   141                     $this->_credential->setUsername($value);
       
   142                     break;
       
   143                 case 'Password' :
       
   144                     $this->_credential->setPassword($value);
       
   145                     break;
       
   146                 case 'Realm' :
       
   147                     $this->_credential->setRealm($value);
       
   148                     break;
       
   149                 case 'Environment' :
       
   150                     $this->setEnvironment($value);
       
   151             }
       
   152         }
       
   153 
       
   154         if (empty($this->_wsdlFile)) {
       
   155             require_once 'Zend/Service/DeveloperGarden/Exception.php';
       
   156             throw new Zend_Service_DeveloperGarden_Exception('_wsdlFile not set for this service.');
       
   157         }
       
   158 
       
   159         if (!empty($this->_wsdlFileLocal)) {
       
   160             $this->_wsdlFileLocal = realpath(dirname(__FILE__) . '/../' . $this->_wsdlFileLocal);
       
   161         }
       
   162 
       
   163         if (empty($this->_wsdlFileLocal) || $this->_wsdlFileLocal === false) {
       
   164             require_once 'Zend/Service/DeveloperGarden/Exception.php';
       
   165             throw new Zend_Service_DeveloperGarden_Exception('_wsdlFileLocal not set for this service.');
       
   166         }
       
   167     }
       
   168 
       
   169     /**
       
   170      * Set an option
       
   171      *
       
   172      * @param  string $name
       
   173      * @param  mixed $value
       
   174      * @throws Zend_Service_DeveloperGarden_Client_Exception
       
   175      * @return Zend_Service_DeveloperGarden_Client_ClientAbstract
       
   176      */
       
   177     public function setOption($name, $value)
       
   178     {
       
   179         if (!is_string($name)) {
       
   180             require_once 'Zend/Service/DeveloperGarden/Client/Exception.php';
       
   181             throw new Zend_Service_DeveloperGarden_Client_Exception('Incorrect option name: ' . $name);
       
   182         }
       
   183         $name = strtolower($name);
       
   184         if (array_key_exists($name, $this->_options)) {
       
   185             $this->_options[$name] = $value;
       
   186         }
       
   187 
       
   188         return $this;
       
   189     }
       
   190 
       
   191     /**
       
   192      * get an option value from the internal options object
       
   193      *
       
   194      * @param  string $name
       
   195      * @return mixed
       
   196      */
       
   197     public function getOption($name)
       
   198     {
       
   199         $name = strtolower($name);
       
   200         if (array_key_exists($name, $this->_options)) {
       
   201             return $this->_options[$name];
       
   202         }
       
   203 
       
   204         return null;
       
   205     }
       
   206 
       
   207     /**
       
   208      * returns the internal soap client
       
   209      * if not allready exists we create an instance of
       
   210      * Zend_Soap_Client
       
   211      *
       
   212      * @final
       
   213      * @return Zend_Service_DeveloperGarden_Client_Soap
       
   214      */
       
   215     final public function getSoapClient()
       
   216     {
       
   217         if ($this->_soapClient === null) {
       
   218             /**
       
   219              * init the soapClient
       
   220              */
       
   221             $this->_soapClient = new Zend_Service_DeveloperGarden_Client_Soap(
       
   222                 $this->getWsdl(),
       
   223                 $this->getClientOptions()
       
   224             );
       
   225             $this->_soapClient->setCredential($this->_credential);
       
   226             $tokenService = new Zend_Service_DeveloperGarden_SecurityTokenServer(
       
   227                 array(
       
   228                     'username'    => $this->_credential->getUsername(),
       
   229                     'password'    => $this->_credential->getPassword(),
       
   230                     'environment' => $this->getEnvironment(),
       
   231                     'realm'       => $this->_credential->getRealm(),
       
   232                 )
       
   233             );
       
   234             $this->_soapClient->setTokenService($tokenService);
       
   235         }
       
   236 
       
   237         return $this->_soapClient;
       
   238     }
       
   239 
       
   240     /**
       
   241      * sets new environment
       
   242      *
       
   243      * @param int $environment
       
   244      * @return Zend_Service_DeveloperGarden_Client_ClientAbstract
       
   245      */
       
   246     public function setEnvironment($environment)
       
   247     {
       
   248         self::checkEnvironment($environment);
       
   249         $this->_serviceEnvironment = $environment;
       
   250         return $this;
       
   251     }
       
   252 
       
   253     /**
       
   254      * returns the current configured environemnt
       
   255      *
       
   256      * @return int
       
   257      */
       
   258     public function getEnvironment()
       
   259     {
       
   260         return $this->_serviceEnvironment;
       
   261     }
       
   262 
       
   263     /**
       
   264      * returns the wsdl file path, a uri or the local path
       
   265      *
       
   266      * @return string
       
   267      */
       
   268     public function getWsdl()
       
   269     {
       
   270         if ($this->_useLocalWsdl) {
       
   271             $retVal = $this->_wsdlFileLocal;
       
   272         } else {
       
   273             $retVal = $this->_wsdlFile;
       
   274         }
       
   275 
       
   276         return $retVal;
       
   277     }
       
   278 
       
   279     /**
       
   280      * switch to the local wsdl file usage
       
   281      *
       
   282      * @param boolen $use
       
   283      * @return Zend_Service_DeveloperGarden_Client_ClientAbstract
       
   284      */
       
   285     public function setUseLocalWsdl($use = true)
       
   286     {
       
   287         $this->_useLocalWsdl = (boolean) $use;
       
   288         return $this;
       
   289     }
       
   290 
       
   291     /**
       
   292      * sets a new wsdl file
       
   293      *
       
   294      * @param string $wsdlFile
       
   295      * @return Zend_Service_DeveloperGarden_Client_ClientAbstract
       
   296      */
       
   297     public function setWsdl($wsdlFile = null)
       
   298     {
       
   299         if (empty($wsdlFile)) {
       
   300             require_once 'Zend/Service/DeveloperGarden/Exception.php';
       
   301             throw new Zend_Service_DeveloperGarden_Exception('_wsdlFile not set for this service.');
       
   302         }
       
   303         $this->_wsdlFile = $wsdlFile;
       
   304         return $this;
       
   305     }
       
   306 
       
   307     /**
       
   308      * sets a new local wsdl file
       
   309      *
       
   310      * @param string $wsdlFile
       
   311      * @return Zend_Service_DeveloperGarden_Client_ClientAbstract
       
   312      */
       
   313     public function setLocalWsdl($wsdlFile = null)
       
   314     {
       
   315         if (empty($wsdlFile)) {
       
   316             require_once 'Zend/Service/DeveloperGarden/Exception.php';
       
   317             throw new Zend_Service_DeveloperGarden_Exception('_wsdlFileLocal not set for this service.');
       
   318         }
       
   319         $this->_wsdlFileLocal = $wsdlFile;
       
   320         return $this;
       
   321     }
       
   322 
       
   323     /**
       
   324      * returns an array with configured options for this client
       
   325      *
       
   326      * @return array
       
   327      */
       
   328     public function getClientOptions()
       
   329     {
       
   330         $options = array(
       
   331             'soap_version' => SOAP_1_1,
       
   332         );
       
   333         if (!empty($this->_classMap)) {
       
   334             $options['classmap'] = $this->_classMap;
       
   335         }
       
   336         $wsdlCache = Zend_Service_DeveloperGarden_SecurityTokenServer_Cache::getWsdlCache();
       
   337         if ($wsdlCache !== null) {
       
   338             $options['cache_wsdl'] = $wsdlCache;
       
   339         }
       
   340         return $options;
       
   341     }
       
   342 
       
   343     /**
       
   344      * returns the internal credential object
       
   345      *
       
   346      * @return Zend_Service_DeveloperGarden_Credential
       
   347      */
       
   348     public function getCredential()
       
   349     {
       
   350         return $this->_credential;
       
   351     }
       
   352 
       
   353     /**
       
   354      * helper method to create const arrays
       
   355      * @return null
       
   356      */
       
   357     static protected function _buildConstArray()
       
   358     {
       
   359         $r = new ReflectionClass(__CLASS__);
       
   360         foreach ($r->getConstants() as $k => $v) {
       
   361             $s = explode('_', $k, 2);
       
   362             if (!isset(self::$_consts[$s[0]])) {
       
   363                 self::$_consts[$s[0]] = array();
       
   364             }
       
   365             self::$_consts[$s[0]][$v] = $k;
       
   366         }
       
   367     }
       
   368 
       
   369     /**
       
   370      * returns an array of all available environments
       
   371      *
       
   372      * @return array
       
   373      */
       
   374     static public function getParticipantActions()
       
   375     {
       
   376         if (empty(self::$_consts)) {
       
   377             self::_buildConstArray();
       
   378         }
       
   379         return self::$_consts['PARTICIPANT'];
       
   380     }
       
   381 
       
   382     /**
       
   383      * checks if the given action is valid
       
   384      * otherwise it @throws Zend_Service_DeveloperGarden_Exception
       
   385      *
       
   386      * @param int $action
       
   387      * @throws Zend_Service_DeveloperGarden_Client_Exception
       
   388      * @return void
       
   389      */
       
   390     static public function checkParticipantAction($action)
       
   391     {
       
   392         if (!array_key_exists($action, self::getParticipantActions())) {
       
   393             require_once 'Zend/Service/DeveloperGarden/Client/Exception.php';
       
   394             throw new Zend_Service_DeveloperGarden_Client_Exception(
       
   395                 'Wrong Participant Action ' . $action . ' supplied.'
       
   396             );
       
   397         }
       
   398     }
       
   399 
       
   400     /**
       
   401      * returns an array of all available environments
       
   402      *
       
   403      * @return array
       
   404      */
       
   405     static public function getEnvironments()
       
   406     {
       
   407         if (empty(self::$_consts)) {
       
   408             self::_buildConstArray();
       
   409         }
       
   410         return self::$_consts['ENV'];
       
   411     }
       
   412 
       
   413     /**
       
   414      * checks if the given environemnt is valid
       
   415      * otherwise it @throws Zend_Service_DeveloperGarden_Client_Exception
       
   416      *
       
   417      * @param int $environment
       
   418      * @throws Zend_Service_DeveloperGarden_Client_Exception
       
   419      * @return void
       
   420      */
       
   421     static public function checkEnvironment($environment)
       
   422     {
       
   423         if (!array_key_exists($environment, self::getEnvironments())) {
       
   424             require_once 'Zend/Service/DeveloperGarden/Client/Exception.php';
       
   425             throw new Zend_Service_DeveloperGarden_Client_Exception(
       
   426                 'Wrong environment ' . $environment . ' supplied.'
       
   427             );
       
   428         }
       
   429     }
       
   430 }