Completed
Pull Request — master (#301)
by
unknown
04:34
created

ActiveChecker::checkExactOrSub()   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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
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
66
          $regex = mb_substr($pattern, 6);
67
68
          if(preg_match($regex, request()->path()) == 1) {
69
            return true;
70
          }
71
72
          return false;
73
        }
74 28
        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 3
    private function isExplicitActive($active)
89
    {
90 3
        foreach ($active as $url) {
91 3
            if ($this->checkExact($url)) {
92 3
                return true;
93
            }
94
        }
95
96 1
        return false;
97
    }
98
}
99