Completed
Push — master ( 2d084d...ce6a8c )
by ARCANEDEV
11s
created

HasBreadcrumbs::addBreadcrumbRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\Breadcrumbs;
2
3
/**
4
 * Trait     HasBreadcrumbs
5
 *
6
 * @package  Arcanedev\Breadcrumbs
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
trait HasBreadcrumbs
10
{
11
    /* -----------------------------------------------------------------
12
     |  Properties
13
     | -----------------------------------------------------------------
14
     */
15
16
    /**
17
     * Breadcrumbs container name.
18
     *
19
     * @var string
20
     */
21
    protected $breadcrumbsContainer   = 'public';
22
23
    /**
24
     * Breadcrumbs items collection.
25
     *
26
     * @var array
27
     */
28
    private $breadcrumbsItems       = [];
29
30
    /* -----------------------------------------------------------------
31
     |  Getters & Setters
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Set breadcrumbs container name.
37
     *
38
     * @param  string  $name
39
     *
40
     * @return self
41
     */
42 4
    protected function setBreadcrumbsContainer($name)
43
    {
44 4
        $this->breadcrumbsContainer = $name;
45
46 4
        return $this;
47
    }
48
49
    /**
50
     * Get the breadcrumbs home item (root).
51
     *
52
     * @return array
53
     */
54 4
    protected function getBreadcrumbsHomeItem()
55
    {
56
        return [
57 4
            'title' => trans('breadcrumbs::items.home'),
58 4
            'url'   => route(config('breadcrumbs.home-route', 'public::home')),
59
            'data'  => [],
60
        ];
61
    }
62
63
    /* -----------------------------------------------------------------
64
     |  Main Functions
65
     | -----------------------------------------------------------------
66
     */
67
68
    /**
69
     * Register a breadcrumb.
70
     *
71
     * @param  string  $container
72
     * @param  array   $item
73
     */
74 4
    protected function registerBreadcrumbs($container, array $item = [])
75
    {
76 4
        $this->setBreadcrumbsContainer($container);
77
78
        breadcrumbs()->register('main', function(Builder $bc) use ($item) {
79 4
            if (empty($item)) {
80 4
                $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...
81
            }
82
83 4
            $bc->push($item['title'], $item['url'], $item['data'] ?? []);
84 4
        });
85 4
    }
86
87
    /**
88
     * Load all breadcrumbs.
89
     */
90 4
    protected function loadBreadcrumbs()
91
    {
92
        breadcrumbs()->register($this->breadcrumbsContainer, function(Builder $bc) {
93 4
            $bc->parent('main');
94
95 4
            if ( ! empty($this->breadcrumbsItems)) {
96 4
                foreach ($this->breadcrumbsItems as $crumb) {
97 4
                    $bc->push($crumb['title'], $crumb['url'], $crumb['data'] ?? []);
98
                }
99
            }
100 4
        });
101 4
    }
102
103
    /**
104
     * Add breadcrumb.
105
     *
106
     * @param  string  $title
107
     * @param  string  $url
108
     * @param  array   $data
109
     *
110
     * @return self
111
     */
112 4
    protected function addBreadcrumb($title, $url = '', array $data = [])
113
    {
114 4
        $this->breadcrumbsItems[] = compact('title', 'url', 'data');
115
116 4
        return $this;
117
    }
118
119
    /**
120
     * Add breadcrumb with route.
121
     *
122
     * @param  string  $title
123
     * @param  string  $route
124
     * @param  array   $parameters
125
     * @param  array   $data
126
     *
127
     * @return self
128
     */
129 4
    protected function addBreadcrumbRoute($title, $route, array $parameters = [], array $data = [])
130
    {
131 4
        return $this->addBreadcrumb($title, route($route, $parameters), $data);
132
    }
133
}
134