Completed
Push — master ( 25b1a7...f23563 )
by ARCANEDEV
03:32
created

Item::setUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace Arcanesoft\Sidebar\Entities;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Support\Arr;
6
use JsonSerializable;
7
8
/**
9
 * Class     Item
10
 *
11
 * @package  Arcanesoft\Sidebar\Entitites
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Item implements Arrayable, Jsonable, JsonSerializable
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The item name.
23
     *
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * The item title.
30
     *
31
     * @var string
32
     */
33
    protected $title;
34
35
    /**
36
     * The item url.
37
     *
38
     * @var string
39
     */
40
    protected $url;
41
42
    /**
43
     * The item icon.
44
     *
45
     * @var string
46
     */
47
    protected $icon;
48
49
    /**
50
     * The item active state.
51
     *
52
     * @var bool
53
     */
54
    protected $active = false;
55
56
    /**
57
     * The item roles.
58
     *
59
     * @var array
60
     */
61
    protected $roles      = [];
62
63
    /**
64
     * The item permissions.
65
     *
66
     * @var array
67
     */
68
    protected $permissions = [];
69
70
    /**
71
     * The item children (sub-items).
72
     *
73
     * @var \Arcanesoft\Sidebar\Entities\ItemCollection
74
     */
75
    protected $children;
76
77
    /* -----------------------------------------------------------------
78
     |  Constructor
79
     | -----------------------------------------------------------------
80
     */
81
82
    /**
83
     * Item constructor.
84
     *
85
     * @param  string       $name
86
     * @param  string       $title
87
     * @param  string       $url
88
     * @param  string|null  $icon
89
     */
90 24
    public function __construct($name, $title, $url, $icon = null)
91
    {
92 24
        $this->name     = $name;
93 24
        $this->title    = $title;
94 24
        $this->url      = $url;
95 24
        $this->icon     = $icon;
96 24
        $this->active   = false;
97 24
        $this->children = new ItemCollection;
98 24
    }
99
100
    /* -----------------------------------------------------------------
101
     |  Getters & Setters
102
     | -----------------------------------------------------------------
103
     */
104
105
    /**
106
     * Get the item name.
107
     *
108
     * @return string
109
     */
110 12
    public function name()
111
    {
112 12
        return $this->name;
113
    }
114
115
    /**
116
     * Get the item title.
117
     *
118
     * @return string
119
     */
120 18
    public function title()
121
    {
122
        /** @var \Illuminate\Translation\Translator $trans */
123 18
        $trans = trans();
124
125 18
        return $trans->has($this->title) ? $trans->get($this->title) : $this->title;
126
    }
127
128
    /**
129
     * Set the title.
130
     *
131
     * @param  string  $title
132
     *
133
     * @return self
134
     */
135
    public function setTitle($title)
