/library/Zend/Service/StrikeIron/Base.php

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