Completed
Push — master ( 37142f...a7fd39 )
by ARCANEDEV
03:23
created

BreadcrumbsTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.77%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 126
wmc 11
lcom 1
cbo 2
ccs 30
cts 31
cp 0.9677
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setBreadcrumbsContainer() 0 6 1
A getBreadcrumbsHomeItem() 0 10 1
A registerBreadcrumbs() 0 12 3
A loadBreadcrumbs() 0 14 4
A addBreadcrumb() 0 6 1
A addBreadcrumbRoute() 0 4 1
1
<?php namespace Arcanedev\Breadcrumbs\Traits;
2
3
use Arcanedev\Breadcrumbs\Builder;
4
5
/**
6
 * Trait     BreadcrumbsTrait
7
 *
8
 * @package  Arcanedev\Breadcrumbs\Traits
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
trait BreadcrumbsTrait
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Breadcrumbs container name.
19
     *
20
     * @var string
21
     */
22
    protected $breadcrumbsContainer   = 'public';
23
24
    /**
25
     * Breadcrumbs items collection.
26
     *
27
     * @var array
28
     */
29
    private $breadcrumbsItems       = [];
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Getters & Setters
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 4
        $route = config('breadcrumbs.home-route', 'public::home');
57
58
        return [
59 4
            'title' => 'Home',
60 4
            'url'   => route($route),
61 3
            'data'  => [],
62 3
        ];
63
    }
64
65
    /* ------------------------------------------------------------------------------------------------
66
     |  Main Functions
67
     | ------------------------------------------------------------------------------------------------
68
     */
69
    /**
70
     * Register a breadcrumb.
71
     *
72
     * @param  string  $container
73
     * @param  array   $item
74
     */
75 4
    protected function registerBreadcrumbs($container, array $item = [])
76
    {
77 4
        $this->setBreadcrumbsContainer($container);
78
79
        breadcrumbs()->register('main', function(Builder $bc) use ($item) {
80 4
            if (empty($item)) {
81 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...
82 3
            }
83
84 4
            $bc->push($item['title'], $item['url'], isset($item['array']) ? $item['array'] : []);
85 4
        });
86 4
    }
87
88
    /**
89
     * Load all breadcrumbs.
90
     */
91
    protected function loadBreadcrumbs()
92
    {
93 4
        breadcrumbs()->register($this->breadcrumbsContainer, function(Builder $bc) {
94 4
            $bc->parent('main');
95
96 4
            if (empty($this->breadcrumbsItems)) {
97
                return;
98
            }
99
100 4
            foreach ($this->breadcrumbsItems as $crumb) {
101 4
                $bc->push($crumb['title'], $crumb['url'], isset($crumb['array']) ? $crumb['array'] : []);
102 3
            }
103 4
        });
104 4
    }
105
106
    /**
107
     * Add breadcrumb.
108
     *
109
     * @param  string  $title
110
     * @param  string  $url
111
     * @param  array   $data
112
     *
113
     * @return self
114
     */
115 4
    protected function addBreadcrumb($title, $url = '', array $data = [])
116
    {
117 4
        $this->breadcrumbsItems[] = compact('title', 'url', 'data');
118
119 4
        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 self
131
     */
132 4
    protected function addBreadcrumbRoute($title, $route, array $parameters = [], array $data = [])
133
    {
134 4
        return $this->addBreadcrumb($title, route($route, $parameters), $data);
135
    }
136
}
137