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