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