Passed
Push — master ( f475f6...720950 )
by Florian
03:26
created

GateFilter::isVisible()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 23
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 9.2222
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 44
    public function __construct(Gate $gate)
13
    {
14 44
        $this->gate = $gate;
15 44
    }
16
17 44
    public function transform($item, Builder $builder)
18
    {
19 44
        if (! $this->isVisible($item)) {
20 4
            return false;
21
        }
22
23 44
        return $item;
24
    }
25
26 44
    protected function isVisible($item)
27
    {
28 44
        if (! isset($item['can'])) {
29 41
            return true;
30
        }
31
32 4
        $args = [];
33
34 4
        if (isset($item['model'])) {
35
            $args = $item['model'];
36
        }
37
38 4
        if (! is_array($item['can'])) {
39 3
            return $this->gate->allows($item['can'], $args);
40
        }
41
42 1
        foreach ($item['can'] as $can) {
43 1
            if ($this->gate->allows($can, $args)) {
44 1
                return true;
45
            }
46
        }
47
48 1
        return false;
49
    }
50
}
51