Completed
Push — master ( 93e691...5acfa0 )
by wen
12:06
created

HasNavigation::setPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Component\Concerns;
4
5
use InvalidArgumentException;
6
use KodiComponents\Navigation\Contracts\BadgeInterface;
7
use Sco\Admin\Navigation\Badge;
8
use Sco\Admin\Navigation\Page;
9
10
trait HasNavigation
11
{
12
    protected $icon;
13
14
    protected $parentPageId;
15
16
    protected $priority = 100;
17
18
    /**
19
     * @return mixed
20
     */
21
    public function getIcon()
22
    {
23
        return $this->icon;
24
    }
25
26
    /**
27
     * @param mixed $icon
28
     *
29
     * @return $this
30
     */
31
    public function setIcon($icon)
32
    {
33
        $this->icon = $icon;
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function getParentPageId()
42
    {
43
        return $this->parentPageId;
44
    }
45
46
    /**
47
     * @param mixed $parentPageId
48
     *
49
     * @return $this
50
     */
51
    public function setParentPageId($parentPageId)
52
    {
53
        $this->parentPageId = $parentPageId;
54
55
        return $this;
56
    }
57
58
    public function hasParentPageId()
59
    {
60
        return $this->parentPageId !== null;
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    public function getPriority(): int
67
    {
68
        return $this->priority;
69
    }
70
71
    /**
72
     * @param int $priority
73
     *
74
     * @return $this
75
     */
76
    public function setPriority(int $priority)
77
    {
78
        $this->priority = $priority;
79
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getNavigation()
87
    {
88
        return $this->app['admin.navigation'];
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function addToNavigation($badge = null)
95
    {
96
        $nav = $this->getNavigation();
97
        if ($this->hasParentPageId()) {
98
            $nav = $nav->getPages()->findById($this->getParentPageId());
99
            if (is_null($nav)) {
100
                throw new InvalidArgumentException(
101
                    sprintf(
102
                        'Not Found "%s" navigation',
103
                        $this->getParentPageId()
104
                    )
105
                );
106
            }
107
        }
108
109
        $page = $this->makePage($badge);
110
111
        $nav->addPage($page);
112
113
        return $page;
114
    }
115
116
    /**
117
     * page
118
     *
119
     * @param null $badge
120
     *
121
     * @return \Sco\Admin\Navigation\Page
122
     */
123
    protected function makePage($badge = null)
124
    {
125
        $page = new Page($this);
126
        $page->setPriority($this->getPriority())
127
            ->setIcon($this->getIcon())
128
            ->setAccessLogic(function () {
129
                return $this->isView();
0 ignored issues
show
Bug introduced by
It seems like isView() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
130
            });
131
132
        if ($badge) {
133
            if (!($badge instanceof BadgeInterface)) {
134
                $badge = new Badge($badge);
135
            }
136
            $page->addBadge($badge);
137
        }
138
139
        $page->setIcon($this->getIcon());
140
141
        return $page;
142
    }
143
144
    /**
145
     * @param array $parameters
146
     *
147
     * @return string
148
     */
149
    public function getViewUrl(array $parameters = [])
150
    {
151
        array_unshift($parameters, $this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
152
153
        return route('admin.model.index', $parameters, false);
154
    }
155
}
156