Completed
Push — master ( 15f051...29343b )
by Oleg
05:33
created

Item::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 6
crap 2
1
<?php
2
3
namespace Malezha\Menu\Entity;
4
5
use Malezha\Menu\Traits\DisplayRule;
6
use Malezha\Menu\Traits\HasAttributes;
7
use Malezha\Menu\Traits\IsUrlEqual;
8
9
class Item
10
{
11
    use HasAttributes, DisplayRule, IsUrlEqual;
12
13
    /**
14
     * @var Link
15
     */
16
    protected $link;
17
18
    /**
19
     * @var Builder
20
     */
21
    protected $builder;
22
23
    /**
24
     * @param Builder $builder
25
     * @param string $name
26
     * @param array $attributes
27
     * @param string $title
28
     * @param string $url
29
     * @param array $linkAttributes
30
     */
31 10
    function __construct(Builder $builder, $name, $attributes = [], $title = '', $url = '#', $linkAttributes = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
    {
33 10
        $title = empty($title) ? $name : $title;
34 10
        $this->builder = $builder;
35 10
        $this->attributes = new Attributes($attributes);
36 10
        $this->link = new Link($title, $url, $linkAttributes);
37 10
    }
38
39
    /**
40
     * @return Link
41
     */
42 5
    public function getLink()
43
    {
44 5
        return $this->link;
45
    }
46
47
    /**
48
     * @param array $attributes
49
     * @return string
50
     */
51 3
    public function buildAttributes($attributes = [])
52
    {
53 3
        $attributes = $this->isActive() ?
54 3
            Attributes::mergeArrayValues($this->builder->activeAttributes()->all(), $attributes) :
55 3
            $attributes;
56
57 3
        return $this->attributes->build($attributes);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63 3
    public function isActive()
64
    {
65 3
        $currentUrl = app('request')->url();
66 3
        $url = url($this->getLink()->getUrl());
67
        
68 3
        return $this->isUrlEqual($url, $currentUrl);
69
    }
70
}