Completed
Push — master ( 20f1f2...9061ff )
by wen
13:56
created

HasNavigation::setPageId()   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
    /**
13
     * The page id name
14
     *
15
     * @var string
16
     */
17
    protected $pageId;
18
19
    /**
20
     * The page icon class name
21
     *
22
     * @var string
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
     * @return mixed
42
     */
43
    public function getPageId()
44
    {
45
        return $this->pageId;
46
    }
47
48
    /**
49
     * @param mixed $pageId
50
     *
51
     * @return $this
52
     */
53
    public function setPageId($pageId)
54
    {
55
        $this->pageId = $pageId;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getIcon()
64
    {
65
        return $this->icon;
66
    }
67
68
    /**
69
     * @param mixed $icon
70
     *
71
     * @return $this
72
     */
73
    public function setIcon($icon)
74
    {
75
        $this->icon = $icon;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getParentPageId()
84
    {
85
        return $this->parentPageId;
86
    }
87
88
    /**
89
     * @param mixed $parentPageId
90
     *
91
     * @return $this
92
     */
93
    public function setParentPageId($parentPageId)
94
    {
95
        $this->parentPageId = $parentPageId;
96
97
        return $this;
98
    }
99
100
    public function hasParentPageId()
101
    {
102
        return $this->parentPageId !== null;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getPriority(): int
109
    {
110
        return $this->priority;
111
    }
112
113
    /**
114
     * @param int $priority
115
     *
116
     * @return $this
117
     */
118
    public function setPriority(int $priority)
119
    {
120
        $this->priority = $priority;
121
122
        return $this;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getNavigation()
129
    {
130
        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...
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function addToNavigation($badge = null)
137
    {
138
        $nav = $this->getNavigation();
139
        if ($this->hasParentPageId()) {
140
            $nav = $nav->getPages()->findById($this->getParentPageId());
141
            if (is_null($nav)) {
142
                throw new InvalidArgumentException(
143
                    sprintf(
144
                        'Not Found "%s" navigation',
145
                        $this->getParentPageId()
146
                    )
147
                );
148
            }
149
        }
150
151
        $page = $this->makePage($badge);
152
153
        $nav->addPage($page);
154
155
        return $page;
156
    }
157
158
    /**
159
     * page
160
     *
161
     * @param null $badge
162
     *
163
     * @return \Sco\Admin\Navigation\Page
164
     */
165
    protected function makePage($badge = null)
166
    {
167
        $page = new Page($this);
168
        $page->setPriority($this->getPriority())
0 ignored issues
show
Bug introduced by
The method serId() does not seem to exist on object<Sco\Admin\Navigation\Page>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            ->setIcon($this->getIcon())
170
            ->serId($this->getPageId())
171
            ->setAccessLogic(function () {
172
                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...
173
            });
174
175
        if ($badge) {
176
            if (!($badge instanceof BadgeInterface)) {
177
                $badge = new Badge($badge);
178
            }
179
            $page->addBadge($badge);
180
        }
181
182
        $page->setIcon($this->getIcon());
183
184
        return $page;
185
    }
186
187
    /**
188
     * @param array $parameters
189
     *
190
     * @return string
191
     */
192
    public function getViewUrl(array $parameters = [])
193
    {
194
        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...
195
196
        return route('admin.model.index', $parameters, false);
197
    }
198
}
199