HasNavigation::setPriority()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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
    /**
13
     * The page id name.
14
     *
15
     * @var string|null
16
     */
17
    protected $pageId;
18
19
    /**
20
     * The page icon class name.
21
     *
22
     * @var string|null
23
     */
24
    protected $icon;
25
26
    /**
27
     * The page belong to page id name.
28
     *
29
     * @var string
30
     */
31
    protected $parentPageId;
32
33
    /**
34
     * The page priority.
35
     *
36
     * @var int
37
     */
38
    protected $priority = 100;
39
40
    /**
41
     * Get the page id
42
     *
43
     * @return string|null
44
     */
45
    public function getPageId()
46
    {
47
        return $this->pageId;
48
    }
49
50
    /**
51
     * Set the page id
52
     *
53
     * @param string $pageId
54
     *
55
     * @return $this
56
     */
57
    public function setPageId(string $pageId)
58
    {
59
        $this->pageId = $pageId;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Get the page icon class name.
66
     *
67
     * @return string|null
68
     */
69
    public function getIcon()
70
    {
71
        return $this->icon;
72
    }
73
74
    /**
75
     * Set the page icon class name.
76
     *
77
     * @param string $icon
78
     *
79
     * @return $this
80
     */
81
    public function setIcon(string $icon)
82
    {
83
        $this->icon = $icon;
84
85
        return $this;
86
    }
87
88
    /**
89
     *
90
     * @return string
91
     */
92
    public function getParentPageId(): string
93
    {
94
        return $this->parentPageId;
95
    }
96
97
    /**
98
     * @param string $parentPageId
99
     *
100
     * @return $this
101
     */
102
    public function setParentPageId(string $parentPageId)
103
    {
104
        $this->parentPageId = $parentPageId;
105
106
        return $this;
107
    }
108
109
    public function hasParentPageId()
110
    {
111
        return $this->parentPageId !== null;
112
    }
113
114
    /**
115
     * @return int
116
     */
117
    public function getPriority(): int
118
    {
119
        return $this->priority;
120
    }
121
122
    /**
123
     * @param int $priority
124
     *
125
     * @return $this
126
     */
127
    public function setPriority(int $priority)
128
    {
129
        $this->priority = $priority;
130
131
        return $this;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getNavigation()
138
    {
139
        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...
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function addToNavigation($badge = null)
146
    {
147
        $nav = $this->getNavigation();
148
        if ($this->hasParentPageId()) {
149
            $nav = $nav->getPages()->findById($this->getParentPageId());
150
            if (is_null($nav)) {
151
                throw new InvalidArgumentException(
152
                    sprintf(
153
                        'Not Found "%s" navigation',
154
                        $this->getParentPageId()
155
                    )
156
                );
157
            }
158
        }
159
160
        $page = $this->makePage($badge);
161
162
        $nav->addPage($page);
163
164
        return $page;
165
    }
166
167
    /**
168
     * Make page
169
     *
170
     * @param string|Closure|null $badge
171
     *
172
     * @return \Sco\Admin\Navigation\Page
173
     */
174
    protected function makePage($badge = null)
175
    {
176
        $page = new Page($this);
177
        $page->setPriority($this->getPriority())
178
            ->setIcon($this->getIcon())
179
            ->setId($this->getPageId())
180
            ->setAccessLogic(function () {
181
                return $this->isDisplay();
0 ignored issues
show
Bug introduced by
It seems like isDisplay() 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...
182
            });
183
184
        if ($badge) {
185
            if (! ($badge instanceof BadgeInterface)) {
186
                $badge = new Badge($badge);
187
            }
188
            $page->addBadge($badge);
189
        }
190
191
        return $page;
192
    }
193
194
    /**
195
     * Get the model display url.
196
     *
197
     * @param array $parameters
198
     *
199
     * @return string
200
     */
201
    public function getDisplayUrl(array $parameters = [])
202
    {
203
        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...
204
205
        return route('admin.model.index', $parameters, false);
206
    }
207
}
208