Passed
Push — master ( 3ee2e0...a71027 )
by Diego
04:43
created

HrefFilter::makeHref()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 2
b 0
f 1
nc 3
nop 1
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu\Filters;
4
5
use JeroenNoten\LaravelAdminLte\Helpers\MenuItemHelper;
6
7
class HrefFilter implements FilterInterface
8
{
9
    /**
10
     * Transforms a menu item. Compile the href HTML attribute when situable.
11
     *
12
     * @param  array  $item  A menu item
13
     * @return array
14
     */
15 57
    public function transform($item)
16
    {
17 57
        if (! MenuItemHelper::isHeader($item)) {
18 56
            $item['href'] = $this->makeHref($item);
19
        }
20
21 57
        return $item;
22
    }
23
24
    /**
25
     * Make and return the href HTML attribute for a menu item.
26
     *
27
     * @param  array  $item  A menu item
28
     * @return string
29
     */
30 56
    protected function makeHref($item)
31
    {
32
        // If url attribute is available, use it to make the href property.
33
        // Otherwise, check if route attribute is available.
34
35 56
        if (! empty($item['url'])) {
36 48
            return url($item['url']);
37 16
        } elseif (! empty($item['route'])) {
38 4
            return $this->makeHrefFromRouteAttr($item['route']);
39
        }
40
41
        // When url and route are not available, return a default value.
42
43 14
        return '#';
44
    }
45
46
    /**
47
     * Make and return the href HTML attribute fom the route attribute of a
48
     * menu item.
49
     *
50
     * @param  mixed  $routeAttr  The route attribute of a menu item
51
     * @return string
52
     */
53 4
    protected function makeHrefFromRouteAttr($routeAttr)
54
    {
55 4
        $routeName = $routeParams = null;
56
57
        // Check type of the route attribute.
58
59 4
        if (is_array($routeAttr)) {
60 1
            $routeName = $routeAttr[0] ?? null;
61 1
            $routeParams = is_array($routeAttr[1]) ? $routeAttr[1] : null;
62 4
        } elseif (is_string($routeAttr)) {
63 3
            $routeName = $routeAttr;
64
        }
65
66 4
        return $routeName ? route($routeName, $routeParams) : '#';
67
    }
68
}
69