Passed
Branch master (ef5451)
by Caen
03:29
created

CanBeInNavigation   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 88
rs 10
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
B showInNavigation() 0 21 7
A navigationMenuTitle() 0 21 6
B navigationMenuPriority() 0 25 7
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Contracts\AbstractMarkdownPage;
6
use Hyde\Framework\Models\Pages\DocumentationPage;
7
use Hyde\Framework\Models\Pages\MarkdownPost;
8
9
/**
10
 * Offloads logic related to navigation menu items for AbstractPage classes.
11
 *
12
 * @see \Hyde\Framework\Testing\Feature\Concerns\CanBeInNavigationTest
13
 */
14
trait CanBeInNavigation
15
{
16
    /**
17
     * Should the item should be displayed in the navigation menu?
18
     *
19
     * @return bool
20
     */
21
    public function showInNavigation(): bool
22
    {
23
        if ($this instanceof MarkdownPost) {
24
            return false;
25
        }
26
27
        if ($this instanceof DocumentationPage) {
28
            return $this->identifier === 'index' && ! in_array('docs', config('hyde.navigation.exclude', []));
29
        }
30
31
        if ($this instanceof AbstractMarkdownPage) {
32
            if ($this->matter('navigation.hidden', false)) {
33
                return false;
34
            }
35
        }
36
37
        if (in_array($this->identifier, config('hyde.navigation.exclude', ['404']))) {
38
            return false;
39
        }
40
41
        return true;
42
    }
43
44
    /**
45
     * The relative priority, determining the position of the item in the menu.
46
     *
47
     * @return int
48
     */
49
    public function navigationMenuPriority(): int
50
    {
51
        if ($this instanceof AbstractMarkdownPage) {
52
            if ($this->matter('navigation.priority') !== null) {
53
                return $this->matter('navigation.priority');
54
            }
55
        }
56
57
        if ($this instanceof DocumentationPage) {
58
            return (int) config('hyde.navigation.order.docs', 100);
59
        }
60
61
        if ($this->identifier === 'index') {
62
            return (int) config('hyde.navigation.order.index', 0);
63
        }
64
65
        if ($this->identifier === 'posts') {
66
            return (int) config('hyde.navigation.order.posts', 10);
67
        }
68
69
        if (array_key_exists($this->identifier, config('hyde.navigation.order', []))) {
70
            return (int) config('hyde.navigation.order.'.$this->identifier);
71
        }
72
73
        return 999;
74
    }
75
76
    /**
77
     * The page title to display in the navigation menu.
78
     *
79
     * @return string
80
     */
81
    public function navigationMenuTitle(): string
82
    {
83
        if ($this instanceof AbstractMarkdownPage) {
84
            if ($this->matter('navigation.title') !== null) {
85
                return $this->matter('navigation.title');
86
            }
87
88
            if ($this->matter('title') !== null) {
89
                return $this->matter('title');
90
            }
91
        }
92
93
        if ($this->identifier === 'index') {
94
            if ($this instanceof DocumentationPage) {
95
                return config('hyde.navigation.labels.docs', 'Docs');
96
            }
97
98
            return config('hyde.navigation.labels.home', 'Home');
99
        }
100
101
        return $this->title;
102
    }
103
104
    /**
105
     * Not yet implemented.
106
     *
107
     * If an item returns a route collection,
108
     * it will automatically be made into a dropdown.
109
     *
110
     * @return \Illuminate\Support\Collection<\Hyde\Framework\Models\Route>
111
     */
112
    // public function navigationMenuChildren(): Collection;
113
}
114