Skip to content

Commit

Permalink
Update Response.php - Add isSuccessStatusCode and enhance validateSta…
Browse files Browse the repository at this point in the history
…tusCode +semver: minor (#308)

* Update Response.php +semver: minor

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in 21dfa65 according to the output
from PHP CS Fixer.

Details: #308

* Update Response.php

* Update Response.php

* Update Response.php

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in bcf8db6 according to the output
from PHP CS Fixer.

Details: #308

* Update Response.php

* Update src/Response.php

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in 1969cd4 according to the output
from PHP CS Fixer.

Details: #308

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 18, 2025
1 parent 1f2734b commit 24d10d7
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Response
private int $statusCode;
private string $url;
private ?array $headers;
private const HTTP_OK = 200;
private const HTTP_REDIRECT = 300;
private const HTTP_BAD_REQUEST = 400;

private function __construct(bool $success, ?string $body, string $message, int $statusCode, string $url, ?array $headers)
{
Expand Down Expand Up @@ -62,10 +65,30 @@ public function isSuccess(): bool
return $this->success;
}

/**
* Determines if the response indicates a successful outcome with a valid HTTP Status Code
*
* @param bool $includeRedirects Whether to include redirect status codes in the validation.
*
* @return bool True if the response is successful, false otherwise.
*/
public function isSuccessStatusCode(bool $includeRedirects = false): bool
{
if ($includeRedirects === true) {
$maxCode = self::HTTP_BAD_REQUEST;
} else {
$maxCode = self::HTTP_REDIRECT;
}

return $this->isSuccess() === true
&& $this->statusCode >= self::HTTP_OK
&& $this->statusCode < $maxCode;
}

/**
* Retrieves the body of the response.
*
* @return string|null The body of the response, or null if not set.
* @return string|null The response body, or null if not set.
*/
public function getBody(): ?string
{
Expand All @@ -75,7 +98,7 @@ public function getBody(): ?string
/**
* Get the HTTP request body as a decoded JSON object.
*
* @return object|null Returns an object if the body is valid JSON, or null otherwise.
* @return object|null Returns an object if the body is valid JSON or null otherwise.
* @throws JsonException If JSON decoding fails.
*/
public function getBodyAsJson(): ?object
Expand All @@ -95,7 +118,7 @@ public function getBodyAsJson(): ?object
/**
* Get the HTTP request body as a decoded JSON array.
*
* @return array|null Returns an array if the body is valid JSON, or null otherwise.
* @return array|null Returns an array if the body is valid JSON or null otherwise.
* @throws JsonException If JSON decoding fails.
*/
public function getBodyAsArray(): ?array
Expand Down Expand Up @@ -145,7 +168,7 @@ public function getUrl(): string
/**
* Get the headers of the response.
*
* @return array|null An array of headers if available, or null if no headers are set.
* @return array|null An array of headers if available or null if no headers are set.
*/
public function getHeaders(): ?array
{
Expand Down Expand Up @@ -178,17 +201,8 @@ public function ensureSuccessStatus(): void
*/
public function validateStatusCode(bool $includeRedirects = false): void
{
if ($includeRedirects) {
if ($this->statusCode < 200 || $this->statusCode >= 400) {
throw new RequestException("Invalid status code", $this->statusCode);
}

return;
}

if ($this->statusCode < 200 || $this->statusCode >= 300) {
if ($this->isSuccessStatusCode($includeRedirects) === false) {
throw new RequestException("Invalid status code", $this->statusCode);

}
}
/**
Expand Down

0 comments on commit 24d10d7

Please sign in to comment.