Completed
Pull Request — master (#556)
by
unknown
05:03 queued 01:54
created

GateFilter::isVisible()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 5
cts 6
cp 0.8333
rs 8.9457
c 0
b 0
f 0
cc 6
nc 6
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
        if (isset($item['model'])) {
33
            return $this->gate->allows($item['can'], $item['model']);
34
        }
35
36 2
        if (is_array($item['can'])) {
37
            foreach ($item['can'] as $can) {
38
                if ($this->gate->allows($can)) {
39
                    return true;
40
                }
41
            }
42
43
            return false;
44
        }
45
46
        return $this->gate->allows($item['can']);
47
    }
48
}
49