RedirectResponse::setTargetUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 27
rs 9.9666
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Scrawler\Http;
12
13
/**
14
 * RedirectResponse on Scrawler namespace.
15
 */
16
class RedirectResponse extends Response
17
{
18
    protected string $targetUrl;
19
20
    /**
21
     * Creates a redirect response so that it conforms to the rules defined for a redirect status code.
22
     *
23
     * @param string $url     The URL to redirect to. The URL should be a full URL, with schema etc.,
24
     *                        but practically every browser redirects on paths only as well
25
     * @param int    $status  The HTTP status code (302 "Found" by default)
26
     * @param array<string,string>  $headers The headers (Location is always set to the given URL)
27
     *
28
     * @throws \InvalidArgumentException
29
     *
30
     */
31
    public function __construct(string $url, int $status = 302, array $headers = [])
32
    {
33
        parent::__construct('', $status, $headers);
34
35
        $this->setTargetUrl($url);
36
37
        if (!$this->isRedirect()) {
38
            throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
39
        }
40
41
        if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) {
42
            $this->headers->remove('cache-control');
43
        }
44
    }
45
46
    /**
47
     * Returns the target URL.
48
     */
49
    public function getTargetUrl(): string
50
    {
51
        return $this->targetUrl;
52
    }
53
54
    /**
55
     * Sets the redirect target of this response.
56
     *
57
     * @return $this
58
     *
59
     * @throws \InvalidArgumentException
60
     */
61
    public function setTargetUrl(string $url): static
62
    {
63
        if ('' === $url) {
64
            throw new \InvalidArgumentException('Cannot redirect to an empty URL.');
65
        }
66
67
        $this->targetUrl = $url;
68
69
        $this->setContent(
70
            sprintf('<!DOCTYPE html>
71
<html>
72
    <head>
73
        <meta charset="UTF-8" />
74
        <meta http-equiv="refresh" content="0;url=\'%1$s\'" />
75
76
        <title>Redirecting to %1$s</title>
77
    </head>
78
    <body>
79
        Redirecting to <a href="%1$s">%1$s</a>.
80
    </body>
81
</html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8'))
82
        );
83
84
        $this->headers->set('Location', $url);
85
        $this->headers->set('Content-Type', 'text/html; charset=utf-8');
86
87
        return $this;
88
    }
89
}
90