Passed
Push — master ( 6d6565...6a1810 )
by Caen
03:18 queued 12s
created

GeneratesNavigationData   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 104
rs 10
c 3
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A constructNavigationData() 0 6 1
A constructSidebarNavigationData() 0 7 1
A findNavigationMenuPriorityInNavigationConfig() 0 3 2
A findNavigationMenuPriorityInSidebarConfig() 0 4 2
A findNavigationMenuHidden() 0 15 4
A getDocumentationPageGroup() 0 9 2
A findNavigationMenuPriority() 0 10 3
A findNavigationMenuLabel() 0 11 3
A getNavigationLabelConfig() 0 6 1
A setNavigationData() 0 7 1
1
<?php
2
3
namespace Hyde\Framework\Concerns\Internal;
4
5
use Hyde\Framework\Models\NavigationData;
6
use Hyde\Framework\Models\Pages\DocumentationPage;
7
use Hyde\Framework\Models\Pages\MarkdownPost;
8
use Illuminate\Support\Str;
9
10
/**
11
 * @internal Trait for HydePages to manage data used for navigation menus and the documentation sidebar.
12
 *
13
 * @see \Hyde\Framework\Concerns\HydePage
14
 */
15
trait GeneratesNavigationData
16
{
17
    protected function constructNavigationData(): void
18
    {
19
        $this->setNavigationData(
20
            $this->findNavigationMenuLabel(),
21
            $this->findNavigationMenuHidden(),
22
            $this->findNavigationMenuPriority(),
23
        );
24
    }
25
26
    protected function constructSidebarNavigationData(): void
27
    {
28
        $this->setNavigationData(
29
            $this->findNavigationMenuLabel(),
30
            $this->findNavigationMenuHidden(),
31
            $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

31
            $this->/** @scrutinizer ignore-call */ 
32
                   matter('navigation.priority', $this->findNavigationMenuPriority()),
Loading history...
32
            $this->getDocumentationPageGroup()
33
        );
34
    }
35
36
    protected function setNavigationData(string $label, bool $hidden, int $priority, ?string $group = null): void
37
    {
38
        $this->navigation = NavigationData::make([
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...
39
            'label' => $label,
40
            'group' => $group,
41
            'hidden' => $hidden,
42
            'priority' => $priority,
43
        ]);
44
    }
45
46
    private function findNavigationMenuLabel(): string
47
    {
48
        if ($this->matter('navigation.label') !== null) {
49
            return $this->matter('navigation.label');
50
        }
51
52
        if (isset($this->getNavigationLabelConfig()[$this->routeKey])) {
53
            return $this->getNavigationLabelConfig()[$this->routeKey];
54
        }
55
56
        return $this->matter('title') ?? $this->title;
57
    }
58
59
    private function findNavigationMenuHidden(): bool
60
    {
61
        if ($this instanceof MarkdownPost) {
62
            return true;
63
        }
64
65
        if ($this->matter('navigation.hidden', false)) {
66
            return true;
67
        }
68
69
        if (in_array($this->routeKey, config('hyde.navigation.exclude', ['404']))) {
70
            return true;
71
        }
72
73
        return false;
74
    }
75
76
    private function findNavigationMenuPriority(): int
77
    {
78
        if ($this->matter('navigation.priority') !== null) {
79
            return $this->matter('navigation.priority');
80
        }
81
82
        // Different default return values are to preserve backwards compatibility
83
        return $this instanceof DocumentationPage
84
            ? $this->findNavigationMenuPriorityInSidebarConfig(array_flip(config('docs.sidebar_order', []))) ?? 500
85
            : $this->findNavigationMenuPriorityInNavigationConfig(config('hyde.navigation.order', [])) ?? 999;
86
    }
87
88
    private function findNavigationMenuPriorityInNavigationConfig(array $config): ?int
89
    {
90
        return array_key_exists($this->routeKey, $config) ? (int) $config[$this->routeKey] : null;
91
    }
92
93
    private function findNavigationMenuPriorityInSidebarConfig(array $config): ?int
94
    {
95
        // Sidebars uses a special syntax where the keys are just the page identifiers in a flat array
96
        return isset($config[$this->identifier]) ? $config[$this->identifier] + 250 : null;
97
        // Adding 250 makes so that pages with a front matter priority that is lower
98
        // can be shown first. It's lower than the fallback of 500 so that they
99
        // still come first. This is all to make it easier to mix priorities.
100
    }
101
102
    private function getNavigationLabelConfig(): array
103
    {
104
        return array_merge([
105
            'index' => 'Home',
106
            'docs/index' => 'Docs',
107
        ], config('hyde.navigation.labels', []));
108
    }
109
110
    private function getDocumentationPageGroup(): ?string
111
    {
112
        // If the documentation page is in a subdirectory,
113
        // then we can use that as the category name.
114
        // Otherwise, we look in the front matter.
115
116
        return str_contains($this->identifier, '/')
117
            ? Str::before($this->identifier, '/')
118
            : $this->matter('navigation.group', 'other');
119
    }
120
}
121