Item::getClasses()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Larabros\Laranav\Menus;
4
5
use Illuminate\Support\Collection;
6
7
class Item
8
{
9
    /**
10
     * The unique name used to identify an `Item` instance.
11
     *
12
     * @var string
13
     */
14
    protected $title;
15
16
    /**
17
     * The URL for the item.
18
     *
19
     * @var string
20
     */
21
    protected $url;
22
23
    /**
24
     * The active state of the item.
25
     *
26
     * @var bool
27
     */
28
    protected $isActive;
29
30
    /**
31
     * The CSS class used to highlight an active item.
32
     *
33
     * @var string
34
     */
35
    protected $activeClass;
36
37
    /**
38
     * A `Collection` of `Item` instances for any child items.
39
     *
40
     * @var Collection|null
41
     */
42
    protected $children;
43
44
    /**
45
     * The CSS class used to highlight an item with children.
46
     *
47
     * @var string
48
     */
49
    protected $childrenClass;
50
51
    /**
52
     * Create a new `Item` instance.
53
     *
54
     * @param string          $title
55
     * @param string          $url
56
     * @param boolean         $isActive
57
     * @param Collection|null $children
58
     * @param string          $activeClass
59
     * @param string          $childrenClass
60
     */
61 48
    public function __construct(
62
        $title,
63
        $url = '#',
64
        $isActive = false,
65
        Collection $children = null,
66
        $activeClass = 'active',
67
        $childrenClass = 'dropdown'
68
    ) {
69 48
        $this->title         = $title;
70 48
        $this->url           = $url;
71 48
        $this->isActive      = $isActive;
72 48
        $this->children      = $children;
73 48
        $this->activeClass   = $activeClass;
74 48
        $this->childrenClass = $childrenClass;
75 48
    }
76
77
    /**
78
     * Return title of the `Item` instance.
79
     *
80
     * @return string
81
     */
82 6
    public function getTitle()
83
    {
84 6
        return $this->title;
85
    }
86
87
    /**
88
     * Return the URL of the `Item` instance.
89
     *
90
     * @return string
91
     */
92 6
    public function getUrl()
93
    {
94 6
        return $this->url;
95
    }
96
97
    /**
98
     * Returns the active status of the item.
99
     *
100
     * @return boolean
101
     */
102 12
    public function isActive()
103
    {
104 12
        return $this->isActive;
105
    }
106
107
    /**
108
     * Check if this item has any children.
109
     *
110
     * @return boolean
111
     */
112 8
    public function hasChildren()
113
    {
114 8
        return (isset($this->children) && !is_null($this->children));
115
    }
116
117
    /**
118
     * Returns any child items as a `Collection`.
119
     *
120
     * @return Collection
121
     */
122 8
    public function getChildren()
123
    {
124 8
        return $this->hasChildren() ? $this->children : new Collection();
125
    }
126
127
    /**
128
     * Returns any CSS classes that should be applied to this item when, for
129
     * example highlighting an active link, or displaying a menu item with children.
130
     *
131
     * @return string
132
     */
133 16
    public function getClasses()
134
    {
135 16
        $classesString = '';
136
137
        // Check if this is the active item
138 16
        if ($this->isActive()) {
139 8
            $classesString .= "$this->activeClass ";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
140 8
        }
141
142 16
        if ($this->hasChildren()) {
143 4
            $classesString .= "$this->childrenClass ";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
144 4
        }
145
146 16
        return trim($classesString);
147
    }
148
}
149