Completed
Push — master ( c722af...139fcc )
by
unknown
02:47
created

GateFilter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 43
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 8 2
B isVisible() 0 24 6
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;
4
5
use Illuminate\Contracts\Auth\Access\Gate;
6
use JeroenNoten\LaravelAdminLte\Menu\Builder;
7
8
class GateFilter implements FilterInterface
9
{
10
    protected $gate;
11
12 20
    public function __construct(Gate $gate)
13
    {
14 20
        $this->gate = $gate;
15 20
    }
16
17 19
    public function transform($item, Builder $builder)
18
    {
19 19
        if (! $this->isVisible($item)) {
20 2
            return false;
21
        }
22
23 19
        return $item;
24
    }
25
26 19
    protected function isVisible($item)
27
    {
28 19
        if (! isset($item['can'])) {
29 17
            return true;
30
        }
31
32 2
        $args = [];
33
34
        if (isset($item['model'])) {
35
            $args = $item['model'];
36 2
        }
37
38
        if (! is_array($item['can'])) {
39
            return $this->gate->allows($item['can'], $args);
40
        }
41
42
        foreach ($item['can'] as $can) {
43
            if ($this->gate->allows($can, $args)) {
44
                return true;
45
            }
46
        }
47
48
        return false;
49
    }
50
}
51