oxonlinecaller.php

Go to the documentation of this file.
00001 <?php
00002 
00011 abstract class oxOnlineCaller
00012 {
00013 
00014     const ALLOWED_HTTP_FAILED_CALLS_COUNT = 4;
00015 
00017     const CURL_EXECUTION_TIMEOUT = 5;
00018 
00022     private $_oCurl;
00023 
00027     private $_oEmailBuilder;
00028 
00032     private $_oSimpleXml;
00033 
00039     abstract protected function _getXMLDocumentName();
00040 
00046     abstract protected function _getServiceUrl();
00047 
00055     public function __construct(oxCurl $oCurl, oxOnlineServerEmailBuilder $oEmailBuilder, oxSimpleXml $oSimpleXml)
00056     {
00057         $this->_oCurl = $oCurl;
00058         $this->_oEmailBuilder = $oEmailBuilder;
00059         $this->_oSimpleXml = $oSimpleXml;
00060     }
00061 
00069     public function call(oxOnlineRequest $oRequest)
00070     {
00071         $sOutputXml = null;
00072         $iFailedCallsCount = oxRegistry::getConfig()->getSystemConfigParameter('iFailedOnlineCallsCount');
00073         try {
00074             $sXml = $this->_formXMLRequest($oRequest);
00075             $sOutputXml = $this->_executeCurlCall($this->_getServiceUrl(), $sXml);
00076             if ($this->_getCurl()->getStatusCode() != 200) {
00078                 $oException = oxNew('oxException');
00079                 throw $oException;
00080             }
00081             $this->_resetFailedCallsCount($iFailedCallsCount);
00082         } catch (Exception $oEx) {
00083             if ($iFailedCallsCount > self::ALLOWED_HTTP_FAILED_CALLS_COUNT) {
00084                 $sXml = $this->_formEmail($oRequest);
00085                 $this->_sendEmail($sXml);
00086                 $this->_resetFailedCallsCount($iFailedCallsCount);
00087             } else {
00088                 $this->_increaseFailedCallsCount($iFailedCallsCount);
00089             }
00090         }
00091 
00092         return $sOutputXml;
00093     }
00094 
00102     protected function _formEmail($oRequest)
00103     {
00104         return $this->_formXMLRequest($oRequest);
00105     }
00106 
00114     protected function _formXMLRequest($oRequest)
00115     {
00116         return $this->_getSimpleXml()->objectToXml($oRequest, $this->_getXMLDocumentName());
00117     }
00118 
00124     protected function _getSimpleXml()
00125     {
00126         return $this->_oSimpleXml;
00127     }
00128 
00134     protected function _getCurl()
00135     {
00136         return $this->_oCurl;
00137     }
00138 
00144     protected function _getEmailBuilder()
00145     {
00146         return $this->_oEmailBuilder;
00147     }
00148 
00157     private function _executeCurlCall($sUrl, $sXml)
00158     {
00159         $oCurl = $this->_getCurl();
00160         $oCurl->setMethod('POST');
00161         $oCurl->setUrl($sUrl);
00162         $oCurl->setParameters(array('xmlRequest' => $sXml));
00163         $oCurl->setOption(
00164             oxCurl::EXECUTION_TIMEOUT_OPTION,
00165             static::CURL_EXECUTION_TIMEOUT
00166         );
00167         $sOutput = $oCurl->execute();
00168 
00169         return $sOutput;
00170     }
00171 
00177     private function _sendEmail($sBody)
00178     {
00179         $oEmail = $this->_getEmailBuilder()->build($sBody);
00180         $oEmail->send();
00181     }
00182 
00188     private function _resetFailedCallsCount($iFailedOnlineCallsCount)
00189     {
00190         if ($iFailedOnlineCallsCount > 0) {
00191             oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', 0);
00192         }
00193     }
00194 
00200     private function _increaseFailedCallsCount($iFailedOnlineCallsCount)
00201     {
00202         oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', ++$iFailedOnlineCallsCount);
00203     }
00204 }