Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

DocumentationSidebar::findPriorityInConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
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
    /** @return $this */
13
    public function generate(): static
14
    {
15
        RoutingService::getInstance()->getRoutesForModel(DocumentationPage::class)->each(function (Route $route) {
16
            if (! $route->getSourceModel()->get('hidden', false)) {
17
                $this->items->push(NavItem::fromRoute($route)->setPriority($this->getPriorityForRoute($route)));
18
            }
19
        });
20
21
        return $this;
22
    }
23
24
    public function hasGroups(): bool
25
    {
26
        return $this->items->map(function (NavItem $item) {
27
            return $item->getGroup() !== null;
28
        })->contains(true);
29
    }
30
31
    public function getGroups(): array
32
    {
33
        return $this->items->map(function (NavItem $item) {
34
            return $item->getGroup();
35
        })->unique()->toArray();
36
    }
37
38
    public function getItemsInGroup(?string $group): Collection
39
    {
40
        return $this->items->filter(function ($item) use ($group) {
41
            return $item->getGroup() === $group || $item->getGroup() === Str::slug($group);
42
        })->sortBy('priority')->values();
43
    }
44
45
    protected function filterHiddenItems(): Collection
46
    {
47
        return $this->items;
48
    }
49
50
    protected function getPriorityForRoute(Route $route): int
51
    {
52
        return $route->getSourceModel()->get('priority');
53
    }
54
}
55