File indexing completed on 2024-12-22 05:37:04
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage StrikeIron 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 0024 /** 0025 * @see Zend_Service_StrikeIron_Decorator 0026 */ 0027 // require_once 'Zend/Service/StrikeIron/Decorator.php'; 0028 0029 0030 /** 0031 * @category Zend 0032 * @package Zend_Service 0033 * @subpackage StrikeIron 0034 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0035 * @license http://framework.zend.com/license/new-bsd New BSD License 0036 */ 0037 class Zend_Service_StrikeIron_Base 0038 { 0039 /** 0040 * Configuration options 0041 * @param array 0042 */ 0043 protected $_options = array('username' => null, 0044 'password' => null, 0045 'client' => null, 0046 'options' => null, 0047 'headers' => null, 0048 'wsdl' => null); 0049 0050 /** 0051 * Output headers returned by the last call to SOAPClient->__soapCall() 0052 * @param array 0053 */ 0054 protected $_outputHeaders = array(); 0055 0056 /** 0057 * Class constructor 0058 * 0059 * @param array $options Key/value pair options 0060 * @throws Zend_Service_StrikeIron_Exception 0061 */ 0062 public function __construct($options = array()) 0063 { 0064 if (!extension_loaded('soap')) { 0065 /** 0066 * @see Zend_Service_StrikeIron_Exception 0067 */ 0068 // require_once 'Zend/Service/StrikeIron/Exception.php'; 0069 throw new Zend_Service_StrikeIron_Exception('SOAP extension is not enabled'); 0070 } 0071 0072 $this->_options = array_merge($this->_options, $options); 0073 0074 $this->_initSoapHeaders(); 0075 $this->_initSoapClient(); 0076 } 0077 0078 /** 0079 * Proxy method calls to the SOAPClient instance, transforming method 0080 * calls and responses for convenience. 0081 * 0082 * @param string $method Method name 0083 * @param array $params Parameters for method 0084 * @return mixed Result 0085 * @throws Zend_Service_StrikeIron_Exception 0086 */ 0087 public function __call($method, $params) 0088 { 0089 // prepare method name and parameters for soap call 0090 list($method, $params) = $this->_transformCall($method, $params); 0091 $params = isset($params[0]) ? array($params[0]) : array(); 0092 0093 // make soap call, capturing the result and output headers 0094 try { 0095 $result = $this->_options['client']->__soapCall($method, 0096 $params, 0097 $this->_options['options'], 0098 $this->_options['headers'], 0099 $this->_outputHeaders); 0100 } catch (Exception $e) { 0101 $message = get_class($e) . ': ' . $e->getMessage(); 0102 /** 0103 * @see Zend_Service_StrikeIron_Exception 0104 */ 0105 // require_once 'Zend/Service/StrikeIron/Exception.php'; 0106 throw new Zend_Service_StrikeIron_Exception($message, $e->getCode(), $e); 0107 } 0108 0109 // transform/decorate the result and return it 0110 $result = $this->_transformResult($result, $method, $params); 0111 return $result; 0112 } 0113 0114 /** 0115 * Initialize the SOAPClient instance 0116 * 0117 * @return void 0118 */ 0119 protected function _initSoapClient() 0120 { 0121 if (! isset($this->_options['options'])) { 0122 $this->_options['options'] = array(); 0123 } 0124 0125 if (! isset($this->_options['client'])) { 0126 $this->_options['client'] = new SoapClient($this->_options['wsdl'], 0127 $this->_options['options']); 0128 } 0129 } 0130 0131 /** 0132 * Initialize the headers to pass to SOAPClient->__soapCall() 0133 * 0134 * @return void 0135 * @throws Zend_Service_StrikeIron_Exception 0136 */ 0137 protected function _initSoapHeaders() 0138 { 0139 // validate headers and check if LicenseInfo was given 0140 $foundLicenseInfo = false; 0141 if (isset($this->_options['headers'])) { 0142 if (! is_array($this->_options['headers'])) { 0143 $this->_options['headers'] = array($this->_options['headers']); 0144 } 0145 0146 foreach ($this->_options['headers'] as $header) { 0147 if (! $header instanceof SoapHeader) { 0148 /** 0149 * @see Zend_Service_StrikeIron_Exception 0150 */ 0151 // require_once 'Zend/Service/StrikeIron/Exception.php'; 0152 throw new Zend_Service_StrikeIron_Exception('Header must be instance of SoapHeader'); 0153 } else if ($header->name == 'LicenseInfo') { 0154 $foundLicenseInfo = true; 0155 break; 0156 } 0157 } 0158 } else { 0159 $this->_options['headers'] = array(); 0160 } 0161 0162 // add default LicenseInfo header if a custom one was not supplied 0163 if (! $foundLicenseInfo) { 0164 $this->_options['headers'][] = new SoapHeader('http://ws.strikeiron.com', 0165 'LicenseInfo', 0166 array('RegisteredUser' => array('UserID' => $this->_options['username'], 0167 'Password' => $this->_options['password']))); 0168 } 0169 } 0170 0171 /** 0172 * Transform a method name or method parameters before sending them 0173 * to the remote service. This can be useful for inflection or other 0174 * transforms to give the method call a more PHP-like interface. 0175 * 0176 * @see __call() 0177 * @param string $method Method name called from PHP 0178 * @param mixed $param Parameters passed from PHP 0179 * @return array [$method, $params] for SOAPClient->__soapCall() 0180 */ 0181 protected function _transformCall($method, $params) 0182 { 0183 return array(ucfirst($method), $params); 0184 } 0185 0186 /** 0187 * Transform the result returned from a method before returning 0188 * it to the PHP caller. This can be useful for transforming 0189 * the SOAPClient returned result to be more PHP-like. 0190 * 0191 * The $method name and $params passed to the method are provided to 0192 * allow decisions to be made about how to transform the result based 0193 * on what was originally called. 0194 * 0195 * @see __call() 0196 * @param object $result Raw result returned from SOAPClient_>__soapCall() 0197 * @param string $method Method name that was passed to SOAPClient->__soapCall() 0198 * @param array $params Method parameters that were passed to SOAPClient->__soapCall() 0199 * @return mixed Transformed result 0200 */ 0201 protected function _transformResult($result, $method, $params) 0202 { 0203 $resultObjectName = "{$method}Result"; 0204 if (isset($result->$resultObjectName)) { 0205 $result = $result->$resultObjectName; 0206 } 0207 if (is_object($result)) { 0208 $result = new Zend_Service_StrikeIron_Decorator($result, $resultObjectName); 0209 } 0210 return $result; 0211 } 0212 0213 /** 0214 * Get the WSDL URL for this service. 0215 * 0216 * @return string 0217 */ 0218 public function getWsdl() 0219 { 0220 return $this->_options['wsdl']; 0221 } 0222 0223 /** 0224 * Get the SOAP Client instance for this service. 0225 */ 0226 public function getSoapClient() 0227 { 0228 return $this->_options['client']; 0229 } 0230 0231 /** 0232 * Get the StrikeIron output headers returned with the last method response. 0233 * 0234 * @return array 0235 */ 0236 public function getLastOutputHeaders() 0237 { 0238 return $this->_outputHeaders; 0239 } 0240 0241 /** 0242 * Get the StrikeIron subscription information for this service. 0243 * If any service method was recently called, the subscription info 0244 * should have been returned in the SOAP headers so it is cached 0245 * and returned from the cache. Otherwise, the getRemainingHits() 0246 * method is called as a dummy to get the subscription info headers. 0247 * 0248 * @param boolean $now Force a call to getRemainingHits instead of cache? 0249 * @param string $queryMethod Method that will cause SubscriptionInfo header to be sent 0250 * @return Zend_Service_StrikeIron_Decorator Decorated subscription info 0251 * @throws Zend_Service_StrikeIron_Exception 0252 */ 0253 public function getSubscriptionInfo($now = false, $queryMethod = 'GetRemainingHits') 0254 { 0255 if ($now || empty($this->_outputHeaders['SubscriptionInfo'])) { 0256 $this->$queryMethod(); 0257 } 0258 0259 // capture subscription info if returned in output headers 0260 if (isset($this->_outputHeaders['SubscriptionInfo'])) { 0261 $info = (object)$this->_outputHeaders['SubscriptionInfo']; 0262 $subscriptionInfo = new Zend_Service_StrikeIron_Decorator($info, 'SubscriptionInfo'); 0263 } else { 0264 $msg = 'No SubscriptionInfo header found in last output headers'; 0265 /** 0266 * @see Zend_Service_StrikeIron_Exception 0267 */ 0268 // require_once 'Zend/Service/StrikeIron/Exception.php'; 0269 throw new Zend_Service_StrikeIron_Exception($msg); 0270 } 0271 0272 return $subscriptionInfo; 0273 } 0274 }