Completed
Push — master ( 5e1e13...f15dc3 )
by Nicolas
03:21
created

Menu   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 42.42%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 166
ccs 14
cts 33
cp 0.4242
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 4 1
A create() 0 10 1
A has() 0 4 1
A instance() 0 4 2
A get() 0 5 2
A render() 0 4 1
A style() 0 4 1
A all() 0 4 1
A count() 0 4 1
A destroy() 0 4 1
A modify() 0 8 1
1
<?php
2
3
namespace Nwidart\Menus;
4
5
use Closure;
6
use Countable;
7
use Illuminate\Config\Repository;
8
use Illuminate\View\Factory;
9
10
class Menu implements Countable
11
{
12
    /**
13
     * The menus collections.
14
     *
15
     * @var array
16
     */
17
    protected $menus = array();
18
    /**
19
     * @var Repository
20
     */
21
    private $config;
22
    /**
23
     * @var Factory
24
     */
25
    private $views;
26
27
    /**
28
     * The constructor.
29
     *
30
     * @param Factory    $views
31
     * @param Repository $config
32
     */
33 1
    public function __construct(Factory $views, Repository $config)
34
    {
35 1
        $this->views = $views;
36 1
        $this->config = $config;
37 1
    }
38
39
    /**
40
     * Make new menu.
41
     *
42
     * @param string $name
43
     * @param Closure $callback
44
     *
45
     * @return \Nwidart\Menus\MenuBuilder
46
     */
47
    public function make($name, \Closure $callback)
48
    {
49
        return $this->create($name, $callback);
50
    }
51
52
    /**
53
     * Create new menu.
54
     *
55
     * @param string   $name
56
     * @param Callable $resolver
57
     *
58
     * @return \Nwidart\Menus\MenuBuilder
59
     */
60 1
    public function create($name, Closure $resolver)
61
    {
62 1
        $builder = new MenuBuilder($name, $this->config);
63
64 1
        $builder->setViewFactory($this->views);
65
66 1
        $this->menus[$name] = $builder;
67
68 1
        return $resolver($builder);
69
    }
70
71
    /**
72
     * Check if the menu exists.
73
     *
74
     * @param string $name
75
     *
76
     * @return bool
77
     */
78 1
    public function has($name)
79
    {
80 1
        return array_key_exists($name, $this->menus);
81
    }
82
83
    /**
84
     * Get instance of the given menu if exists.
85
     *
86
     * @param string $name
87
     *
88
     * @return string|null
89
     */
90
    public function instance($name)
91
    {
92
        return $this->has($name) ? $this->menus[$name] : null;
93
    }
94
95
    /**
96
     * Modify a specific menu.
97
     *
98
     * @param  string   $name
99
     * @param  Closure  $callback
100
     * @return void
101
     */
102
    public function modify($name, Closure $callback)
103
    {
104
        $menu = collect($this->menus)->filter(function ($menu) use ($name) {
105
            return $menu->getName() == $name;
106
        })->first();
107
108
        $callback($menu);
109
    }
110
111
    /**
112
     * Render the menu tag by given name.
113
     *
114
     * @param string $name
115
     * @param string $presenter
116
     *
117
     * @return string|null
118
     */
119 1
    public function get($name, $presenter = null, $bindings = array())
120
    {
121 1
        return $this->has($name) ?
122 1
            $this->menus[$name]->setBindings($bindings)->render($presenter) : null;
123
    }
124
125
    /**
126
     * Render the menu tag by given name.
127
     *
128
     * @param $name
129
     * @param null $presenter
130
     *
131
     * @return string
132
     */
133
    public function render($name, $presenter = null, $bindings = array())
134
    {
135
        return $this->get($name, $presenter, $bindings);
136
    }
137
138
    /**
139
     * Get a stylesheet for enable multilevel menu.
140
     *
141
     * @return mixed
142
     */
143
    public function style()
144
    {
145
        return $this->views->make('menus::style')->render();
146
    }
147
148
    /**
149
     * Get all menus.
150
     *
151
     * @return array
152
     */
153
    public function all()
154
    {
155
        return $this->menus;
156
    }
157
158
    /**
159
     * Get count from all menus.
160
     *
161
     * @return int
162
     */
163
    public function count()
164
    {
165
        return count($this->menus);
166
    }
167
168
    /**
169
     * Empty the current menus.
170
     */
171
    public function destroy()
172
    {
173
        $this->menus = array();
174
    }
175
}
176