Completed
Push — master ( 35a7c4...8ca7eb )
by
unknown
04:34
created

ActiveChecker::checkSub()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Contracts\Routing\UrlGenerator;
8
9
class ActiveChecker
10
{
11
    private $request;
12
13
    private $url;
14
15 36
    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 36
        $this->request = $request;
18 36
        $this->url = $url;
19 36
    }
20
21 34
    public function isActive($item)
22
    {
23 34
        if (isset($item['submenu'])) {
24 8
            return $this->containsActive($item['submenu']);
25
        }
26
27 31
        if (isset($item['active'])) {
28 7
            return $this->isExplicitActive($item['active']);
29
        }
30
31 27
        if (isset($item['href'])) {
32 15
            return $this->checkExactOrSub($item['href']);
33
        }
34
35
        // Support URL for backwards compatibility
36 15
        if (isset($item['url'])) {
37 14
            return $this->checkExactOrSub($item['url']);
38
        }
39
40 1
        return false;
41
    }
42
43 26
    protected function checkExactOrSub($url)
44
    {
45 26
        return $this->checkExact($url) || $this->checkSub($url);
46
    }
47
48 30
    protected function checkExact($url)
49
    {
50 30
        return $this->checkPattern($url);
51
    }
52
53 14
    protected function checkSub($url)
54
    {
55 14
        return $this->checkPattern($url.'/*') || $this->checkPattern($url.'?*');
56
    }
57
58 30
    protected function checkPattern($pattern)
59
    {
60 30
        $fullUrlPattern = $this->url->to($pattern);
61
62 30
        $fullUrl = $this->request->fullUrl();
63
64 30
        if (mb_substr($pattern, 0, 6) === 'regex:') {
65 1
            $regex = mb_substr($pattern, 6);
66
67 1
            if (preg_match($regex, $this->request->path()) == 1) {
68 1
                return true;
69
            }
70
71
            return false;
72
        }
73
74 29
        return Str::is($fullUrlPattern, $fullUrl);
75
    }
76
77 8
    protected function containsActive($items)
78
    {
79 8
        foreach ($items as $item) {
80 5
            if ($this->isActive($item)) {
81 5
                return true;
82
            }
83
        }
84
85 5
        return false;
86
    }
87
88 7
    private function isExplicitActive($active)
89
    {
90 7
        if (! is_array($active)) {
91 3
            return $active;
92
        }
93
94 4
        foreach ($active as $url) {
95 4
            if ($this->checkExact($url)) {
96 4
                return true;
97
            }
98
        }
99
100 1
        return false;
101
    }
102
}
103