Completed
Pull Request — master (#4)
by ARCANEDEV
03:30
created

BreadcrumbsTrait::loadBreadcrumbs()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0219

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.2
cc 4
eloc 7
nc 1
nop 0
crap 4.0219
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($crumb['array']) ? $crumb['array'] : []);
0 ignored issues
show
Bug introduced by
The variable $crumb seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
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