Skip to content

Commit 0fc6f6f

Browse files
authored
Merge pull request #14 from kodedphp/http-status
HTTP status codes refactor
2 parents 4377fd4 + 5404830 commit 0fc6f6f

16 files changed

+269
-256
lines changed

AcceptHeaderNegotiator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use Generator;
2929
use InvalidArgumentException;
30+
use Koded\Http\Interfaces\HttpStatus;
3031

3132
class AcceptHeaderNegotiator
3233
{
@@ -101,7 +102,7 @@ public function __construct(string $header)
101102

102103
if (!empty($type) && !preg_match('~^(\*|[a-z0-9._]+)([/|_-])?(\*|[a-z0-9.\-_+]+)?$~i', $type, $matches)) {
103104
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header),
104-
StatusCode::NOT_ACCEPTABLE);
105+
HttpStatus::NOT_ACCEPTABLE);
105106
}
106107

107108
$this->separator = $matches[2] ?? '/';
@@ -110,7 +111,7 @@ public function __construct(string $header)
110111
if ('*' === $type && '*' !== $subtype) {
111112
// @see https://tools.ietf.org/html/rfc7231#section-5.3.2
112113
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header),
113-
StatusCode::NOT_ACCEPTABLE);
114+
HttpStatus::NOT_ACCEPTABLE);
114115
}
115116

116117
// @see https://tools.ietf.org/html/rfc7540#section-8.1.2

Client/CurlClient.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace Koded\Http\Client;
1414

15-
use Koded\Http\{ClientRequest, ServerResponse, StatusCode};
16-
use Koded\Http\Interfaces\{HttpRequestClient, Response};
15+
use Koded\Http\{ClientRequest, ServerResponse};
16+
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus, Response};
1717
use Throwable;
1818
use function Koded\Http\create_stream;
1919
use function Koded\Stdlib\json_serialize;
@@ -59,14 +59,14 @@ public function read(): Response
5959
if (false === $resource = $this->createResource()) {
6060
return new ServerResponse(
6161
'The HTTP client is not created therefore cannot read anything',
62-
StatusCode::PRECONDITION_FAILED);
62+
HttpStatus::PRECONDITION_FAILED);
6363
}
6464

6565
curl_setopt_array($resource, $this->options);
6666
$response = curl_exec($resource);
6767

6868
if (true === $this->hasError($resource)) {
69-
return (new ServerResponse($this->getCurlError($resource), StatusCode::FAILED_DEPENDENCY))
69+
return (new ServerResponse($this->getCurlError($resource), HttpStatus::FAILED_DEPENDENCY))
7070
->withHeader('Content-Type', 'application/json');
7171
}
7272

@@ -76,7 +76,7 @@ public function read(): Response
7676
$this->responseHeaders
7777
);
7878
} catch (Throwable $e) {
79-
return new ServerResponse($e->getMessage(), StatusCode::INTERNAL_SERVER_ERROR);
79+
return new ServerResponse($e->getMessage(), HttpStatus::INTERNAL_SERVER_ERROR);
8080
} finally {
8181
unset($response);
8282

@@ -193,7 +193,7 @@ protected function getCurlError($resource): string
193193
'uri' => curl_getinfo($resource, CURLINFO_EFFECTIVE_URL),
194194
'message' => curl_strerror(curl_errno($resource)),
195195
'explain' => curl_error($resource),
196-
'code' => StatusCode::FAILED_DEPENDENCY,
196+
'code' => HttpStatus::FAILED_DEPENDENCY,
197197
]);
198198
}
199199

Client/EncodingTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
namespace Koded\Http\Client;
1414

1515
use Exception;
16-
use Koded\Http\Interfaces\HttpRequestClient;
17-
use Koded\Http\StatusCode;
16+
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus};
1817
use Psr\Http\Client\ClientExceptionInterface;
1918

2019

@@ -32,7 +31,7 @@ public function withEncoding(int $type): HttpRequestClient
3231

3332
throw new class(
3433
'Invalid encoding type. Expects 0, PHP_QUERY_RFC1738 or PHP_QUERY_RFC3986',
35-
StatusCode::BAD_REQUEST
34+
HttpStatus::BAD_REQUEST
3635
) extends Exception implements ClientExceptionInterface {};
3736
}
3837
}

Client/PhpClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace Koded\Http\Client;
1414

15-
use Koded\Http\{ClientRequest, ServerResponse, StatusCode};
16-
use Koded\Http\Interfaces\{HttpRequestClient, Response};
15+
use Koded\Http\{ClientRequest, ServerResponse};
16+
use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus, Response};
1717
use Throwable;
1818
use function Koded\Http\create_stream;
1919

@@ -56,7 +56,7 @@ public function read(): Response
5656

