Completed
Push — master ( b2d063...c3dd2f )
by Jeroen
03:08
created

ActiveChecker::checkExact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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