|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00027 require_once 'Zend/Service/StrikeIron/Decorator.php'; 00028 00029 00037 class Zend_Service_StrikeIron_Base 00038 { 00043 protected $_options = array('username' => null, 00044 'password' => null, 00045 'client' => null, 00046 'options' => null, 00047 'headers' => null, 00048 'wsdl' => null); 00049 00054 protected $_outputHeaders = array(); 00055 00062 public function __construct($options = array()) 00063 { 00064 if (!extension_loaded('soap')) { 00068 require_once 'Zend/Service/StrikeIron/Exception.php'; 00069 throw new Zend_Service_StrikeIron_Exception('SOAP extension is not enabled'); 00070 } 00071 00072 $this->_options = array_merge($this->_options, $options); 00073 00074 $this->_initSoapHeaders(); 00075 $this->_initSoapClient(); 00076 } 00077 00087 public function __call($method, $params) 00088 { 00089 // prepare method name and parameters for soap call 00090 list($method, $params) = $this->_transformCall($method, $params); 00091 $params = isset($params[0]) ? array($params[0]) : array(); 00092 00093 // make soap call, capturing the result and output headers 00094 try { 00095 $result = $this->_options['client']->__soapCall($method, 00096 $params, 00097 $this->_options['options'], 00098 $this->_options['headers'], 00099 $this->_outputHeaders); 00100 } catch (Exception $e) { 00101 $message = get_class($e) . ': ' . $e->getMessage(); 00105 require_once 'Zend/Service/StrikeIron/Exception.php'; 00106 throw new Zend_Service_StrikeIron_Exception($message, $e->getCode(), $e); 00107 } 00108 00109 // transform/decorate the result and return it 00110 $result = $this->_transformResult($result, $method, $params); 00111 return $result; 00112 } 00113 00119 protected function _initSoapClient() 00120 { 00121 if (! isset($this->_options['options'])) { 00122 $this->_options['options'] = array(); 00123 } 00124 00125 if (! isset($this->_options['client'])) { 00126 $this->_options['client'] = new SoapClient($this->_options['wsdl'], 00127 $this->_options['options']); 00128 } 00129 } 00130 00137 protected function _initSoapHeaders() 00138 { 00139 // validate headers and check if LicenseInfo was given 00140 $foundLicenseInfo = false; 00141 if (isset($this->_options['headers'])) { 00142 if (! is_array($this->_options['headers'])) { 00143 $this->_options['headers'] = array($this->_options['headers']); 00144 } 00145 00146 foreach ($this->_options['headers'] as $header) { 00147 if (! $header instanceof SoapHeader) { 00151 require_once 'Zend/Service/StrikeIron/Exception.php'; 00152 throw new Zend_Service_StrikeIron_Exception('Header must be instance of SoapHeader'); 00153 } else if ($header->name == 'LicenseInfo') { 00154 $foundLicenseInfo = true; 00155 break; 00156 } 00157 } 00158 } else { 00159 $this->_options['headers'] = array(); 00160 } 00161 00162 // add default LicenseInfo header if a custom one was not supplied 00163 if (! $foundLicenseInfo) { 00164 $this->_options['headers'][] = new SoapHeader('http://ws.strikeiron.com', 00165 'LicenseInfo', 00166 array('RegisteredUser' => array('UserID' => $this->_options['username'], 00167 'Password' => $this->_options['password']))); 00168 } 00169 } 00170 00181 protected function _transformCall($method, $params) 00182 { 00183 return array(ucfirst($method), $params); 00184 } 00185 00201 protected function _transformResult($result, $method, $params) 00202 { 00203 $resultObjectName = "{$method}Result"; 00204 if (isset($result->$resultObjectName)) { 00205 $result = $result->$resultObjectName; 00206 } 00207 if (is_object($result)) { 00208 $result = new Zend_Service_StrikeIron_Decorator($result, $resultObjectName); 00209 } 00210 return $result; 00211 } 00212 00218 public function getWsdl() 00219 { 00220 return $this->_options['wsdl']; 00221 } 00222 00226 public function getSoapClient() 00227 { 00228 return $this->_options['client']; 00229 } 00230 00236 public function getLastOutputHeaders() 00237 { 00238 return $this->_outputHeaders; 00239 } 00240 00253 public function getSubscriptionInfo($now = false, $queryMethod = 'GetRemainingHits') 00254 { 00255 if ($now || empty($this->_outputHeaders['SubscriptionInfo'])) { 00256 $this->$queryMethod(); 00257 } 00258 00259 // capture subscription info if returned in output headers 00260 if (isset($this->_outputHeaders['SubscriptionInfo'])) { 00261 $info = (object)$this->_outputHeaders['SubscriptionInfo']; 00262 $subscriptionInfo = new Zend_Service_StrikeIron_Decorator($info, 'SubscriptionInfo'); 00263 } else { 00264 $msg = 'No SubscriptionInfo header found in last output headers'; 00268 require_once 'Zend/Service/StrikeIron/Exception.php'; 00269 throw new Zend_Service_StrikeIron_Exception($msg); 00270 } 00271 00272 return $subscriptionInfo; 00273 } 00274 }