Completed
Pull Request — master (#34)
by
unknown
02:57
created

ActiveChecker   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.1%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 15
c 3
b 1
f 0
lcom 1
cbo 1
dl 0
loc 63
ccs 27
cts 29
cp 0.931
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isActive() 0 16 4
A isActiveUrl() 0 4 2
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 25
    public function __construct(Request $request)
12
    {
13 25
        $this->request = $request;
14 25
    }
15
16 24
    public function isActive($item)
17
    {
18 24
        if (isset($item['active'])) {
19 2
            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->isActiveUrl($item['url']);
28
        }
29
30 2
        return false;
31
    }
32
33 19
    protected function isActiveUrl($url)
34
    {
35 19
        return $this->checkExact($url) || $this->checkSub($url);
36
    }
37
38 19
    protected function checkExact($url)
39
    {
40 19
        return $this->request->is($url);
41
    }
42
43 8
    protected function checkSub($url)
44
    {
45 8
        return $this->request->is($url.'/*');
46
    }
47
48 8
    protected function containsActive($items)
49
    {
50 8
        foreach ($items as $item) {
51 5
            if ($this->isActive($item)) {
52 3
                return true;
53
            }
54 5
        }
55
56 5
        return false;
57
    }
58
59 2
    private function isExplicitActive($active)
60
    {
61 2
        foreach ($active as $url) {
62 2
            if ($this->isActiveUrl($url)) {
63 2
                return true;
64
            }
65
        }
66
67
        return false;
68
    }
69
}
70