PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/Service/StrikeIron/Base.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 274 lines | 118 code | 24 blank | 132 comment | 17 complexity | 7f1257e093209f209a7cefe88d9193cb MD5 | raw file
  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 StrikeIron
  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$
  21. */
  22. /**
  23. * @see Zend_Service_StrikeIron_Decorator
  24. */
  25. require_once 'Zend/Service/StrikeIron/Decorator.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage StrikeIron
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_StrikeIron_Base
  34. {
  35. /**
  36. * Configuration options
  37. * @param array
  38. */
  39. protected $_options = array('username' => null,
  40. 'password' => null,
  41. 'client' => null,
  42. 'options' => null,
  43. 'headers' => null,
  44. 'wsdl' => null);
  45. /**
  46. * Output headers returned by the last call to SOAPClient->__soapCall()
  47. * @param array
  48. */
  49. protected $_outputHeaders = array();
  50. /**
  51. * Class constructor
  52. *
  53. * @param array $options Key/value pair options
  54. * @throws Zend_Service_StrikeIron_Exception
  55. */
  56. public function __construct($options = array())
  57. {
  58. if (!extension_loaded('soap')) {
  59. /**
  60. * @see Zend_Service_StrikeIron_Exception
  61. */
  62. require_once 'Zend/Service/StrikeIron/Exception.php';
  63. throw new Zend_Service_StrikeIron_Exception('SOAP extension is not enabled');
  64. }
  65. $this->_options = array_merge($this->_options, $options);
  66. $this->_initSoapHeaders();
  67. $this->_initSoapClient();
  68. }
  69. /**
  70. * Proxy method calls to the SOAPClient instance, transforming method
  71. * calls and responses for convenience.
  72. *
  73. * @param string $method Method name
  74. * @param array $params Parameters for method
  75. * @return mixed Result
  76. * @throws Zend_Service_StrikeIron_Exception
  77. */
  78. public function __call($method, $params)
  79. {
  80. // prepare method name and parameters for soap call
  81. list($method, $params) = $this->_transformCall($method, $params);
  82. $params = isset($params[0]) ? array($params[0]) : array();
  83. // make soap call, capturing the result and output headers
  84. try {
  85. $result = $this->_options['client']->__soapCall($method,
  86. $params,
  87. $this->_options['options'],
  88. $this->_options['headers'],
  89. $this->_outputHeaders);
  90. } catch (Exception $e) {
  91. $message = get_class($e) . ': ' . $e->getMessage();
  92. /**
  93. * @see Zend_Service_StrikeIron_Exception
  94. */
  95. require_once 'Zend/Service/StrikeIron/Exception.php';
  96. throw new Zend_Service_StrikeIron_Exception($message, $e->getCode(), $e);
  97. }
  98. // transform/decorate the result and return it
  99. $result = $this->_transformResult($result, $method, $params);
  100. return $result;
  101. }
  102. /**
  103. * Initialize the SOAPClient instance
  104. *
  105. * @return void
  106. */
  107. protected function _initSoapClient()
  108. {
  109. if (! isset($this->_options['options'])) {
  110. $this->_options['options'] = array();
  111. }
  112. if (! isset($this->_options['client'])) {
  113. $this->_options['client'] = new SoapClient($this->_options['wsdl'],
  114. $this->_options['options']);
  115. }
  116. }
  117. /**
  118. * Initialize the headers to pass to SOAPClient->__soapCall()
  119. *
  120. * @return void
  121. * @throws Zend_Service_StrikeIron_Exception
  122. */
  123. protected function _initSoapHeaders()
  124. {
  125. // validate headers and check if LicenseInfo was given
  126. $foundLicenseInfo = false;
  127. if (isset($this->_options['headers'])) {
  128. if (! is_array($this->_options['headers'])) {
  129. $this->_options['headers'] = array($this->_options['headers']);
  130. }
  131. foreach ($this->_options['headers'] as $header) {
  132. if (! $header instanceof SoapHeader) {
  133. /**
  134. * @see Zend_Service_StrikeIron_Exception
  135. */
  136. require_once 'Zend/Service/StrikeIron/Exception.php';
  137. throw new Zend_Service_StrikeIron_Exception('Header must be instance of SoapHeader');
  138. } else if ($header->name == 'LicenseInfo') {
  139. $foundLicenseInfo = true;
  140. break;
  141. }
  142. }
  143. } else {
  144. $this->_options['headers'] = array();
  145. }
  146. // add default LicenseInfo header if a custom one was not supplied
  147. if (! $foundLicenseInfo) {
  148. $this->_options['headers'][] = new SoapHeader('http://ws.strikeiron.com',
  149. 'LicenseInfo',
  150. array('RegisteredUser' => array('UserID' => $this->_options['username'],
  151. 'Password' => $this->_options['password'])));
  152. }
  153. }
  154. /**
  155. * Transform a method name or method parameters before sending them
  156. * to the remote service. This can be useful for inflection or other
  157. * transforms to give the method call a more PHP-like interface.
  158. *
  159. * @see __call()
  160. * @param string $method Method name called from PHP
  161. * @param mixed $param Parameters passed from PHP
  162. * @return array [$method, $params] for SOAPClient->__soapCall()
  163. */
  164. protected function _transformCall($method, $params)
  165. {
  166. return array(ucfirst($method), $params);
  167. }
  168. /**
  169. * Transform the result returned from a method before returning
  170. * it to the PHP caller. This can be useful for transforming
  171. * the SOAPClient returned result to be more PHP-like.
  172. *
  173. * The $method name and $params passed to the method are provided to
  174. * allow decisions to be made about how to transform the result based
  175. * on what was originally called.
  176. *
  177. * @see __call()
  178. * @param $result Raw result returned from SOAPClient_>__soapCall()
  179. * @param $method Method name that was passed to SOAPClient->__soapCall()
  180. * @param $params Method parameters that were passed to SOAPClient->__soapCall()
  181. * @return mixed Transformed result
  182. */
  183. protected function _transformResult($result, $method, $params)
  184. {
  185. $resultObjectName = "{$method}Result";
  186. if (isset($result->$resultObjectName)) {
  187. $result = $result->$resultObjectName;
  188. }
  189. if (is_object($result)) {
  190. $result = new Zend_Service_StrikeIron_Decorator($result, $resultObjectName);
  191. }
  192. return $result;
  193. }
  194. /**
  195. * Get the WSDL URL for this service.
  196. *
  197. * @return string
  198. */
  199. public function getWsdl()
  200. {
  201. return $this->_options['wsdl'];
  202. }
  203. /**
  204. * Get the SOAP Client instance for this service.
  205. */
  206. public function getSoapClient()
  207. {
  208. return $this->_options['client'];
  209. }
  210. /**
  211. * Get the StrikeIron output headers returned with the last method response.
  212. *
  213. * @return array
  214. */
  215. public function getLastOutputHeaders()
  216. {
  217. return $this->_outputHeaders;
  218. }
  219. /**
  220. * Get the StrikeIron subscription information for this service.
  221. * If any service method was recently called, the subscription info
  222. * should have been returned in the SOAP headers so it is cached
  223. * and returned from the cache. Otherwise, the getRemainingHits()
  224. * method is called as a dummy to get the subscription info headers.
  225. *
  226. * @param boolean $now Force a call to getRemainingHits instead of cache?
  227. * @param string $queryMethod Method that will cause SubscriptionInfo header to be sent
  228. * @return Zend_Service_StrikeIron_Decorator Decorated subscription info
  229. * @throws Zend_Service_StrikeIron_Exception
  230. */
  231. public function getSubscriptionInfo($now = false, $queryMethod = 'GetRemainingHits')
  232. {
  233. if ($now || empty($this->_outputHeaders['SubscriptionInfo'])) {
  234. $this->$queryMethod();
  235. }
  236. // capture subscription info if returned in output headers
  237. if (isset($this->_outputHeaders['SubscriptionInfo'])) {
  238. $info = (object)$this->_outputHeaders['SubscriptionInfo'];
  239. $subscriptionInfo = new Zend_Service_StrikeIron_Decorator($info, 'SubscriptionInfo');
  240. } else {
  241. $msg = 'No SubscriptionInfo header found in last output headers';
  242. /**
  243. * @see Zend_Service_StrikeIron_Exception
  244. */
  245. require_once 'Zend/Service/StrikeIron/Exception.php';
  246. throw new Zend_Service_StrikeIron_Exception($msg);
  247. }
  248. return $subscriptionInfo;
  249. }
  250. }