Skip to content

Commit fd4f237

Browse files
committed
Refactor executeRequest method for better testing possibilities
1 parent 3d9fe02 commit fd4f237

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

src/PrestashopWebServiceLibrary.php

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,23 @@ protected function checkStatusCode($status_code)
102102
}
103103

104104
/**
105-
* Handles a CURL request to PrestaShop Webservice. Can throw exception.
105+
* Throws exception if prestashop version is not supported
106+
* @param int $version The prestashop version
107+
* @throws PrestaShopWebserviceException
108+
*/
109+
public function isPrestashopVersionSupported($version)
110+
{
111+
if (version_compare($version, self::PS_COMPATIBLE_VERSION_MIN, '>=') === false ||
112+
version_compare($version, self::PS_COMPATIBLE_VERSION_MAX, '<=') === false
113+
) {
114+
$exception = 'This library is not compatible with this version of PrestaShop. ';
115+
$exception.= 'Please upgrade/downgrade this library';
116+
throw new PrestaShopWebserviceException($exception);
117+
}
118+
}
119+
120+
/**
121+
* Prepares and validate a CURL request to PrestaShop Webservice. Can throw exception.
106122
* @param string $url Resource name
107123
* @param mixed $curl_params CURL parameters (sent to curl_set_opt)
108124
* @return array status_code, response
@@ -119,8 +135,6 @@ protected function executeRequest($url, $curl_params = array())
119135
CURLOPT_HTTPHEADER => array( 'Expect:' )
120136
);
121137

122-
$session = curl_init($url);
123-
124138
$curl_options = array();
125139
foreach ($defaultParams as $defkey => $defval) {
126140
if (isset($curl_params[$defkey])) {
@@ -135,55 +149,79 @@ protected function executeRequest($url, $curl_params = array())
135149
}
136150
}
137151

138-
curl_setopt_array($session, $curl_options);
139-
$response = curl_exec($session);
152+
list($response, $info, $error) = $this->executeCurl($url, $curl_options);
153+
154+
$status_code = $info['http_code'];
155+
if ($status_code === 0 || $error) {
156+
throw new PrestaShopWebserviceException('CURL Error: '.$error);
157+
}
140158

141-
$index = strpos($response, "\r\n\r\n");
142-
if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
159+
$index = $info['header_size'];
160+
if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] !== 'HEAD') {
143161
throw new PrestaShopWebserviceException('Bad HTTP response');
144162
}
145163

146164
$header = substr($response, 0, $index);
147-
$body = substr($response, $index + 4);
148-
149-
$headerArrayTmp = explode("\n", $header);
165+
$body = substr($response, $index);
150166

151167
$headerArray = array();
152-
foreach ($headerArrayTmp as &$headerItem) {
153-
$tmp = explode(':', $headerItem);
154-
$tmp = array_map('trim', $tmp);
155-
if (count($tmp) == 2) {
168+
foreach (explode("\n", $header) as $headerItem) {
169+
$tmp = explode(':', $headerItem, 2);
170+
if (count($tmp) === 2) {
171+
$tmp = array_map('trim', $tmp);
156172
$headerArray[$tmp[0]] = $tmp[1];
157173
}
158174
}
159175

160176
if (array_key_exists('PSWS-Version', $headerArray)) {
177+
$this->isPrestashopVersionSupported($headerArray['PSWS-Version']);
161178
$this->version = $headerArray['PSWS-Version'];
162-
if (version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSION_MIN, $headerArray['PSWS-Version']) == 1 ||
163-
version_compare(PrestaShopWebservice::PS_COMPATIBLE_VERSION_MAX, $headerArray['PSWS-Version']) == -1
164-
) {
165-
$exception = 'This library is not compatible with this version of PrestaShop. ';
166-
$exception.= 'Please upgrade/downgrade this library';
167-
throw new PrestaShopWebserviceException($exception);
168-
}
169179
}
170180

171-
$this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
181+
$this->printDebug('HTTP REQUEST HEADER', $info['request_header']);
172182
$this->printDebug('HTTP RESPONSE HEADER', $header);
173183

174-
$status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
175-
if ($status_code === 0) {
176-
throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session));
177-
}
178-
curl_close($session);
179-
180184
if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') {
181185
$this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
182186
}
183187
if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
184188
$this->printDebug('RETURN HTTP BODY', $body);
185189
}
186-
return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
190+
191+
return array(
192+
'status_code' => $status_code,
193+
'response' => $body,
194+
'header' => $header,
195+
'headers' => $headerArray
196+
);
197+
}
198+
199+
/**
200+
* Executes the CURL request to PrestaShop Webservice.
201+
* @param string $url Resource name
202+
* @param mixed $options CURL parameters (sent to curl_setopt_array)
203+
* @return array response, info
204+
*/
205+
protected function executeCurl($url, array $options = array())
206+
{
207+
$session = curl_init($url);
208+
209+
if (count($options)) {
210+
curl_setopt_array($session, $options);
211+
}
212+
213+
$response = curl_exec($session);
214+
215+
$error = false;
216+
if ($response === false) {
217+
$error = curl_error($session);
218+
} else {
219+
$info = curl_getinfo($session);
220+
}
221+
222+
curl_close($session);
223+
224+
return array($response, $info, $error);
187225
}
188226

189227
public function printDebug($title, $content)

0 commit comments

Comments
 (0)