Passed
Push — 2.x ( 63a107...267eec )
by Quentin
13:48 queued 09:25
created

moduleRoute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 5
dl 0
loc 22
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
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
        // Fix module name case
18 30
        $moduleName = Str::camel($moduleName);
19
20
        // Create base route name
21 30
        $routeName = 'admin.' . ($prefix ? $prefix . '.' : '');
22
23
        // Prefix it with module name only if prefix doesn't contains it already
24
        if (
25 30
            config('twill.allow_duplicates_on_route_names', true) ||
26
            ($prefix !== $moduleName &&
27 30
                !Str::endsWith($prefix, '.' . $moduleName))
28
        ) {
29 30
            $routeName .= "{$moduleName}.";
30
        }
31
32
        //  Add the action name
33 30
        $routeName .= $action;
34
35
        // Build the route
36 30
        return route($routeName, $parameters, $absolute);
37
    }
38
}
39
40
if (!function_exists('getNavigationUrl')) {
41
    /**
42
     * @param array $element
43
     * @param string $key
44
     * @param string|null $prefix
45
     * @return string
46
     */
47
    function getNavigationUrl($element, $key, $prefix = null)
48
    {
49 46
        $isModule = $element['module'] ?? false;
50
51 46
        if ($isModule) {
52 6
            $action = $element['route'] ?? 'index';
53 6
            return moduleRoute($key, $prefix, $action);
54 46
        } elseif ($element['raw'] ?? false) {
55
            return !empty($element['route']) ? $element['route'] : '#';
56
        }
57
58 46
        return !empty($element['route']) ? route($element['route'], $element['params'] ?? []) : '#';
59
    }
60
61
}
62
63
if (!function_exists('isActiveNavigation')) {
64
    /**
65
     * @param array $navigationElement
66
     * @param string $navigationKey
67
     * @param string $activeNavigationKey
68
     * @return bool
69
     */
70
    function isActiveNavigation($navigationElement, $navigationKey, $activeNavigationKey)
71
    {
72 46
        $keysAreMatching = isset($activeNavigationKey) && $navigationKey === $activeNavigationKey;
73
74 46
        if ($keysAreMatching) {
75 7
            return true;
76
        }
77
78 46
        $urlsAreMatching = ($navigationElement['raw'] ?? false) && Str::endsWith(Request::url(), $navigationElement['route']);
79
80 46
        return $urlsAreMatching;
81
    }
82
}
83