Passed
Push — master ( 2fb99f...a26130 )
by Caen
03:18 queued 12s
created

getNavigationMenuVisible()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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