Passed
Push — master ( c11fa8...cb30dd )
by Caen
03:11 queued 12s
created

Render::setPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 2
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Models;
6
7
use Hyde\Pages\Concerns\HydePage;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Support\Facades\View;
10
use InvalidArgumentException;
11
12
/**
13
 * Contains data for the current page being rendered/compiled.
14
 *
15
 * All public data here will be available in the Blade views through @see ManagesViewData::shareViewData().
16
 *
17
 * @see \Hyde\Support\Facades\Render
18
 * @see \Hyde\Framework\Testing\Feature\RenderHelperTest
19
 */
20
class Render implements Arrayable
21
{
22
    protected HydePage $page;
23
    protected Route $currentRoute;
24
    protected string $currentPage;
25
26
    public function setPage(HydePage $page): void
27
    {
28
        $this->page = $page;
29
        $this->currentRoute = $page->getRoute();
30
        $this->currentPage = $page->getRouteKey();
31
32
        $this->shareToView();
33
    }
34
35
    public function getPage(): ?HydePage
36
    {
37
        return $this->page ?? self::handleFallback('page');
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Support\Models\Render::handleFallback() has been deprecated. ( Ignorable by Annotation )

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

37
        return $this->page ?? /** @scrutinizer ignore-deprecated */ self::handleFallback('page');
Loading history...
38
    }
39
40
    public function getCurrentRoute(): ?Route
41
    {
42
        return $this->currentRoute ?? self::handleFallback('currentRoute');
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Support\Models\Render::handleFallback() has been deprecated. ( Ignorable by Annotation )

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

42
        return $this->currentRoute ?? /** @scrutinizer ignore-deprecated */ self::handleFallback('currentRoute');
Loading history...
43
    }
44
45
    public function getCurrentPage(): ?string
46
    {
47
        return $this->currentPage ?? self::handleFallback('currentPage');
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Support\Models\Render::handleFallback() has been deprecated. ( Ignorable by Annotation )

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

47
        return $this->currentPage ?? /** @scrutinizer ignore-deprecated */ self::handleFallback('currentPage');
Loading history...
48
    }
49
50
    public function shareToView(): void
51
    {
52
        View::share($this->toArray());
53
    }
54
55
    public function share(string $key, mixed $value): void
56
    {
57
        if (property_exists($this, $key)) {
58
            $this->{$key} = $value;
59
            $this->shareToView();
60
        } else {
61
            throw new InvalidArgumentException("Property '$key' does not exist on ".self::class);
62
        }
63
    }
64
65
    public function clearData(): void
66
    {
67
        unset($this->page, $this->currentRoute, $this->currentPage);
68
        View::share(['page' => null, 'currentRoute' => null, 'currentPage' => null]);
69
    }
70
71
    public function toArray(): array
72
    {
73
        return [
74
            'render' => $this,
75
            'page' => $this->getPage(),
76
            'currentRoute' => $this->getCurrentRoute(),
77
            'currentPage' => $this->getCurrentPage(),
78
        ];
79
    }
80
81
    /**
82
     * @deprecated
83
     * @codeCoverageIgnore
84
     */
85
    protected static function handleFallback(string $property): mixed
86
    {
87
        $shared = View::shared($property);
88
89
        if ($shared !== null) {
90
            trigger_error("Setting page rendering data via the view facade is deprecated. Use `Render::share('$property', \$value)` instead", E_USER_DEPRECATED);
91
        }
92
93
        return $shared;
94
    }
95
}
96