Completed
Pull Request — master (#556)
by
unknown
02:44
created

GateFilter::isVisible()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 5
cts 6
cp 0.8333
rs 8.9137
c 0
b 0
f 0
cc 6
nc 9
nop 1
crap 6.1666
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