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

ActiveChecker::checkSub()   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
rs 10
ccs 2
cts 2
cp 1
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 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