Passed
Push — master ( a74eae...3ee2e0 )
by Diego
04:09
created

GateFilter::isAllowed()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 7
cts 7
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;
4
5
use Illuminate\Support\Facades\Gate;
6
7
class GateFilter implements FilterInterface
8
{
9
    /**
10
     * Transforms a menu item. Add the restricted property to a menu item
11
     * when the it's not authorized by the Laravel's Gate feature.
12
     *
13
     * @param  array  $item  A menu item
14
     * @return array
15
     */
16 58
    public function transform($item)
17
    {
18
        // Set a special property when the item is not authorized by Gate.
19
        // Items with this property set will be filtered out from the menu.
20
21 58
        if (! $this->isAuthorized($item)) {
22 9
            $item['restricted'] = true;
23
        }
24
25 58
        return $item;
26
    }
27
28
    /**
29
     * Check if a menu item is authorized to be shown for the current user.
30
     *
31
     * @param  array  $item  A menu item
32
     * @return bool
33
     */
34 58
    protected function isAuthorized($item)
35
    {
36
        // Check if there are any permission defined for the item.
37
38 58
        if (empty($item['can'])) {
39 53
            return true;
40
        }
41
42
        // Read the extra arguments (a db model instance can be used).
43
44 10
        $args = ! empty($item['model']) ? $item['model'] : [];
45
46
        // Check if the current user can perform the configured permissions.
47
48 10
        if (is_string($item['can']) || is_array($item['can'])) {
49 9
            return Gate::any($item['can'], $args);
50
        }
51
52 1
        return true;
53
    }
54
}
55