00001 <?php 00002 00011 abstract class oxOnlineCaller 00012 { 00013 00014 const ALLOWED_HTTP_FAILED_CALLS_COUNT = 4; 00015 00019 private $_oCurl; 00020 00024 private $_oEmailBuilder; 00025 00029 private $_oSimpleXml; 00030 00036 abstract protected function _getXMLDocumentName(); 00037 00043 abstract protected function _getServiceUrl(); 00044 00052 public function __construct(oxCurl $oCurl, oxOnlineServerEmailBuilder $oEmailBuilder, oxSimpleXml $oSimpleXml) 00053 { 00054 $this->_oCurl = $oCurl; 00055 $this->_oEmailBuilder = $oEmailBuilder; 00056 $this->_oSimpleXml = $oSimpleXml; 00057 } 00058 00066 public function call(oxOnlineRequest $oRequest) 00067 { 00068 $sOutputXml = null; 00069 $iFailedCallsCount = oxRegistry::getConfig()->getSystemConfigParameter('iFailedOnlineCallsCount'); 00070 try { 00071 $sXml = $this->_formXMLRequest($oRequest); 00072 $sOutputXml = $this->_executeCurlCall($this->_getServiceUrl(), $sXml); 00073 if ($this->_getCurl()->getStatusCode() != 200) { 00075 $oException = oxNew('oxException'); 00076 throw $oException; 00077 } 00078 $this->_resetFailedCallsCount($iFailedCallsCount); 00079 } catch (Exception $oEx) { 00080 if ($iFailedCallsCount > self::ALLOWED_HTTP_FAILED_CALLS_COUNT) { 00081 $sXml = $this->_formEmail($oRequest); 00082 $this->_sendEmail($sXml); 00083 $this->_resetFailedCallsCount($iFailedCallsCount); 00084 } else { 00085 $this->_increaseFailedCallsCount($iFailedCallsCount); 00086 } 00087 } 00088 00089 return $sOutputXml; 00090 } 00091 00099 protected function _formEmail($oRequest) 00100 { 00101 return $this->_formXMLRequest($oRequest); 00102 } 00103 00111 protected function _formXMLRequest($oRequest) 00112 { 00113 return $this->_getSimpleXml()->objectToXml($oRequest, $this->_getXMLDocumentName()); 00114 } 00115 00121 protected function _getSimpleXml() 00122 { 00123 return $this->_oSimpleXml; 00124 } 00125 00131 protected function _getCurl() 00132 { 00133 return $this->_oCurl; 00134 } 00135 00141 protected function _getEmailBuilder() 00142 { 00143 return $this->_oEmailBuilder; 00144 } 00145 00154 private function _executeCurlCall($sUrl, $sXml) 00155 { 00156 $oCurl = $this->_getCurl(); 00157 $oCurl->setMethod('POST'); 00158 $oCurl->setUrl($sUrl); 00159 $oCurl->setParameters(array('xmlRequest' => $sXml)); 00160 $sOutput = $oCurl->execute(); 00161 00162 return $sOutput; 00163 } 00164 00170 private function _sendEmail($sBody) 00171 { 00172 $oEmail = $this->_getEmailBuilder()->build($sBody); 00173 $oEmail->send(); 00174 } 00175 00181 private function _resetFailedCallsCount($iFailedOnlineCallsCount) 00182 { 00183 if ($iFailedOnlineCallsCount > 0) { 00184 oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', 0); 00185 } 00186 } 00187 00193 private function _increaseFailedCallsCount($iFailedOnlineCallsCount) 00194 { 00195 oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', ++$iFailedOnlineCallsCount); 00196 } 00197 }