Passed
Push — master ( 28d464...2fb99f )
by Caen
03:05 queued 16s
created

getNavigationMenuPriority()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions\Constructors;
4
5
use Hyde\Framework\Contracts\AbstractMarkdownPage;
6
use Hyde\Framework\Contracts\AbstractPage;
7
use Hyde\Framework\Models\Pages\DocumentationPage;
8
use Hyde\Framework\Models\Pages\MarkdownPost;
9
use JetBrains\PhpStorm\ArrayShape;
10
11
class FindsNavigationDataForPage
12
{
13
    #[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
14
    public static function run(AbstractPage $page): array
15
    {
16
        return (new static($page))->getData();
17
    }
18
19
    protected function __construct(protected AbstractPage $page)
20
    {
21
    }
22
23
    #[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
24
    protected function getData(): array
25
    {
26
        return [
27
            'title' => $this->getNavigationMenuTitle(),
28
            'hidden' => ! $this->getNavigationMenuVisible(),
29
            'priority' => $this->getNavigationMenuPriority(),
30
        ];
31
    }
32
33
    protected function getNavigationMenuTitle(): string
34
    {
35
        if ($this->page->matter('navigation.title') !== null) {
36
            return $this->page->matter('navigation.title');
37
        }
38
39
        if ($this->page->identifier === 'index') {
40
            if ($this->page instanceof DocumentationPage) {
41
                return config('hyde.navigation.labels.docs', 'Docs');
42
            }
43
44
            return config('hyde.navigation.labels.home', 'Home');
45
        }
46
47
        if ($this->page->matter('title') !== null) {
48
            return $this->page->matter('title');
49
        }
50
51
        return $this->page->title;
52
    }
53
54
    protected function getNavigationMenuVisible(): bool
55
    {
56
        if ($this->page instanceof MarkdownPost) {
57
            return false;
58
        }
59
60
        if ($this->page instanceof DocumentationPage) {
61
            return $this->page->identifier === 'index' && ! in_array($this->page->routeKey, config('hyde.navigation.exclude', []));
62
        }
63
64
        if ($this->page instanceof AbstractMarkdownPage) {
65
            if ($this->page->matter('navigation.hidden', false)) {
66
                return false;
67
            }
68
        }
69
70
        if (in_array($this->page->identifier, config('hyde.navigation.exclude', ['404']))) {
71
            return false;
72
        }
73
74
        return true;
75
    }
76
77
    protected function getNavigationMenuPriority(): int
78
    {
79
        if ($this->page instanceof AbstractMarkdownPage) {
80
            if ($this->page->matter('navigation.priority') !== null) {
81
                return $this->page->matter('navigation.priority');
82
            }
83
        }
84
85
        if (array_key_exists($this->page->routeKey, config('hyde.navigation.order', []))) {
86
            return (int) config('hyde.navigation.order.'.$this->page->routeKey);
87
        }
88
89
        return 999;
90
    }
91
}
92