5757
try {
5858
if (false === $resource = $this->createResource(stream_context_create(['http' => $this->options]))) {
59-
return new ServerResponse(error_get_last()['message'], StatusCode::FAILED_DEPENDENCY);
59+
return new ServerResponse(error_get_last()['message'], HttpStatus::FAILED_DEPENDENCY);
6060
}
6161

6262
$this->extractFromResponseHeaders($resource, $headers, $statusCode);
@@ -67,7 +67,7 @@ public function read(): Response
6767
$headers
6868
);
6969
} catch (Throwable $e) {
70-
return new ServerResponse($e->getMessage(), StatusCode::INTERNAL_SERVER_ERROR);
70+
return new ServerResponse($e->getMessage(), HttpStatus::INTERNAL_SERVER_ERROR);
7171
} finally {
7272
if (is_resource($resource)) {
7373
fclose($resource);
@@ -174,7 +174,7 @@ protected function extractFromResponseHeaders($response, &$headers, &$statusCode
174174
return false !== stripos($header, 'HTTP/', 0);
175175
});
176176
$statusCode = array_pop($statusCode) ?: 'HTTP/1.1 200 OK';
177-
$statusCode = (int)(explode(' ', $statusCode)[1] ?? StatusCode::OK);
177+
$statusCode = (int)(explode(' ', $statusCode)[1] ?? HttpStatus::OK);
178178

179179
foreach ($_headers as $header) {
180180
[$k, $v] = explode(':', $header, 2) + [1 => null];

Client/Psr18ClientTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
namespace Koded\Http\Client;
1414

1515
use Exception;
16-
use Koded\Http\Interfaces\Response;
17-
use Koded\Http\StatusCode;
16+
use Koded\Http\Interfaces\{HttpStatus, Response};
1817
use Psr\Http\Client\{NetworkExceptionInterface, RequestExceptionInterface};
1918
use Psr\Http\Message\{RequestInterface, ResponseInterface};
2019

@@ -40,7 +39,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
4039
->withBody($request->getBody())
4140
->read();
4241

43-
if ($response->getStatusCode() >= StatusCode::BAD_REQUEST) {
42+
if ($response->getStatusCode() >= HttpStatus::BAD_REQUEST) {
4443
throw new Psr18Exception($response->getBody()->getContents(), $response->getStatusCode(), $this);
4544
}
4645

ClientRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use InvalidArgumentException;
1616
use JsonSerializable;
17-
use Koded\Http\Interfaces\Request;
17+
use Koded\Http\Interfaces\{HttpStatus, Request};
1818
use Psr\Http\Message\{RequestInterface, UriInterface};
1919
use function Koded\Stdlib\json_serialize;
2020

@@ -107,7 +107,7 @@ public function getRequestTarget(): string
107107
public function withRequestTarget($requestTarget): ClientRequest
108108
{
109109
if (preg_match('/\s+/', $requestTarget)) {
110-
throw new InvalidArgumentException(self::E_INVALID_REQUEST_TARGET, StatusCode::BAD_REQUEST);
110+
throw new InvalidArgumentException(self::E_INVALID_REQUEST_TARGET, HttpStatus::BAD_REQUEST);
111111
}
112112

113113
$instance = clone $this;
@@ -172,7 +172,7 @@ protected function setMethod(string $method, RequestInterface $instance): Reques
172172
protected function assertSafeMethod(): ?ServerResponse
173173
{
174174
if ($this->isSafeMethod() && $this->getBody()->getSize() > 0) {
175-
return new ServerResponse(self::E_SAFE_METHODS_WITH_BODY, StatusCode::BAD_REQUEST);
175+
return new ServerResponse(self::E_SAFE_METHODS_WITH_BODY, HttpStatus::BAD_REQUEST);
176176
}
177177

178178
return null;

HeaderTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Koded\Http;
1414

1515
use InvalidArgumentException;
16+
use Koded\Http\Interfaces\HttpStatus;
1617
use Throwable;
1718

1819

@@ -198,12 +199,12 @@ protected function normalizeHeaderName($name): string
198199
$name = str_replace(["\r", "\n", "\t"], '', trim($name));
199200
} catch (Throwable $e) {
200201
throw new InvalidArgumentException(
201-
sprintf('Header name must be a string, %s given', gettype($name)), StatusCode::BAD_REQUEST
202+
sprintf('Header name must be a string, %s given', gettype($name)), HttpStatus::BAD_REQUEST
202203
);
203204
}
204205

205206
if ('' === $name) {
206-
throw new InvalidArgumentException('Empty header name', StatusCode::BAD_REQUEST);
207+
throw new InvalidArgumentException('Empty header name', HttpStatus::BAD_REQUEST);
207208
}
208209

209210
return $name;
@@ -227,15 +228,15 @@ protected function normalizeHeaderValue(string $name, $value): array
227228
break;
228229
default:
229230
throw new InvalidArgumentException(
230-
sprintf('Invalid header value, expects string or array, "%s" given', $type), StatusCode::BAD_REQUEST
231+
sprintf('Invalid header value, expects string or array, "%s" given', $type), HttpStatus::BAD_REQUEST
231232
);
232233
}
233234

234235
if (empty($value = array_map(function($v) {
235236
return trim(preg_replace('/\s+/', ' ', $v));
236237
}, $value))) {
237238
throw new InvalidArgumentException(
238-
sprintf('The value for header "%s" cannot be empty', $name), StatusCode::BAD_REQUEST
239+
sprintf('The value for header "%s" cannot be empty', $name), HttpStatus::BAD_REQUEST
239240
);
240241
}
241242

0 commit comments

Comments
 (0)