RedirectResponse::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 8
nop 5
dl 0
loc 17
rs 9.6111
1
<?php
2
3
namespace Lepton\Http\Response;
4
use Lepton\Core\Application;
5
6
class RedirectResponse extends HttpResponse
7
{
8
9
    public $headers;
10
    public $body;
11
12
    public function __construct($url, $htmx = false, $parse = true, $code = 302, $redirect_after = null )
13
    {
14
        if ($this->isLocalUrl($url) && $parse) {
15
            $parsedUrl = Application::getDir()."/".$url;
16
        } else {
17
            $parsedUrl = $url;
18
        }
19
        if ($htmx) {
20
            $headers = ["HX-Redirect" => $parsedUrl];
21
        } else {
22
            $headers = [ "Location" => $parsedUrl ];
23
        }
24
        if(isset($redirect_after)){
25
            $_SESSION["redirect_url"] = $redirect_after;
26
27
        }
28
        parent::__construct(body: '', statusCode: $code, headers: $headers);
29
    }
30
31
    public function isLocalUrl($url)
32
    {
33
        $parsed_url = parse_url($url);
34
        return empty($parsed_url['host']) || $parsed_url['host'] === 'localhost';
35
    }
36
37
38
}
39