Passed
Push — master ( 558174...f2f8c0 )
by Caen
02:58 queued 12s
created

DocumentationSidebar::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Hyde\Framework\Models\Navigation;
4
5
use Hyde\Framework\Hyde;
6
use Hyde\Framework\Models\Pages\DocumentationPage;
7
use Hyde\Framework\Models\Route;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Str;
10
11
/**
12
 * @see \Hyde\Framework\Testing\Feature\Services\DocumentationSidebarTest
13
 */
14
class DocumentationSidebar extends NavigationMenu
15
{
16
    /** @return $this */
17
    public function generate(): static
18
    {
19
        Hyde::routes()->getRoutes(DocumentationPage::class)->each(function (Route $route) {
20
            if (! $route->getSourceModel()->get('hidden', false)) {
21
                $this->items->push(tap(NavItem::fromRoute($route)->setPriority($this->getPriorityForRoute($route)), function (NavItem $item) {
22
                    $item->title = $item->route->getSourceModel()->get('label');
23
                }));
24
            }
25
        });
26
27
        return $this;
28
    }
29
30
    public function hasGroups(): bool
31
    {
32
        return count($this->getGroups()) >= 1 && $this->getGroups() !== [0 => 'other'];
33
    }
34
35
    public function getGroups(): array
36
    {
37
        return $this->items->map(function (NavItem $item) {
38
            return $item->getGroup();
39
        })->unique()->toArray();
40
    }
41
42
    public function getItemsInGroup(?string $group): Collection
43
    {
44
        return $this->items->filter(function ($item) use ($group) {
45
            return $item->getGroup() === $group || $item->getGroup() === Str::slug($group);
46
        })->sortBy('priority')->values();
47
    }
48
49
    protected function filterHiddenItems(): Collection
50
    {
51
        return $this->items;
52
    }
53
54
    protected function getPriorityForRoute(Route $route): int
55
    {
56
        return $route->getSourceModel()->get('priority');
57
    }
58
}
59