OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxonlinecaller.php
Go to the documentation of this file.
1 <?php
2 
11 abstract class oxOnlineCaller
12 {
13 
15 
18 
22  private $_oCurl;
23 
27  private $_oEmailBuilder;
28 
32  private $_oSimpleXml;
33 
39  abstract protected function _getXMLDocumentName();
40 
46  abstract protected function _getServiceUrl();
47 
55  public function __construct(oxCurl $oCurl, oxOnlineServerEmailBuilder $oEmailBuilder, oxSimpleXml $oSimpleXml)
56  {
57  $this->_oCurl = $oCurl;
58  $this->_oEmailBuilder = $oEmailBuilder;
59  $this->_oSimpleXml = $oSimpleXml;
60  }
61 
69  public function call(oxOnlineRequest $oRequest)
70  {
71  $sOutputXml = null;
72  $iFailedCallsCount = oxRegistry::getConfig()->getSystemConfigParameter('iFailedOnlineCallsCount');
73  try {
74  $sXml = $this->_formXMLRequest($oRequest);
75  $sOutputXml = $this->_executeCurlCall($this->_getServiceUrl(), $sXml);
76  if ($this->_getCurl()->getStatusCode() != 200) {
78  $oException = oxNew('oxException');
79  throw $oException;
80  }
81  $this->_resetFailedCallsCount($iFailedCallsCount);
82  } catch (Exception $oEx) {
83  if ($iFailedCallsCount > self::ALLOWED_HTTP_FAILED_CALLS_COUNT) {
84  $sXml = $this->_formEmail($oRequest);
85  $this->_sendEmail($sXml);
86  $this->_resetFailedCallsCount($iFailedCallsCount);
87  } else {
88  $this->_increaseFailedCallsCount($iFailedCallsCount);
89  }
90  }
91 
92  return $sOutputXml;
93  }
94 
102  protected function _formEmail($oRequest)
103  {
104  return $this->_formXMLRequest($oRequest);
105  }
106 
114  protected function _formXMLRequest($oRequest)
115  {
116  return $this->_getSimpleXml()->objectToXml($oRequest, $this->_getXMLDocumentName());
117  }
118 
124  protected function _getSimpleXml()
125  {
126  return $this->_oSimpleXml;
127  }
128 
134  protected function _getCurl()
135  {
136  return $this->_oCurl;
137  }
138 
144  protected function _getEmailBuilder()
145  {
146  return $this->_oEmailBuilder;
147  }
148 
157  private function _executeCurlCall($sUrl, $sXml)
158  {
159  $oCurl = $this->_getCurl();
160  $oCurl->setMethod('POST');
161  $oCurl->setUrl($sUrl);
162  $oCurl->setParameters(array('xmlRequest' => $sXml));
163  $oCurl->setOption(
165  static::CURL_EXECUTION_TIMEOUT
166  );
167  $sOutput = $oCurl->execute();
168 
169  return $sOutput;
170  }
171 
177  private function _sendEmail($sBody)
178  {
179  $oEmail = $this->_getEmailBuilder()->build($sBody);
180  $oEmail->send();
181  }
182 
188  private function _resetFailedCallsCount($iFailedOnlineCallsCount)
189  {
190  if ($iFailedOnlineCallsCount > 0) {
191  oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', 0);
192  }
193  }
194 
200  private function _increaseFailedCallsCount($iFailedOnlineCallsCount)
201  {
202  oxRegistry::getConfig()->saveSystemConfigParameter('int', 'iFailedOnlineCallsCount', ++$iFailedOnlineCallsCount);
203  }
204 }