PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Service/DeveloperGarden/Client/ClientAbstract.php

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