Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
RedirectResponse | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | /** |
6 | * This file is part of php-fast-forward/http-message. |
7 | * |
8 | * This source file is subject to the license bundled |
9 | * with this source code in the file LICENSE. |
10 | * |
11 | * @link https://github.com/php-fast-forward/http-message |
12 | * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
13 | * @license https://opensource.org/licenses/MIT MIT License |
14 | */ |
15 | |
16 | namespace FastForward\Http\Message; |
17 | |
18 | use Nyholm\Psr7\Response; |
19 | use Psr\Http\Message\UriInterface; |
20 | |
21 | /** |
22 | * Class RedirectResponse. |
23 | * |
24 | * Represents an HTTP redirect response with customizable status codes for temporary or permanent redirects. |
25 | * This class MUST be used for generating HTTP responses that instruct clients to navigate to a different location, |
26 | * by automatically setting the 'Location' header. |
27 | * |
28 | * @package FastForward\Http\Message |
29 | */ |
30 | final class RedirectResponse extends Response |
31 | { |
32 | /** |
33 | * Constructs a new RedirectResponse instance. |
34 | * |
35 | * This constructor SHALL set the 'Location' header and apply the appropriate HTTP status code |
36 | * for temporary (302 Found) or permanent (301 Moved Permanently) redirects. |
37 | * |
38 | * @param string|UriInterface $uri The target URI for redirection. MUST be absolute or relative according to context. |
39 | * @param bool $permanent if true, the response status will be 301 (permanent redirect); otherwise, 302 (temporary redirect) |
40 | * @param array<string, string|string[]> $headers optional additional headers to include in the response |
41 | */ |
42 | public function __construct(string|UriInterface $uri, bool $permanent = false, array $headers = []) |
43 | { |
44 | $headers['Location'] = (string) $uri; |
45 | $status = $permanent ? StatusCode::MovedPermanently : StatusCode::Found; |
46 | |
47 | parent::__construct( |
48 | status: $status->value, |
49 | headers: $headers, |
50 | reason: $status->getReasonPhrase(), |
51 | ); |
52 | } |
53 | } |