PageRouter::getHtml()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 1
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\RealtimeCompiler\Routing;
6
7
use Desilva\Microserve\Request;
8
use Desilva\Microserve\Response;
9
use Hyde\Foundation\Facades\Routes;
10
use Hyde\Pages\Concerns\BaseMarkdownPage;
11
use Hyde\Framework\Actions\StaticPageBuilder;
12
use Hyde\RealtimeCompiler\Http\LiveEditController;
13
use Hyde\Framework\Exceptions\RouteNotFoundException;
14
use Hyde\Pages\Concerns\HydePage;
15
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses;
16
use Hyde\RealtimeCompiler\Http\DashboardController;
0 ignored issues
show
Bug introduced by
The type Hyde\RealtimeCompiler\Http\DashboardController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Desilva\Microserve\HtmlResponse;
18
use Hyde\Hyde;
19
20
/**
21
 * Handle routing for a web page request.
22
 */
23
class PageRouter
24
{
25
    use SendsErrorResponses;
26
27
    protected Request $request;
28
29
    public function __construct(Request $request)
30
    {
31
        $this->request = $request;
32
    }
33
34
    protected function handlePageRequest(): Response
35
    {
36
        return new HtmlResponse(200, 'OK', [
37
            'body' => $this->getHtml($this->getPageFromRoute()),
38
        ]);
39
    }
40
41
    protected function normalizePath(string $path): string
42
    {
43
        // If URL ends in .html, strip it
44
        if (str_ends_with($path, '.html')) {
45
            $path = substr($path, 0, -5);
46
        }
47
48
        // If the path is empty, serve the index file
49
        if (empty($path) || $path == '/') {
50
            $path = '/index';
51
        }
52
53
        return ltrim($path, '/');
54
    }
55
56
    protected function getHtml(HydePage $page): string
57
    {
58
        if ($page->identifier === 'index' && DashboardController::enabled()) {
59
            return DashboardController::renderIndexPage($page);
60
        }
61
62
        if (config('hyde.server.save_preview')) {
63
            $contents = file_get_contents(StaticPageBuilder::handle($page));
64
        } else {
65
            Hyde::shareViewData($page);
0 ignored issues
show
Bug introduced by
The method shareViewData() 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

65
            Hyde::/** @scrutinizer ignore-call */ 
66
                  shareViewData($page);
Loading history...
66
67
            $contents = $page->compile();
68
        }
69
70
        if ($page instanceof BaseMarkdownPage && LiveEditController::enabled()) {
71
            $contents = LiveEditController::injectLiveEditScript($contents);
72
        }
73
74
        return $contents;
75
    }
76
77
    public static function handle(Request $request): Response
78
    {
79
        return (new self($request))->handlePageRequest();
80
    }
81
82
    protected function getPageFromRoute(): HydePage
83
    {
84
        try {
85
            return Routes::get($this->normalizePath($this->request->path))->getPage();
86
        } catch (RouteNotFoundException $exception) {
87
            $index = Routes::find($this->normalizePath($this->request->path).'/index');
88
89
            if ($index) {
90
                return $index->getPage();
91
            }
92
93
            throw $exception;
94
        }
95
    }
96
}
97