Skip to content

Commit

Permalink
Simplify Header::sendStatus()
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Nov 29, 2024
1 parent 51ea126 commit c25161c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
2 changes: 1 addition & 1 deletion formwork/src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function prepare(Request $request): static
*/
public function sendStatus(): void
{
Header::status($this->responseStatus);
Header::sendStatus($this->responseStatus);
}

/**
Expand Down
18 changes: 3 additions & 15 deletions formwork/src/Http/Utils/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,12 @@ class Header

/**
* Send an HTTP response status code
*
* @param bool $send Whether to send status code or return
* @param bool $exit Whether to exit from the script after sending the status code
*
* @return string|null
*/
public static function status(ResponseStatus $responseStatus, bool $send = true, bool $exit = false)
public static function sendStatus(ResponseStatus $responseStatus): void
{
$protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1';
$responseStatus = implode(' ', [$protocol, $responseStatus->value]);
if (!$send) {
return $responseStatus;
}
header($responseStatus);
if ($exit) {
exit;
}
return null;
}

/**
Expand Down Expand Up @@ -60,7 +48,7 @@ public static function contentType(string $mimeType): void
*/
public static function notFound(): void
{
static::status(ResponseStatus::NotFound);
static::sendStatus(ResponseStatus::NotFound);
}

/**
Expand All @@ -73,7 +61,7 @@ public static function redirect(string $uri, ResponseStatus $responseStatus = Re
if ($responseStatus->type() !== ResponseStatusType::Redirection) {
throw new InvalidArgumentException(sprintf('Invalid response status "%s" for redirection, only 3XX statuses are allowed', $responseStatus->value));
}
static::status($responseStatus);
static::sendStatus($responseStatus);
static::send('Location', $uri);
exit;
}
Expand Down

0 comments on commit c25161c

Please sign in to comment.