Passed
Push — master ( 4b62eb...7c9315 )
by Caen
03:23 queued 13s
created

BreadcrumbsComponent::makeBreadcrumbs()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 22
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 3
nop 0
dl 0
loc 25
rs 9.5222
c 22
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Views\Components;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Route;
9
use Illuminate\Contracts\View\Factory;
10
use Illuminate\Contracts\View\View;
11
use Illuminate\View\Component;
12
13
class BreadcrumbsComponent extends Component
14
{
15
    public array $breadcrumbs;
16
17
    public function __construct()
18
    {
19
        $this->breadcrumbs = $this->makeBreadcrumbs();
20
    }
21
22
    /** @interitDoc */
23
    public function render(): Factory|View
24
    {
25
        return view('hyde::components.breadcrumbs');
26
    }
27
28
    protected function makeBreadcrumbs(): array
29
    {
30
        $identifier = Hyde::currentRoute()->getPage()->getIdentifier();
31
        $breadcrumbs = [(Route::get('index')?->getLink() ?? '/') => 'Home'];
32
33
        if ($identifier === 'index') {
34
            return $breadcrumbs;
35
        }
36
37
        $previous = '';
38
        $fields = explode('/', $identifier);
39
        foreach ($fields as $index => $basename) {
40
            if ($basename === 'index') {
41
                break;
42
            }
43
44
            // if it's not the last basename, add index.html (since it must be a directory) otherwise add .html
45
            $path = $previous.$basename.($index < count($fields) - 1 ? '/index.html' : '.html');
46
47
            $breadcrumbs[Hyde::relativeLink($path)] = Hyde::makeTitle($basename);
48
49
            $previous .= $basename.'/';
50
        }
51
52
        return $breadcrumbs;
53
    }
54
}
55