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

GateFilter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
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 41
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 22 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
        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