Passed
Push — master ( aa36e5...64fca5 )
by Caen
03:13 queued 12s
created

Redirect   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 13 1
A make() 0 3 1
A store() 0 5 1
A __construct() 0 4 1
1
<?php
2
3
namespace Hyde\Framework\Helpers;
4
5
use Hyde\Framework\Hyde;
6
7
/**
8
 * A basic redirect page. Is not discoverable by Hyde, instead you manually need to create the pages.
9
 * Typically, you'll do this in a build task. Pass data a new object, then call the store method.
10
 *
11
 * @example `Redirect::make('foo', 'bar')->store();`
12
 */
13
class Redirect
14
{
15
    public string $path;
16
    public string $destination;
17
18
    public function __construct(string $path, string $destination)
19
    {
20
        $this->path = $path;
21
        $this->destination = $destination;
22
    }
23
24
    public static function make(string $path, string $destination): static
25
    {
26
        return new static($path, $destination);
27
    }
28
29
    public function render(): string
30
    {
31
        return <<<HTML
32
<!DOCTYPE html>
33
<html lang="en">
34
    <head>
35
        <meta charset="UTF-8" />
36
        <meta http-equiv="refresh" content="0;url='$this->destination'" />
37
38
        <title>Redirecting to $this->destination</title>
39
    </head>
40
    <body>
41
        Redirecting to <a href="$this->destination">$this->destination</a>.
42
    </body>
43
</html>
44
HTML;
45
    }
46
47
    public function store(): static
48
    {
49
        file_put_contents(Hyde::sitePath("$this->path.html"), $this->render());
50
51
        return $this;
52
    }
53
}
54