Completed
Push — master ( 22b591...fa6610 )
by Jeroen
04:16
created

ActiveChecker   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 59
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B isActive() 0 16 5
A checkExact() 0 4 1
A checkSub() 0 4 1
A containsActive() 0 10 3
A isExplicitActive() 0 10 3
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Http\Request;
6
7
class ActiveChecker
8
{
9
    private $request;
10
11 27
    public function __construct(Request $request)
12
    {
13 27
        $this->request = $request;
14 27
    }
15
16 25
    public function isActive($item)
17
    {
18 25
        if (isset($item['active'])) {
19 3
            return $this->isExplicitActive($item['active']);
20
        }
21
22 22
        if (isset($item['submenu'])) {
23 8
            return $this->containsActive($item['submenu']);
24
        }
25
26 19
        if (isset($item['url'])) {
27 17
            return $this->checkExact($item['url']) || $this->checkSub($item['url']);
28
        }
29
30 2
        return false;
31
    }
32
33
34 20
    protected function checkExact($url)
35
    {
36 20
        return $this->request->is($url);
37
    }
38
39 8
    protected function checkSub($url)
40
    {
41 8
        return $this->request->is($url.'/*');
42
    }
43
44 8
    protected function containsActive($items)
45
    {
46 8
        foreach ($items as $item) {
47 5
            if ($this->isActive($item)) {
48 3
                return true;
49
            }
50 5
        }
51
52 5
        return false;
53
    }
54
55 3
    private function isExplicitActive($active)
56
    {
57 3
        foreach ($active as $url) {
58 3
            if ($this->checkExact($url)) {
59 2
                return true;
60
            }
61 1
        }
62
63 1
        return false;
64
    }
65
}
66