GateFilter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10
c 3
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 10 2
A isAuthorized() 0 19 5
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 56
    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 56
        if (! $this->isAuthorized($item)) {
22 7
            $item['restricted'] = true;
23
        }
24
25 56
        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 56
    protected function isAuthorized($item)
35
    {
36
        // Check if there are any permission defined for the item.
37
38 56
        if (empty($item['can'])) {
39 51
            return true;
40
        }
41
42
        // Read the extra arguments (a db model instance can be used).
43
44 8
        $args = ! empty($item['model']) ? $item['model'] : [];
45
46
        // Check if the current user can perform the configured permissions.
47
48 8
        if (is_string($item['can']) || is_array($item['can'])) {
49 7
            return Gate::any($item['can'], $args);
50
        }
51
52 1
        return true;
53
    }
54
}
55