136
    {
137
        $this->title = $title;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the item url.
144
     *
145
     * @return string
146
     */
147 12
    public function url()
148
    {
149 12
        return $this->url;
150
    }
151
152
    /**
153
     * Get the item icon.
154
     *
155
     * @return string|null
156
     */
157 15
    public function icon()
158
    {
159 15
        return $this->icon;
160
    }
161
162
    /**
163
     * Set the item icon.
164
     *
165
     * @param  string  $icon
166
     *
167
     * @return self
168
     */
169
    public function setIcon($icon)
170
    {
171
        $this->icon = $icon;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Set the current name.
178
     *
179
     * @param  string  $name
180
     *
181
     * @return self
182
     */
183 3
    public function setCurrent($name)
184
    {
185 3
        $this->children->setCurrent($name);
186 3
        $this->active = ($this->name === $name || $this->children->hasActiveItem());
187
188 3
        return $this;
189
    }
190
191
    /**
192
     * Get the roles.
193
     *
194
     * @return array
195
     */
196 9
    public function getRoles()
197
    {
198 9
        return $this->roles;
199
    }
200
201
    /**
202
     * Set the roles.
203
     *
204
     * @param  array  $roles
205
     *
206
     * @return self
207
     */
208 21
    public function setRoles(array $roles)
209
    {
210 21
        $this->roles = $roles;
211
212 21
        return $this;
213
    }
214
215
    /**
216
     * Get the permissions.
217
     *
218
     * @return array
219
     */
220 9
    public function getPermissions()
221
    {
222 9
        return $this->permissions;
223
    }
224
225
    /**
226
     * Set the permissions.
227
     *
228
     * @param  array  $permissions
229
     *
230
     * @return self
231
     */
232 21
    public function setPermissions(array $permissions)
233
    {
234 21
        $this->permissions = $permissions;
235
236 21
        return $this;
237
    }
238
239
    /**
240
     * Get the sub-items.
241
     *
242
     * @return \Arcanesoft\Sidebar\Entities\ItemCollection
243
     */
244 9
    public function children()
245
    {
246 9
        return $this->children;
247
    }
248
249
    /**
250
     * Get the active class.
251
     *
252
     * @param  string  $class
253
     *
254
     * @return string
255
     */
256 3
    public function activeClass($class = 'active')
257
    {
258 3
        return $this->isActive() ? $class : '';
259
    }
260
261
    /**
262
     * Get the sub-items class.
263
     *
264
     * @param  string  $class
265
     *
266
     * @return string
267
     */
268 3
    public function childrenClass($class = 'treeview')
269
    {
270 3
        return $this->hasChildren() ? $class : '';
271
    }
272
273
    /* -----------------------------------------------------------------
274
     |  Main Methods
275
     | -----------------------------------------------------------------
276
     */
277
    /**
278
     * Make the item.
279
     *
280
     * @param  string       $name
281
     * @param  string       $title
282
     * @param  string       $url
283
     * @param  string|null  $icon
284
     *
285
     * @return self
286
     */
287 9
    public static function make($name, $title, $url, $icon = null)
288
    {
289 9
        return new self($name, $title, $url, $icon);
290
    }
291
292
    /**
293
     * Make a Sidebar item from array.
294
     *
295
     * @param  array  $array
296
     *
297
     * @return self
298
     */
299 6
    public static function makeFromArray(array $array)
300
    {
301 6
        $item = self::make(
302 6
            $array['name'],
303 6
            $array['title'],
304 6
            self::getUrlFromArray($array),
305 6
            Arr::get($array, 'icon', null)
306 2
        );
307
308 6
        $item->setRoles(Arr::get($array, 'roles', []));
309 6
        $item->setPermissions(Arr::get($array, 'permissions', []));
310 6
        $item->addChildren(Arr::get($array, 'children', []));
311
312 6
        return $item;
313
    }
314
315
    /**
316
     * Get url from array.
317
     *
318
     * @param  array  $array
319
     *
320
     * @return string
321
     */
322 6
    private static function getUrlFromArray(array $array)
323
    {
324 6
        if (Arr::has($array, 'route'))
325 4
            return route(Arr::get($array, 'route'));
326
327 3
        return Arr::get($array, 'url', '#');
328
    }
329
330
    /**
331
     * Add children to the parent.
332
     *
333
     * @param  array  $children
334
     *
335
     * @return self
336
     */
337 6
    public function addChildren(array $children)
338
    {
339 6
        foreach ($children as $child) {
340 3
            $this->addChild($child);
341 2
        }
342
343 6
        return $this;
344
    }
345
346
    /**
347
     * Add a sub-item to the parent.
348
     *
349
     * @param  array  $child
350
     *
351
     * @return self
352
     */
353 3
    public function addChild(array $child)
354
    {
355 3
        $item = self::makeFromArray($child);
356
357 3
        if ($item->allowed())
358 3
            $this->children->push($item);
359
360 3
        return $this;
361
    }
362
363
    /* -----------------------------------------------------------------
364
     |  Check Methods
365
     | -----------------------------------------------------------------
366
     */
367
368
    /**
369
     * Check if the item is active one.
370
     *
371
     * @return bool
372
     */
373 15
    public function isActive()
374
    {
375 15
        return $this->active;
376
    }
377
378
    /**
379
     * Check if the item has children.
380
     *
381
     * @return bool
382
     */
383 9
    public function hasChildren()
384
    {
385 9
        return ! $this->children->isEmpty();
386
    }
387
388
    /**
389
     * Check the user is allowed to see this item.
390
     *
391
     * @return bool
392
     */
393 6
    public function allowed()
394
    {
395
        /** @var  \Arcanesoft\Contracts\Auth\Models\User  $user */
396 6
        $user = auth()->user();
397
398 6
        if (is_null($user) || ( ! $this->hasRoles() && ! $this->hasPermissions()))
399 6
            return true;
400
401
        if ($user->isAdmin())
402
            return true;
403
404
        foreach ($this->roles as $roleSlug) {
405
            if ($user->hasRoleSlug($roleSlug))
406
                return true;
407
        }
408
409
        foreach ($this->permissions as $permissionSlug) {
410
            if ($user->may($permissionSlug))
411
                return true;
412
        }
413
414
        return $this->children()->first(function (Item $child) {
415
            return $child->allowed();
416
        }, false);
417
    }
418
419
    /**
420
     * Check if the item has roles.
421
     *
422
     * @return bool
423
     */
424 6
    public function hasRoles()
425
    {
426 6
        return ! empty($this->roles);
427
    }
428
429
    /**
430
     * Check if the item has permissions.
431
     *
432
     * @return bool
433
     */
434 6
    public function hasPermissions()
435
    {
436 6
        return ! empty($this->permissions);
437
    }
438
439
    /* -----------------------------------------------------------------
440
     |  Other Methods
441
     | -----------------------------------------------------------------
442
     */
443
444
    /**
445
     * Get the instance as an array.
446
     *
447
     * @return array
448
     */
449 6
    public function toArray()
450
    {
451
        return [
452 6
            'name'        => $this->name(),
453 6
            'title'       => $this->title(),
454 6
            'url'         => $this->url(),
455 6
            'icon'        => $this->icon(),
456 6
            'active'      => $this->isActive(),
457 6
            'roles'       => $this->roles,
458 6
            'permissions' => $this->permissions,
459 6
            'children'    => $this->children->toArray(),
460 2
        ];
461
    }
462
463
    /**
464
     * Convert the object to its JSON representation.
465
     *
466
     * @param  int  $options
467
     *
468
     * @return string
469
     */
470 3
    public function toJson($options = 0)
471
    {
472 3
        return json_encode($this->jsonSerialize(), $options);
473
    }
474
475
    /**
476
     * Convert the object into something JSON serializable.
477
     *
478
     * @return array
479
     */
480 3
    public function jsonSerialize()
481
    {
482 3
        return $this->toArray();
483
    }
484
}
485