Completed
Push — master ( 61c64d...b03532 )
by ARCANEDEV
02:46
created

Manager::loadItemsFromConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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