HasBreadcrumbs::setBreadcrumbsContainer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Breadcrumbs;
6
7
/**
8
 * Trait     HasBreadcrumbs
9
 *
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait HasBreadcrumbs
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Breadcrumbs container name.
21
     *
22
     * @var string
23
     */
24
    protected $breadcrumbsContainer   = 'public';
25
26
    /**
27
     * Breadcrumbs items collection.
28
     *
29
     * @var array
30
     */
31
    private $breadcrumbsItems       = [];
32
33
    /* -----------------------------------------------------------------
34
     |  Getters & Setters
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Set breadcrumbs container name.
40
     *
41
     * @param  string  $name
42
     *
43
     * @return $this
44
     */
45 6
    protected function setBreadcrumbsContainer(string $name)
46
    {
47 6
        $this->breadcrumbsContainer = $name;
48
49 6
        return $this;
50
    }
51
52
    /**
53
     * Get the breadcrumbs home item (root).
54
     *
55
     * @return array
56
     */
57 6
    protected function getBreadcrumbsHomeItem(): array
58
    {
59
        return [
60 6
            'title' => trans('breadcrumbs::items.home'),
61 6
            'url'   => route(config('breadcrumbs.home-route', 'public::home')),
62
            'data'  => [],
63
        ];
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Functions
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Register a breadcrumb.
73
     *
74
     * @param  string  $container
75
     * @param  array   $item
76
     */
77 6
    protected function registerBreadcrumbs(string $container, array $item = []): void
78
    {
79 6
        $this->setBreadcrumbsContainer($container);
80
81 6
        breadcrumbs()->register('main', function(Builder $bc) use ($item) {
82 6
            if (empty($item)) {
83 6
                $item = $this->getBreadcrumbsHomeItem();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $item, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
84
            }
85
86 6
            $bc->push($item['title'], $item['url'], $item['data'] ?? []);
87 6
        });
88 6
    }
89
90
    /**
91
     * Load all breadcrumbs.
92
     */
93 6
    protected function loadBreadcrumbs(): void
94
    {
95 6
        breadcrumbs()->register($this->breadcrumbsContainer, function(Builder $bc) {
96 6
            $bc->parent('main');
97
98 6
            if ( ! empty($this->breadcrumbsItems)) {
99 6
                foreach ($this->breadcrumbsItems as $crumb) {
100 6
                    $bc->push($crumb['title'], $crumb['url'], $crumb['data'] ?? []);
101
                }
102
            }
103 6
        });
104 6
    }
105
106
    /**
107
     * Add breadcrumb.
108
     *
109
     * @param  string  $title
110
     * @param  string  $url
111
     * @param  array   $data
112
     *
113
     * @return $this
114
     */
115 6
    protected function addBreadcrumb(string $title, string $url = '', array $data = [])
116
    {
117 6
        $this->breadcrumbsItems[] = compact('title', 'url', 'data');
118
119 6
        return $this;
120
    }
121
122
    /**
123
     * Add breadcrumb with route.
124
     *
125
     * @param  string  $title
126
     * @param  string  $route
127
     * @param  array   $parameters
128
     * @param  array   $data
129
     *
130
     * @return $this
131
     */
132 6
    protected function addBreadcrumbRoute(string $title, string $route, array $parameters = [], array $data = [])
133
    {
134 6
        return $this->addBreadcrumb($title, route($route, $parameters), $data);
135
    }
136
}
137