Completed
Pull Request — master (#301)
by
unknown
02:56
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 34
    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 34
        $this->request = $request;
18 34
        $this->url = $url;
19 34
    }
20
21 32
    public function isActive($item)
22
    {
23 32
        if (isset($item['active'])) {
24 3
            return $this->isExplicitActive($item['active']);
25
        }
26
27 29
        if (isset($item['submenu'])) {
28 8
            return $this->containsActive($item['submenu']);
29
        }
30
31 26
        if (isset($item['href'])) {
32 14
            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 25
    protected function checkExactOrSub($url)
44
    {
45 25
        return $this->checkExact($url) || $this->checkSub($url);
46
    }
47
48 28
    protected function checkExact($url)
49
    {
50 28
        return $this->checkPattern($url);
51
    }
52
53 13
    protected function checkSub($url)
54
    {
55 13
        return $this->checkPattern($url.'/*') || $this->checkPattern($url.'?*');
56
    }
57
58 28
    protected function checkPattern($pattern)
59
    {
60 28
        $fullUrlPattern = $this->url->to($pattern);
61
62 28
        $fullUrl = $this->request->fullUrl();
63
64 28
        if(mb_substr($pattern, 0, 6) === 'regex:') {
65
            $regex = mb_substr($pattern, 6);
66
67
            if(preg_match($regex, request()->path()) == 1) {
68
                return true;
69
            }
70
            
71
            return false;
72
        }
73 28
        return Str::is($fullUrlPattern, $fullUrl);
74
    }
75
76 8
    protected function containsActive($items)
77
    {
78 8
        foreach ($items as $item) {
79 5
            if ($this->isActive($item)) {
80 5
                return true;
81
            }
82
        }
83
84 5
        return false;
85
    }
86
87 3
    private function isExplicitActive($active)
88
    {
89 3
        foreach ($active as $url) {
90 3
            if ($this->checkExact($url)) {
91 3
                return true;
92
            }
93
        }
94
95 1
        return false;
96
    }
97
}
98