Completed
Push — refactoring ( 84ac1f...ab1ebc )
by Oleg
04:25
created

Builder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.15%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 177
wmc 16
lcom 1
cbo 8
ccs 53
cts 54
cp 0.9815
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A group() 0 22 3
A add() 0 12 2
A has() 0 4 1
A get() 0 4 1
A all() 0 4 1
A forget() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
A render() 0 12 2
A activeAttributes() 0 8 2
1
<?php
2
3
namespace Malezha\Menu\Entity;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Support\Collection;
7
use Illuminate\Contracts\View\Factory as ViewFactory;
8
use Malezha\Menu\Contracts\Builder as BuilderContract;
9
use Malezha\Menu\Traits\HasAttributes;
10
11
class Builder implements BuilderContract
12
{
13
    use HasAttributes;
14
15
    /**
16
     * @var Container
17
     */
18
    protected $container;
19
20
    /**
21
     * @var Collection
22
     */
23
    protected $items;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * @var array
37
     */
38
    protected $activeAttributes;
39
40
    /**
41
     * @param Container $container
42
     * @param string $name
43
     * @param string $type
44
     * @param array $attributes
45
     * @param array $activeAttributes
46
     */
47 14
    function __construct(Container $container, $name, $type = self::UL, $attributes = [], $activeAttributes = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49 14
        $this->container = $container;
50 14
        $this->name = $name;
51 14
        $this->type = $type;
52 14
        $this->attributes = new Attributes($attributes);
53 14
        $this->items = new Collection();
54 14
        $this->activeAttributes = new Attributes($activeAttributes);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Malezha\Menu\Entity...utes($activeAttributes) of type object<Malezha\Menu\Entity\Attributes> is incompatible with the declared type array of property $activeAttributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55 14
    }
56
57
    /**
58
     * @param string $name
59
     * @param callable $itemCallable
60
     * @param callable $menuCallable
61
     * @return Group
62
     */
63 2
    public function group($name, $itemCallable, $menuCallable)
64
    {
65 2
        if (is_callable($itemCallable) && is_callable($menuCallable)) {
66 2
            $item = new Item($this, $name);
67 2
            call_user_func($itemCallable, $item);
68
69 2
            $menu = $this->container->make(BuilderContract::class, [
70 2
                'container' => $this->container, 
71 2
                'name' => $name,
72 2
                'activeAttributes' => $this->activeAttributes()->all(),
73 2
            ]);
74 2
            call_user_func($menuCallable, $menu);
75
76 2
            $group = new Group($menu, $item);
77
78 2
            $this->items->put($name, $group);
79
80 2
            return $group;
81
        }
82
83
        throw new \InvalidArgumentException('Arguments must be callable');
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $title
89
     * @param string $url
90
     * @param array $attributes
91
     * @param array $linkAttributes
92
     * @param callable|null $callback
93
     * @return Item
94
     */
95 6
    public function add($name, $title, $url, $attributes = [], $linkAttributes = [], $callback = null)
96
    {
97 6
        $item = new Item($this, $name, $attributes, $title, $url, $linkAttributes);
98
99 6
        if (is_callable($callback)) {
100 2
            call_user_func($callback, $item);
101 2
        }
102
103 6
        $this->items->put($name, $item);
104
105 6
        return $item;
106
    }
107
108
    /**
109
     * @param string $name
110
     * @return bool
111
     */
112 2
    public function has($name)
113
    {
114 2
        return $this->items->has($name);
115
    }
116
117
    /**
118
     * @param string $name
119
     * @return Item|Group
120
     */
121 2
    public function get($name)
122
    {
123 2
        return $this->items->get($name);
124
    }
125
126
    /**
127
     * @return array
128
     */
129 3
    public function all()
130
    {
131 3
        return $this->items->all();
132
    }
133
134
    /**
135
     * @param string $name
136
     */
137 1
    public function forget($name)
138
    {
139 1
        $this->items->forget($name);
140 1
    }
141
142
    /**
143
     * @return string
144
     */
145 3
    public function getType()
146
    {
147 3
        return $this->type;
148
    }
149
150
    /**
151
     * @param string $type
152
     */
153 1
    public function setType($type)
154
    {
155 1
        $this->type = (string)$type;
156 1
    }
157
158
    /**
159
     * @param string|null $view
160
     * @return string
161
     */
162 2
    public function render($view = null)
163
    {
164 2
        $view = (empty($view)) ? config('menu.view') : $view;
165
        
166
        /* @var ViewFactory $viewFactory */
167 2
        $viewFactory = $this->container->make(ViewFactory::class);
168
169 2
        return $viewFactory->make($view, [
170 2
            'menu' => $this,
171 2
            'renderView' => $view,
172 2
        ])->render();
173
    }
174
175
    /**
176
     * @param callable|null $callback
177
     * @return Attributes|mixed
178
     */
179 5
    public function activeAttributes($callback = null)
180
    {
181 5
        if (is_callable($callback)) {
182 1
            return call_user_func($callback, $this->activeAttributes);
183
        }
184
185 5
        return $this->activeAttributes;
186
    }
187
}