Passed
Branch code-quality (01d554)
by Caen
02:52
created

getNavigationMenuPriority()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
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
 * @see \Hyde\Framework\Testing\Feature\AbstractPageTest
12
 */
13
class FindsNavigationDataForPage
14
{
15
    #[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
16
    public static function run(AbstractPage $page): array
17
    {
18
        return (new static($page))->getData();
19
    }
20
21
    protected function __construct(protected AbstractPage $page)
22
    {
23
    }
24
25
    #[ArrayShape(['title' => 'string', 'hidden' => 'bool', 'priority' => 'int'])]
26
    protected function getData(): array
27
    {
28
        return [
29
            'title' => $this->getNavigationMenuTitle(),
30
            'hidden' => ! $this->getNavigationMenuVisible(),
31
            'priority' => $this->getNavigationMenuPriority(),
32
        ];
33
    }
34
35
    protected function getNavigationMenuTitle(): string
36
    {
37
        if ($this->page->matter('navigation.title') !== null) {
38
            return $this->page->matter('navigation.title');
39
        }
40
41
        if ($this->page->identifier === 'index') {
42
            if ($this->page instanceof DocumentationPage) {
43
                return config('hyde.navigation.labels.docs', 'Docs');
44
            }
45
46
            return config('hyde.navigation.labels.home', 'Home');
47
        }
48
49
        if ($this->page->matter('title') !== null) {
50
            return $this->page->matter('title');
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($this->page->routeKey, config('hyde.navigation.exclude', []));
64
        }
65
66
        if ($this->page->matter('navigation.hidden', false)) {
67
            return false;
68
        }
69
70
        if (in_array($this->page->identifier, config('hyde.navigation.exclude', ['404']))) {
71
            return false;
72
        }
73
74
        return true;
75
    }
76
77
    protected function getNavigationMenuPriority(): int
78
    {
79
        if ($this->page->matter('navigation.priority') !== null) {
80
            return $this->page->matter('navigation.priority');
81
        }
82
83
        if (array_key_exists($this->page->routeKey, config('hyde.navigation.order', []))) {
84
            return (int) config('hyde.navigation.order.'.$this->page->routeKey);
85
        }
86
87
        return 999;
88
    }
89
}
90