Passed
Pull Request — 2.x (#586)
by Bekzat
04:25
created

isActiveNavigation()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 6
nop 3
dl 0
loc 11
ccs 4
cts 5
cp 0.8
crap 4.128
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Str;
4
use Illuminate\Support\Facades\Request;
5
6
if (!function_exists('moduleRoute')) {
7
    /**
8
     * @param string $moduleName
9
     * @param string $prefix
10
     * @param string $action
11
     * @param array $parameters
12
     * @param bool $absolute
13
     * @return string
14
     */
15
    function moduleRoute($moduleName, $prefix, $action, $parameters = [], $absolute = true)
16
    {
17
        $routeName = 'admin.' . ($prefix ? $prefix . '.' : '') . Str::camel($moduleName) . '.' . $action;
18
        return route($routeName, $parameters, $absolute);
19
    }
20
}
21
22
if (!function_exists('getNavigationUrl')) {
23
    /**
24
     * @param array $element
25
     * @param string $key
26
     * @param string|null $prefix
27
     * @return string
28
     */
29
    function getNavigationUrl($element, $key, $prefix = null)
30
    {
31 47
        $isModule = $element['module'] ?? false;
32
33 47
        if ($isModule) {
34
            $action = $element['route'] ?? 'index';
35
            return moduleRoute($key, $prefix, $action);
36 47
        } elseif ($element['raw'] ?? false) {
37
            return !empty($element['route']) ? $element['route'] : '#';
38
        }
39
40 47
        return !empty($element['route']) ? route($element['route'], $element['params'] ?? []) : '#';
41
    }
42
43
}
44
45
if (!function_exists('isActiveNavigation')) {
46
    /**
47
     * @param array $navigationElement
48
     * @param string $navigationKey
49
     * @param string $activeNavigationKey
50
     * @return bool
51
     */
52
    function isActiveNavigation($navigationElement, $navigationKey, $activeNavigationKey)
53
    {
54 47
        $keysAreMatching = isset($activeNavigationKey) && $navigationKey === $activeNavigationKey;
55
56 47
        if ($keysAreMatching) {
57
            return true;
58
        }
59
60 47
        $urlsAreMatching = ($navigationElement['raw'] ?? false) && Str::endsWith(Request::url(), $navigationElement['route']);
61
62 47
        return $urlsAreMatching;
63
    }
64
}
65