Completed
Push — master ( ac6d1f...0637c9 )
by Jeroen
04:16
created

ActiveChecker   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 70
ccs 32
cts 32
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A checkSub() 0 4 1
A __construct() 0 5 1
B isActive() 0 16 5
A checkExact() 0 4 1
A checkPattern() 0 8 1
A containsActive() 0 10 3
A isExplicitActive() 0 10 3
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
class ActiveChecker
10
{
11
    private $request;
12
13
    private $url;
14
15 31
    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 31
        $this->request = $request;
18 31
        $this->url = $url;
19 31
    }
20
21 29
    public function isActive($item)
22
    {
23 29
        if (isset($item['active'])) {
24 3
            return $this->isExplicitActive($item['active']);
25
        }
26
27 26
        if (isset($item['submenu'])) {
28 8
            return $this->containsActive($item['submenu']);
29
        }
30
31 23
        if (isset($item['url'])) {
32 20
            return $this->checkExact($item['url']) || $this->checkSub($item['url']);
33
        }
34
35 3
        return false;
36
    }
37
38 23
    protected function checkExact($url)
39
    {
40 23
        return $this->checkPattern($url);
41
    }
42
43 9
    protected function checkSub($url)
44
    {
45 9
        return $this->checkPattern($url.'/*');
46
    }
47
48 23
    protected function checkPattern($pattern)
49
    {
50 23
        $fullUrlPattern = $this->url->to($pattern);
51
52 23
        $fullUrl = $this->request->fullUrl();
53
54 23
        return Str::is($fullUrlPattern, $fullUrl);
55
    }
56
57 8
    protected function containsActive($items)
58
    {
59 8
        foreach ($items as $item) {
60 5
            if ($this->isActive($item)) {
61 3
                return true;
62
            }
63 5
        }
64
65 5
        return false;
66
    }
67
68 3
    private function isExplicitActive($active)
69
    {
70 3
        foreach ($active as $url) {
71 3
            if ($this->checkExact($url)) {
72 2
                return true;
73
            }
74 1
        }
75
76 1
        return false;
77
    }
78
}
79