Passed
Push — master ( 75f29e...00dcc0 )
by Caen
03:06 queued 13s
created

findNavigationMenuPriorityInNavigationConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns\Internal;
4
5
use Hyde\Framework\Models\Pages\DocumentationPage;
6
use Hyde\Framework\Models\Pages\MarkdownPost;
7
8
/**
9
 * @internal Trait for HydePages to manage data used for navigation menus and the documentation sidebar.
10
 */
11
trait HasNavigationData
12
{
13
    public function showInNavigation(): bool
14
    {
15
        return ! $this->navigation['hidden'];
16
    }
17
18
    public function navigationMenuPriority(): int
19
    {
20
        return $this->navigation['priority'];
21
    }
22
23
    public function navigationMenuLabel(): string
24
    {
25
        return $this->navigation['label'];
26
    }
27
28
    protected function constructNavigationData(): void
29
    {
30
        $this->setNavigationData(
31
            $this->findNavigationMenuLabel(),
32
            $this->findNavigationMenuHidden(),
33
            $this->findNavigationMenuPriority(),
34
        );
35
    }
36
37
    protected function constructSidebarNavigationData(): void
38
    {
39
        $this->setNavigationData(
40
            $this->findNavigationMenuLabel(),
41
            $this->findNavigationMenuHidden(),
42
            $this->matter('navigation.priority', $this->findNavigationMenuPriority())
0 ignored issues
show
Bug introduced by
It seems like matter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
            $this->/** @scrutinizer ignore-call */ 
43
                   matter('navigation.priority', $this->findNavigationMenuPriority())
Loading history...
43
        );
44
    }
45
46
    protected function setNavigationData(string $label, bool $hidden, int $priority): void
47
    {
48
        $this->navigation = [
0 ignored issues
show
Bug Best Practice introduced by
The property navigation does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
            'label' => $label,
50
            'hidden' => $hidden,
51
            'priority' => $priority,
52
        ];
53
    }
54
55
    private function findNavigationMenuLabel(): string
56
    {
57
        if ($this->matter('navigation.label') !== null) {
58
            return $this->matter('navigation.label');
59
        }
60
61
        if (isset($this->getNavigationLabelConfig()[$this->routeKey])) {
62
            return $this->getNavigationLabelConfig()[$this->routeKey];
63
        }
64
65
        return $this->matter('title') ?? $this->title;
66
    }
67
68
    private function findNavigationMenuHidden(): bool
69
    {
70
        if ($this instanceof MarkdownPost) {
71
            return true;
72
        }
73
74
        if ($this->matter('navigation.hidden', false)) {
75
            return true;
76
        }
77
78
        if (in_array($this->routeKey, config('hyde.navigation.exclude', ['404']))) {
79
            return true;
80
        }
81
82
        return false;
83
    }
84
85
    private function findNavigationMenuPriority(): int
86
    {
87
        if ($this->matter('navigation.priority') !== null) {
88
            return $this->matter('navigation.priority');
89
        }
90
91
        // Different default return values are to preserve backwards compatibility
92
        return $this instanceof DocumentationPage
93
            ? $this->findNavigationMenuPriorityInSidebarConfig(array_flip(config('docs.sidebar_order', []))) ?? 500
94
            : $this->findNavigationMenuPriorityInNavigationConfig(config('hyde.navigation.order', [])) ?? 999;
95
    }
96
97
    private function findNavigationMenuPriorityInNavigationConfig(array $config): ?int
98
    {
99
        return array_key_exists($this->routeKey, $config) ? (int) $config[$this->routeKey] : null;
100
    }
101
102
    private function findNavigationMenuPriorityInSidebarConfig(array $config): ?int
103
    {
104
        // Sidebars uses a special syntax where the keys are just the page identifiers in a flat array
105
        return isset($config[$this->identifier]) ? $config[$this->identifier] + 250 : null;
106
        // Adding 250 makes so that pages with a front matter priority that is lower
107
        // can be shown first. It's lower than the fallback of 500 so that they
108
        // still come first. This is all to make it easier to mix priorities.
109
    }
110
111
    private function getNavigationLabelConfig(): array
112
    {
113
        return array_merge([
114
            'index' => 'Home',
115
            'docs/index' => 'Docs',
116
        ], config('hyde.navigation.labels', []));
117
    }
118
}
119