Passed
Push — master ( e54c12...dd8329 )
by Caen
03:56 queued 15s
created

RouteListItem::href()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Internal;
6
7
use Hyde\Hyde;
8
use Hyde\Pages\InMemoryPage;
9
use Hyde\Support\Models\Route;
10
use Hyde\Console\Concerns\Command;
11
12
use function filled;
13
use function sprintf;
14
use function file_exists;
15
use function class_basename;
16
use function str_starts_with;
17
18
/**
19
 * @internal This class is internal and should not be depended on outside the HydePHP framework code.
20
 */
21
class RouteListItem
22
{
23
    protected Route $route;
24
25
    public static function format(Route $route): array
26
    {
27
        $item = new static($route);
28
29
        return [
30
            'page_type' => $item->stylePageType($route->getPageClass()),
31
            'source_file' => $item->styleSourcePath($route->getSourcePath()),
32
            'output_file' => $item->styleOutputPath($route->getOutputPath()),
33
            'route_key' => $item->styleRouteKey($route->getRouteKey()),
34
        ];
35
    }
36
37
    protected function __construct(Route $route)
38
    {
39
        $this->route = $route;
40
    }
41
42
    protected function stylePageType(string $class): string
43
    {
44
        $type = $this->getPageType($class);
45
46
        $page = $this->route->getPage();
47
48
        /** @experimental The typeLabel macro is experimental */
49
        if ($page instanceof InMemoryPage && $page->hasMacro('typeLabel')) {
50
            $type .= sprintf(' <fg=gray>(%s)</>', (string) $page->__call('typeLabel', []));
51
        }
52
53
        return $type;
54
    }
55
56
    protected function styleSourcePath(string $path): string
57
    {
58
        if ($this->getSourcePath($path) === 'none') {
59
            return '<fg=gray>none</>';
60
        }
61
62
        return file_exists(Hyde::path($path))
63
            ? $this->href(Command::fileLink(Hyde::path($path)), $this->getSourcePath($path))
64
            : $this->getSourcePath($path);
65
    }
66
67
    protected function styleOutputPath(string $path): string
68
    {
69
        return file_exists(Hyde::sitePath($path))
70
            ? $this->href(Command::fileLink(Hyde::sitePath($path)), $this->getOutputPath($path))
71
            : $this->getOutputPath($path);
72
    }
73
74
    protected function styleRouteKey(string $key): string
75
    {
76
        return $key;
77
    }
78
79
    protected function getPageType(string $class): string
80
    {
81
        return str_starts_with($class, 'Hyde') ? class_basename($class) : $class;
82
    }
83
84
    protected function getSourcePath(string $path): string
85
    {
86
        return $this->isPageDiscoverable() ? $path : 'none';
87
    }
88
89
    protected function getOutputPath(string $path): string
90
    {
91
        return Hyde::getOutputDirectory()."/$path";
92
    }
93
94
    protected function href(string $link, string $label): string
95
    {
96
        return "<href=$link>$label</>";
97
    }
98
99
    protected function isPageDiscoverable(): bool
100
    {
101
        return filled($this->route->getSourcePath()) && ! $this->route->getPage() instanceof InMemoryPage;
102
    }
103
}
104