Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
24 / 24
RedirectResponse
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
24 / 24
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
13 / 13
 getResponseText
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
11 / 11
1<?php
2
3namespace Miniframe\Response;
4
5use Miniframe\Core\Request;
6use Miniframe\Core\Response;
7
8class RedirectResponse extends Response
9{
10    /**
11     * URL to which we redirect
12     *
13     * @var string
14     */
15    private $url;
16
17    public const
18        MOVED_PERMANENTLY = 301,  // GET methods unchanged. Others may or may not be changed to GET.
19        FOUND = 302,              // GET methods unchanged. Others may or may not be changed to GET.
20        SEE_OTHER = 303,          // GET methods unchanged. Others changed to GET (body lost).
21        TEMPORARY_REDIRECT = 307, // Method and body not changed.
22        PERMANENT_REDIRECT = 308; // Method and body not changed.
23
24    /**
25     * Initializes a new Redirect response
26     *
27     * @param string  $url          URL to redirect to.
28     * @param integer $responseCode One of the HTTP response codes for a redirect.
29     */
30    public function __construct(string $url, int $responseCode = self::FOUND)
31    {
32        $html = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . PHP_EOL
33            . '<html><head>' . PHP_EOL
34            . '<title>' . $responseCode . ' ' . $this->getResponseText($responseCode) . '</title>' . PHP_EOL
35            . '</head><body>' . PHP_EOL
36            . '<p>The document has moved <a href="' . htmlspecialchars($url) . '">here</a>.</p>' . PHP_EOL
37            . '<hr>' . PHP_EOL
38            . (Request::getActual()->getServer('SERVER_SIGNATURE') ?? '') . PHP_EOL
39            . '</body></html>';
40        parent::__construct($html);
41
42        $this->url = $url;
43        $this->setResponseCode($responseCode);
44        $this->addHeader('Location: ' . $this->url);
45    }
46
47    /**
48     * Converts a HTTP status code to it's label
49     *
50     * @param integer $responseCode One of the HTTP response codes for a redirect.
51     *
52     * @return string
53     */
54    private function getResponseText(int $responseCode): string
55    {
56        if ($responseCode == static::MOVED_PERMANENTLY) {
57            return 'Moved permanently';
58        }
59        if ($responseCode == static::FOUND) {
60            return 'Found';
61        }
62        if ($responseCode == static::SEE_OTHER) {
63            return 'See other';
64        }
65        if ($responseCode == static::TEMPORARY_REDIRECT) {
66            return 'Temporary redirect';
67        }
68        if ($responseCode == static::PERMANENT_REDIRECT) {
69            return 'Permanent redirect';
70        }
71        throw new \RuntimeException('Invalid response code for redirects: ' . $responseCode);
72    }
73}