Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

getNavigationMenuVisible()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 21
rs 8.8333
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 instanceof AbstractMarkdownPage) {
36
            if ($this->page->matter('navigation.title') !== null) {
37
                return $this->page->matter('navigation.title');
38
            }
39
40
            if ($this->page->matter('title') !== null) {
41
                return $this->page->matter('title');
42
            }
43
        }
44
45
        if ($this->page->identifier === 'index') {
46
            if ($this->page instanceof DocumentationPage) {
47
                return config('hyde.navigation.labels.docs', 'Docs');
48
            }
49
50
            return config('hyde.navigation.labels.home', 'Home');
51
        }
52
53
        return $this->page->title;
54
    }
55
56
    protected function getNavigationMenuVisible(): bool
57
    {
58
        if ($this->page instanceof MarkdownPost) {
59
            return false;
60
        }
61
62
        if ($this->page instanceof DocumentationPage) {
63
            return $this->page->identifier === 'index' && ! in_array('docs', config('hyde.navigation.exclude', []));
64
        }
65
66
        if ($this->page instanceof AbstractMarkdownPage) {
67
            if ($this->page->matter('navigation.hidden', false)) {
68
                return false;
69
            }
70
        }
71
72
        if (in_array($this->page->identifier, config('hyde.navigation.exclude', ['404']))) {
73
            return false;
74
        }
75
76
        return true;
77
    }
78
79
    protected function getNavigationMenuPriority(): int
80
    {
81
        if ($this->page instanceof AbstractMarkdownPage) {
82
            if ($this->page->matter('navigation.priority') !== null) {
83
                return $this->page->matter('navigation.priority');
84
            }
85
        }
86
87
        if ($this->page instanceof DocumentationPage) {
88
            return (int) config('hyde.navigation.order.docs', 100);
89
        }
90
91
        if ($this->page->identifier === 'index') {
92
            return (int) config('hyde.navigation.order.index', 0);
93
        }
94
95
        if ($this->page->identifier === 'posts') {
96
            return (int) config('hyde.navigation.order.posts', 10);
97
        }
98
99
        if (array_key_exists($this->page->identifier, config('hyde.navigation.order', []))) {
100
            return (int) config('hyde.navigation.order.'.$this->page->identifier);
101
        }
102
103
        return 999;
104
    }
105
}
106