Completed
Push — master ( ac6d1f...0637c9 )
by Jeroen
04:16
created

ActiveChecker::isExplicitActive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
class ActiveChecker
10
{
11
    private $request;
12
13
    private $url;
14
15 31
    public function __construct(Request $request, UrlGenerator $url)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
16
    {
17 31
        $this->request = $request;
18 31
        $this->url = $url;
19 31
    }
20
21 29
    public function isActive($item)
22
    {
23 29
        if (isset($item['active'])) {
24 3
            return $this->isExplicitActive($item['active']);
25
        }
26
27 26
        if (isset($item['submenu'])) {
28 8
            return $this->containsActive($item['submenu']);
29
        }
30
31 23
        if (isset($item['url'])) {
32 20
            return $this->checkExact($item['url']) || $this->checkSub($item['url']);
33
        }
34
35 3
        return false;
36
    }
37
38 23
    protected function checkExact($url)
39
    {
40 23
        return $this->checkPattern($url);
41
    }
42
43 9
    protected function checkSub($url)
44
    {
45 9
        return $this->checkPattern($url.'/*');
46
    }
47
48 23
    protected function checkPattern($pattern)
49
    {
50 23
        $fullUrlPattern = $this->url->to($pattern);
51
52 23
        $fullUrl = $this->request->fullUrl();
53
54 23
        return Str::is($fullUrlPattern, $fullUrl);
55
    }
56
57 8
    protected function containsActive($items)
58
    {
59 8
        foreach ($items as $item) {
60 5
            if ($this->isActive($item)) {
61 3
                return true;
62
            }
63 5
        }
64
65 5
        return false;
66
    }
67
68 3
    private function isExplicitActive($active)
69
    {
70 3
        foreach ($active as $url) {
71 3
            if ($this->checkExact($url)) {
72 2
                return true;
73
            }
74 1
        }
75
76 1
        return false;
77
    }
78
}
79