Completed
Push — master ( a7fd39...cbf15b )
by ARCANEDEV
06:30
created

BreadcrumbsTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 11
c 9
b 0
f 0
lcom 1
cbo 2
dl 0
loc 124
ccs 31
cts 31
cp 1
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 12 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 12
    protected function setBreadcrumbsContainer($name)
43
    {
44 12
        $this->breadcrumbsContainer = $name;
45
46 12
        return $this;
47
    }
48
49
    /**
50
     * Get the breadcrumbs home item (root).
51
     *
52
     * @return array
53
     */
54 12
    protected function getBreadcrumbsHomeItem()
55
    {
56 12
        $route = config('breadcrumbs.home-route', 'public::home');
57
58
        return [
59 12
            'title' => 'Home',
60 12
            'url'   => route($route),
61 9
            'data'  => [],
62 9
        ];
63
    }
64
65
    /* ------------------------------------------------------------------------------------------------
66
     |  Main Functions
67
     | ------------------------------------------------------------------------------------------------
68
     */
69
    /**
70
     * Register a breadcrumb.
71
     *
72
     * @param  string  $container
73
     * @param  array   $item
74
     */
75 12
    protected function registerBreadcrumbs($container, array $item = [])
76
    {
77 12
        $this->setBreadcrumbsContainer($container);
78
79
        breadcrumbs()->register('main', function(Builder $bc) use ($item) {
80 12
            if (empty($item)) {
81 12
                $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 9
            }
83
84 12
            $bc->push($item['title'], $item['url'], isset($item['array']) ? $item['array'] : []);
85 12
        });
86 12
    }
87
88
    /**
89
     * Load all breadcrumbs.
90
     */
91
    protected function loadBreadcrumbs()
92
    {
93 12
        breadcrumbs()->register($this->breadcrumbsContainer, function(Builder $bc) {
94 12
            $bc->parent('main');
95
96 8
            if ( ! empty($this->breadcrumbsItems)) {
97 8
                foreach ($this->breadcrumbsItems as $crumb) {
98 8
                    $bc->push($crumb['title'], $crumb['url'], isset($crumb['array']) ? $crumb['array'] : []);
99 6
                }
100 6
            }
101 12
        });
102 12
    }
103
104
    /**
105
     * Add breadcrumb.
106
     *
107
     * @param  string  $title
108
     * @param  string  $url
109
     * @param  array   $data
110
     *
111
     * @return self
112
     */
113 12
    protected function addBreadcrumb($title, $url = '', array $data = [])
114
    {
115 12
        $this->breadcrumbsItems[] = compact('title', 'url', 'data');
116
117 12
        return $this;
118
    }
119
120
    /**
121
     * Add breadcrumb with route.
122
     *
123
     * @param  string  $title
124
     * @param  string  $route
125
     * @param  array   $parameters
126
     * @param  array   $data
127
     *
128
     * @return self
129
     */
130 12
    protected function addBreadcrumbRoute($title, $route, array $parameters = [], array $data = [])
131
    {
132 12
        return $this->addBreadcrumb($title, route($route, $parameters), $data);
133
    }
134
}
135