Skip to content

Commit 79b9998

Browse files
committed
Extract _getHttpStatusFromHeaders function
1 parent d317f24 commit 79b9998

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/codebird.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,13 +1254,8 @@ protected function _oauth2TokenNoCurl()
12541254
$result .= "\r\n" . $reply;
12551255

12561256
// find HTTP status
1257-
$httpstatus = '500';
1258-
$match = [];
1259-
if (!empty($headers[0]) && preg_match('/HTTP\/\d\.\d (\d{3})/', $headers[0], $match)) {
1260-
$httpstatus = $match[1];
1261-
}
1262-
1263-
$reply = $this->_parseBearerReply($result, $httpstatus);
1257+
$httpstatus = $this->_getHttpStatusFromHeaders($headers);
1258+
$reply = $this->_parseBearerReply($result, $httpstatus);
12641259
return $reply;
12651260
}
12661261

@@ -1269,6 +1264,23 @@ protected function _oauth2TokenNoCurl()
12691264
* General helpers to avoid duplicate code
12701265
*/
12711266

1267+
/**
1268+
* Extract HTTP status code from headers
1269+
*
1270+
* @param array $headers The headers to parse
1271+
*
1272+
* @return string The HTTP status code
1273+
*/
1274+
protected function _getHttpStatusFromHeaders($headers)
1275+
{
1276+
$httpstatus = '500';
1277+
$match = [];
1278+
if (!empty($headers[0]) && preg_match('/HTTP\/\d\.\d (\d{3})/', $headers[0], $match)) {
1279+
$httpstatus = $match[1];
1280+
}
1281+
return $httpstatus;
1282+
}
1283+
12721284
/**
12731285
* Parse oauth2_token reply and set bearer token, if found
12741286
*
@@ -2095,12 +2107,7 @@ protected function _callApiNoCurl(
20952107
$result .= "\r\n" . $reply;
20962108

20972109
// find HTTP status
2098-
$httpstatus = '500';
2099-
$match = [];
2100-
if (!empty($headers[0]) && preg_match('/HTTP\/\d\.\d (\d{3})/', $headers[0], $match)) {
2101-
$httpstatus = $match[1];
2102-
}
2103-
2110+
$httpstatus = $this->_getHttpStatusFromHeaders($headers);
21042111
list($headers, $reply) = $this->_parseApiHeaders($result);
21052112
// TON API & redirects
21062113
$reply = $this->_parseApiReplyPrefillHeaders($headers, $reply);
@@ -2353,12 +2360,7 @@ protected function _callApiStreaming(
23532360
$headers = explode("\r\n", $result);
23542361

23552362
// find HTTP status
2356-
$httpstatus = '500';
2357-
$match = [];
2358-
if (!empty($headers[0]) && preg_match('/HTTP\/\d\.\d (\d{3})/', $headers[0], $match)) {
2359-
$httpstatus = $match[1];
2360-
}
2361-
2363+
$httpstatus = $this->_getHttpStatusFromHeaders($headers);
23622364
list($headers,) = $this->_parseApiHeaders($result);
23632365
$rate = $this->_getRateLimitInfo($headers);
23642366

0 commit comments

Comments
 (0)