Completed
Push — master ( 2179d5...2fbacb )
by ARCANEDEV
03:46
created

Manager::loadItemsFromConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract;
4
use Arcanesoft\Sidebar\Entities\Item;
5
use Arcanesoft\Sidebar\Entities\ItemCollection;
6
use Illuminate\Support\HtmlString;
7
8
/**
9
 * Class     Manager
10
 *
11
 * @package  Arcanesoft\Sidebar
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Manager implements ManagerContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The view name.
23
     *
24
     * @var string
25
     */
26
    protected $view;
27
28
    /**
29
     * The current name.
30
     *
31
     * @var string
32
     */
33
    protected $currentName;
34
35
    /**
36
     * The sidebar items collection.
37
     *
38
     * @var \Arcanesoft\Sidebar\Entities\ItemCollection
39
     */
40
    protected $items;
41
42
    /**
43
     * The authenticated user.
44
     *
45
     * @var \Arcanesoft\Contracts\Auth\Models\User
46
     */
47
    protected $user;
48
49
    /* -----------------------------------------------------------------
50
     |  Constructor
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Manager constructor.
56
     */
57 21
    public function __construct()
58
    {
59 21
        $this->items = new ItemCollection;
60 21
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Getters & Setters
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Set the view name.
69
     *
70
     * @param  string  $view
71
     *
72
     * @return self
73
     */
74 6
    public function setView($view)
75
    {
76 6
        if ( ! is_null($view))
77 6
            $this->view = $view;
78
79 6
        return $this;
80
    }
81
82
    /**
83
     * Get the current item name.
84
     *
85
     * @return string
86
     */
87 3
    public function getCurrent()
88
    {
89 3
        return $this->currentName;
90
    }
91
92
    /**
93
     * Set the current item name.
94
     *
95
     * @param  string  $currentName
96
     *
97
     * @return $this
98
     */
99 3
    public function setCurrent($currentName)
100
    {
101 3
        $this->currentName = $currentName;
102
103 3
        return $this;
104
    }
105
106
    /**
107
     * Get the sidebar items.
108
     *
109
     * @return \Arcanesoft\Sidebar\Entities\ItemCollection
110
     */
111 18
    public function getItems()
112
    {
113 18
        return $this->items;
114
    }
115
116
    /* -----------------------------------------------------------------
117
     |  Main Methods
118
     | -----------------------------------------------------------------
119
     */
120
121
    /**
122
     * Add a routed item.
123
     *
124
     * @param  string       $name
125
     * @param  string       $title
126
     * @param  string       $route
127
     * @param  array        $parameters
128
     * @param  string|null  $icon
129
     *
130
     * @return self
131
     */
132 3
    public function addRouteItem($name, $title, $route, array $parameters = [], $icon = null)
133
    {
134 3
        return $this->addItem($name, $title, route($route, $parameters), $icon);
135
    }
136
137
    /**
138
     * Add an item.
139
     *
140
     * @param  string       $name
141
     * @param  string       $title
142
     * @param  string       $url
143
     * @param  string|null  $icon
144
     *
145
     * @return self
146
     */
147 9
    public function addItem($name, $title, $url = '#', $icon = null)
148
    {
149 9
        return $this->add(compact('name', 'title', 'url', 'icon'));
150
    }
151
152
    /**
153
     * Add an item from array.
154
     *
155
     * @param  array  $array
156
     *
157
     * @return self
158
     */
159 12
    public function add(array $array)
160
    {
161 12
        $item = Item::makeFromArray($array);
162
163 12
        if ($item->allowed())
164 12
            $this->items->push($item);
165
166 12
        return $this;
167
    }
168
169
    /**
170
     * Load items from multiple config keys.
171
     *
172
     * @param  string  $key
173
     *
174
     * @return self
175
     */
176 3
    public function loadItemsFromConfig($key)
177
    {
178 3
        foreach (config($key, []) as $key) {
179 3
            if (config()->has($key)) {
180 3
                $this->add(config($key));
181 1
            }
182 2
            else {
183
                // Throw an exception ??
184
            }
185 1
        }
186
187 3
        return $this;
188
    }
189
190
    /**
191
     * Render the sidebar.
192
     *
193
     * @param  string|null  $view
194
     * @param  array|null   $data
195
     *
196
     * @return \Illuminate\Support\HtmlString
197
     */
198 6
    public function render($view = '_includes.sidebar.default', array $data = [])
199
    {
200 6
        $this->syncCurrentName()->setView($view);
201
202 6
        return new HtmlString(
203 6
            view($this->view, array_merge($data, ['sidebarItems' => $this->getItems()]))->render()
204 1
        );
205
    }
206
207
    /* -----------------------------------------------------------------
208
     |  Check Methods
209
     | -----------------------------------------------------------------
210
     */
211
212
    /**
213
     * Check if the sidebar has items.
214
     *
215
     * @return bool
216
     */
217 9
    public function hasItems()
218
    {
219 9
        return ! $this->items->isEmpty();
220
    }
221
222
    /* -----------------------------------------------------------------
223
     |  Other Methods
224
     | -----------------------------------------------------------------
225
     */
226
227
    /**
228
     * Sync the current name wih the sidebar items.
229
     *
230
     * @return self
231
     */
232 6
    private function syncCurrentName()
233
    {
234 6
        $this->items->setCurrent($this->currentName);
235
236 6
        return $this;
237
    }
238
}
239