Completed
Pull Request — master (#131)
by
unknown
09:49 queued 26s
created

ActiveChecker::isActive()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 5
nc 5
nop 1
crap 5
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 35
    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 35
        $this->request = $request;
18 35
        $this->url = $url;
19 35
    }
20
21 33
    public function isActive($item)
22
    {
23 33
        if (isset($item['submenu'])) {
24 8
            return $this->containsActive($item['submenu']);
25
        }
26
27 30
        if (isset($item['active'])) {
28 6
            return $this->isExplicitActive($item['active']);
29
        }
30
31 27
        if (isset($item['href'])) {
32 15
            return $this->checkPattern($item['href']);
33
        }
34
35
        // Support URL for backwards compatibility
36 15
        if (isset($item['url'])) {
37 14
            return $this->checkPattern($item['url']);
38
        }
39
40 1
        return false;
41
    }
42
43 26
    protected function checkPattern($pattern)
44
    {
45 26
        $fullUrlPattern = $this->url->to($pattern);
46
47
        $fullUrl = $this->request->fullUrl();
48 29
49
        return Str::is($fullUrlPattern, $fullUrl);
50 29
    }
51
52
    protected function containsActive($items)
53 14
    {
54
        foreach ($items as $item) {
55 14
            if ($this->isActive($item)) {
56
                return true;
57
            }
58 29
        }
59
60 29
        return false;
61
    }
62 29
63
    private function isExplicitActive($active)
64 29
    {
65
        if (! is_array($active)) {
66
            return $active;
67 8
        }
68
69 8
        foreach ($active as $url) {
70 5
            if ($this->checkPattern($url)) {
71 5
                return true;
72
            }
73
        }
74
75 5
        return false;
76
    }
77
}
78