Passed
Push — master ( 9755d1...cdd0c1 )
by Caen
04:02 queued 14s
created

DashboardController::button()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 9.488
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\RealtimeCompiler\Http;
6
7
use Hyde\Hyde;
8
use OutOfBoundsException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Framework\Actions\StaticPageBuilder;
11
use Hyde\Framework\Actions\AnonymousViewCompiler;
12
use Desilva\Microserve\Request;
13
use Composer\InstalledVersions;
14
15
use function file_get_contents;
16
use function str_starts_with;
17
use function str_replace;
18
use function array_merge;
19
use function sprintf;
20
use function config;
21
use function app;
22
23
class DashboardController
24
{
25
    public string $title;
26
27
    public function __construct()
28
    {
29
        $this->title = config('hyde.name').' - Dashboard';
30
    }
31
32
    public function show(): string
33
    {
34
        return AnonymousViewCompiler::handle(__DIR__.'/../../resources/dashboard.blade.php', array_merge(
35
            (array) $this, ['dashboard' => $this, 'request' => Request::capture()],
36
        ));
37
    }
38
39
    public function getVersion(): string
40
    {
41
        $version = InstalledVersions::getPrettyVersion('hyde/realtime-compiler');
42
43
        return str_starts_with($version, 'dev-') ? $version : "v$version";
0 ignored issues
show
Bug Best Practice introduced by
The expression return str_starts_with($...$version : 'v'.$version could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $version can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

43
        return str_starts_with(/** @scrutinizer ignore-type */ $version, 'dev-') ? $version : "v$version";
Loading history...
44
    }
45
46
    public function getProjectInformation(): array
47
    {
48
        return [
49
            'Git Version:' => app('git.version'),
50
            'Hyde Version:' => self::getPackageVersion('hyde/hyde'),
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\RealtimeCompiler\Ht...er::getPackageVersion() is not static, but was called statically. ( Ignorable by Annotation )

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

50
            'Hyde Version:' => self::/** @scrutinizer ignore-call */ getPackageVersion('hyde/hyde'),
Loading history...
51
            'Framework Version:' => self::getPackageVersion('hyde/framework'),
52
            'Project Path:' => Hyde::path(),
53
        ];
54
    }
55
56
    /** @return array<string, \Hyde\Support\Models\Route> */
57
    public function getPageList(): array
58
    {
59
        return Hyde::routes()->all();
60
    }
61
62
    public static function enabled(): bool
63
    {
64
        return config('hyde.server.dashboard', true);
65
    }
66
67
    // This method is called from the PageRouter and allows us to serve a dynamic welcome page
68
    public static function renderIndexPage(HydePage $page): string
69
    {
70
        if (config('hyde.server.save_preview')) {
71
            $contents = file_get_contents(StaticPageBuilder::handle($page));
72
        } else {
73
            Hyde::shareViewData($page);
74
75
            $contents = $page->compile();
76
        }
77
78
        // If the page is the default welcome page we inject dashboard components
79
        if (str_contains($contents, 'This is the default homepage')) {
80
            if (config('hyde.server.dashboard.welcome-banner', true)) {
81
                $contents = str_replace("</div>\n            <!-- End Main Hero Content -->",
82
                    sprintf("%s\n</div>\n<!-- End Main Hero Content -->", self::welcomeComponent()),
83
                    $contents);
84
            }
85
86
            if (config('hyde.server.dashboard.welcome-dashboard', true)) {
87
                $contents = str_replace('</body>', sprintf("%s\n</body>", self::welcomeFrame()), $contents);
88
            }
89
90
            if (config('hyde.server.dashboard.button', false)) {
91
                $contents = self::injectDashboardButton($contents);
92
            }
93
        }
94
95
        return $contents;
96
    }
97
98
    protected static function injectDashboardButton(string $contents): string
99
    {
100
        return str_replace('</body>', sprintf('%s</body>', self::button()), $contents);
101
    }
102
103
    protected static function button(): string
104
    {
105
        return <<<'HTML'
106
            <style>
107
                 .dashboard-btn {
108
                    background-image: linear-gradient(to right, #1FA2FF 0%, #12D8FA  51%, #1FA2FF  100%);
109
                    margin: 10px;
110
                    padding: .5rem 1rem;
111
                    text-align: center;
112
                    transition: 0.5s;
113
                    background-size: 200% auto;
114
                    background-position: right center;
115
                    color: white;
116
                    box-shadow: 0 0 20px #162134;
117
                    border-radius: 10px;
118
                    display: block;
119
                    position: absolute;
120
                    right: 1rem;
121
                    top: 1rem
122
                 }
123
124
                 .dashboard-btn:hover {
125
                    background-position: left center;
126
                    color: #fff;
127
                    text-decoration: none;
128
                }
129
            </style>
130
            <a href="/dashboard" class="dashboard-btn">Dashboard</a>
131
        HTML;
132
    }
133
134
    protected static function welcomeComponent(): string
135
    {
136
        $dashboardMessage = config('hyde.server.dashboard.welcome-dashboard', true)
137
            ? '<br>Scroll down to see it, or visit <a href="/dashboard" style="color: #1FA2FF;">/dashboard</a> at any time!' : '';
138
139
        return <<<HTML
140
            <!-- Dashboard Component -->
141
            <section class="text-white">
142
                <hr style="border-width: 1px; max-width: 240px; opacity: .75; margin-top: 30px; margin-bottom: 24px">
143
                <p style="margin-bottom: 8px;">
144
                    <span style="
145
                        background: #1FA2FF;
146
                        background: -webkit-linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
147
                        background: linear-gradient(to right, #1FA2FF, #12D8FA, #1FA2FF);
148
                        padding: 3px 8px;
149
                        border-radius: 25px;
150
                        font-size: 12px;
151
                        text-transform: uppercase;
152
                        font-weight: 600;
153
                    ">New</span> When using the Realtime Compiler, you now have a content dashboard!
154
                    $dashboardMessage
155
                </p>
156
157
                <a href="#dashboard" onclick="document.getElementById('dashboard').scrollIntoView({behavior: 'smooth'}); return false;">
158
                    <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#ffffff"><path d="M0 0h24v24H0z" fill="none"/><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"/></svg>
159
                </a>
160
            </section>
161
            <!-- End Dashboard Component -->
162
        HTML;
163
    }
164
165
    protected static function welcomeFrame(): string
166
    {
167
        return <<<'HTML'
168
            <aside>
169
                <iframe id="dashboard" src="/dashboard?embedded=true" frameborder="0" style="width: 100vw; height: 100vh; position: absolute;"></iframe>
170
            </aside>
171
        HTML;
172
    }
173
174
    protected function getPackageVersion(string $packageName): string
175
    {
176
        try {
177
            $prettyVersion = InstalledVersions::getPrettyVersion($packageName);
178
        } catch (OutOfBoundsException) {
179
            //
180
        }
181
182
        return $prettyVersion ?? 'unreleased';
183
    }
184
}
185