Passed
Push — master ( c97bdb...f3d35e )
by Caen
12:35 queued 13s
created

DocumentationSidebar::getPriorityForRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Models\Pages\DocumentationPage;
6
use Hyde\Framework\Services\RoutingService;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
10
class DocumentationSidebar extends NavigationMenu
11
{
12
    public function generate(): self
13
    {
14
        RoutingService::getInstance()->getRoutesForModel(DocumentationPage::class)->each(function (Route $route) {
15
            $this->items->push(NavItem::fromRoute($route)->setPriority($this->getPriorityForRoute($route)));
16
        });
17
18
        return $this;
19
    }
20
21
    public function hasGroups(): bool
22
    {
23
        return $this->items->map(function (NavItem $item) {
24
            return $item->getGroup() !== null;
25
        })->contains(true);
26
    }
27
28
    public function getGroups(): array
29
    {
30
        return $this->items->map(function (NavItem $item) {
31
            return $item->getGroup();
32
        })->unique()->toArray();
33
    }
34
35
    public function getItemsInGroup(?string $group): Collection
36
    {
37
        return $this->items->filter(function ($item) use ($group) {
38
            return $item->getGroup() === $group || $item->getGroup() === Str::slug($group);
39
        })->sortBy('priority')->values();
40
    }
41
42
    protected function filterHiddenItems(): Collection
43
    {
44
        return $this->items->reject(function (NavItem $item) {
45
            return $item->route->getSourceModel()->matter('hidden', false) || ($item->route->getRouteKey() === 'docs/index');
0 ignored issues
show
Bug introduced by
The method matter() does not exist on Hyde\Framework\Contracts\PageContract. It seems like you code against a sub-type of Hyde\Framework\Contracts\PageContract such as Hyde\Framework\Contracts\AbstractMarkdownPage. ( Ignorable by Annotation )

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

45
            return $item->route->getSourceModel()->/** @scrutinizer ignore-call */ matter('hidden', false) || ($item->route->getRouteKey() === 'docs/index');
Loading history...
46
        })->values();
47
    }
48
49
    protected function getPriorityForRoute(Route $route): int
50
    {
51
        return $route->getSourceModel()->matter('priority') ?? $this->findPriorityInConfig($route->getSourceModel()->slug);
0 ignored issues
show
Bug introduced by
Accessing slug on the interface Hyde\Framework\Contracts\PageContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
    }
53
54
    protected function findPriorityInConfig(string $slug): int
55
    {
56
        $orderIndexArray = config('docs.sidebar_order', []);
57
58
        if (! in_array($slug, $orderIndexArray)) {
59
            return 500;
60
        }
61
62
        return array_search($slug, $orderIndexArray) + 250;
63
64
        // Adding 250 makes so that pages with a front matter priority that is lower
65
        // can be shown first. It's lower than the fallback of 500 so that they
66
        // still come first. This is all to make it easier to mix priorities.
67
    }
68
}
69