Completed
Push — master ( 210ca4...1b9b8e )
by Jeroen
02:57
created

ActiveChecker::checkPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Http\Request;
7
8
class ActiveChecker
9
{
10
    private $request;
11 27
12
    private $url;
13 27
14 27
    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...
15
    {
16 25
        $this->request = $request;
17
        $this->url = $url;
18 25
    }
19 3
20
    public function isActive($item)
21
    {
22 22
        if (isset($item['active'])) {
23 8
            return $this->isExplicitActive($item['active']);
24
        }
25
26 19
        if (isset($item['submenu'])) {
27 17
            return $this->containsActive($item['submenu']);
28
        }
29
30 2
        if (isset($item['url'])) {
31
            return $this->checkExact($item['url']) || $this->checkSub($item['url']);
32
        }
33 20
34
        return false;
35 20
    }
36
37
    protected function checkExact($url)
38 8
    {
39
        return $this->checkPattern($url);
40 8
    }
41
42
    protected function checkSub($url)
43 8
    {
44
        return $this->checkPattern($url . '/*');
45 8
    }
46 5
47 3
    protected function checkPattern($pattern)
48
    {
49 5
        $fullUrlPattern = $this->url->to($pattern);
50
51 5
        return $this->request->fullUrlIs($fullUrlPattern);
52
    }
53
54 3
    protected function containsActive($items)
55
    {
56 3
        foreach ($items as $item) {
57 3
            if ($this->isActive($item)) {
58 2
                return true;
59
            }
60 1
        }
61
62 1
        return false;
63
    }
64
65
    private function isExplicitActive($active)
66
    {
67
        foreach ($active as $url) {
68
            if ($this->checkExact($url)) {
69
                return true;
70
            }
71
        }
72
73
        return false;
74
    }
75
}
76