Redirect::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Models;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Hyde;
9
use Hyde\Pages\InMemoryPage;
10
use Illuminate\Support\Facades\View;
11
12
use function str_ends_with;
13
use function substr;
14
15
/**
16
 * A basic redirect page. Is not discoverable by Hyde, instead you manually need to create the pages.
17
 * Typically, you'll do this in a build task. Pass data a new object, then call the store method.
18
 * The store method will then render the redirect page to the project's site output directory.
19
 * Once viewed in a web browser a meta refresh will redirect the user to the new location.
20
 *
21
 * Since redirects are not discoverable, they also never show up in navigation, sitemaps, etc.
22
 * If you want, you can however add the pages to the HydeKernel route index by adding it
23
 * in the boot method of your AppServiceProvider, or any other suitable location.
24
 * That way, your redirect will be saved by the standard build command.
25
 *
26
 * @example `Redirect::make('foo', 'bar')->store();`
27
 */
28
class Redirect extends InMemoryPage
29
{
30
    public readonly string $path;
31
    public readonly string $destination;
32
    public readonly bool $showText;
33
34
    /**
35
     * Create a new redirect instance that can be saved using the store method.
36
     *
37
     * @param  string  $path  The URI path to redirect from.
38
     * @param  string  $destination  The destination to redirect to.
39
     */
40
    public function __construct(string $path, string $destination, bool $showText = true)
41
    {
42
        $this->path = $this->normalizePath($path);
0 ignored issues
show
Bug introduced by
The property path is declared read-only in Hyde\Support\Models\Redirect.
Loading history...
43
        $this->destination = $destination;
0 ignored issues
show
Bug introduced by
The property destination is declared read-only in Hyde\Support\Models\Redirect.
Loading history...
44
        $this->showText = $showText;
0 ignored issues
show
Bug introduced by
The property showText is declared read-only in Hyde\Support\Models\Redirect.
Loading history...
45
46
        parent::__construct($this->path);
47
    }
48
49
    /**
50
     * Create a new redirect page file in the project's site output directory.
51
     *
52
     * @param  string  $path  The URI path to redirect from.
53
     * @param  string  $destination  The destination to redirect to.
54
     */
55
    public static function create(string $path, string $destination, bool $showText = true): static
56
    {
57
        return (new static($path, $destination, $showText))->store();
58
    }
59
60
    public function compile(): string
61
    {
62
        return View::make('hyde::pages.redirect', [
63
            'destination' => $this->destination,
64
            'showText' => $this->showText,
65
        ])->render();
66
    }
67
68
    public function store(): static
69
    {
70
        Filesystem::putContents(Hyde::sitePath("$this->path.html"), $this->compile());
0 ignored issues
show
Bug introduced by
The method sitePath() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        Filesystem::putContents(Hyde::/** @scrutinizer ignore-call */ sitePath("$this->path.html"), $this->compile());
Loading history...
71
72
        return $this;
73
    }
74
75
    protected function normalizePath(string $path): string
76
    {
77
        if (str_ends_with($path, '.html')) {
78
            return substr($path, 0, -5);
79
        }
80
81
        return $path;
82
    }
83
}
84