Completed
Push — master ( 64d085...390ef3 )
by wen
11:09
created

HasNavigation::getParentPageId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getNavigation()
20
    {
21
        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...
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function addToNavigation($priority = 100, $badge = null)
28
    {
29
        $nav = $this->getNavigation();
30
        if ($this->hasParentPageId()) {
31
            $nav = $nav->getPages()->findById($this->getParentPageId());
32
            if (is_null($nav)) {
33
                throw new InvalidArgumentException(
34
                    sprintf(
35
                        'Not Found "%s" navigation',
36
                        $this->getParentPageId()
37
                    )
38
                );
39
            }
40
41
        }
42
43
        $page = $this->makePage($priority, $badge);
44
45
        $nav->addPage($page);
46
47
        return $page;
48
    }
49
50
    /**
51
     * page
52
     *
53
     * @param int  $priority
54
     * @param null $badge
55
     *
56
     * @return \Sco\Admin\Navigation\Page
57
     */
58
    protected function makePage($priority = 100, $badge = null)
59
    {
60
        $page = new Page($this);
61
        $page->setPriority($priority)
62
            ->setIcon($this->getIcon())
63
            ->setAccessLogic(function () {
64
                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...
65
            });
66
67
        if ($badge) {
68
            if (!($badge instanceof BadgeInterface)) {
69
                $badge = new Badge($badge);
70
            }
71
            $page->addBadge($badge);
72
        }
73
74
        $page->setIcon($this->getIcon());
75
76
        return $page;
77
    }
78
79
    public function hasParentPageId()
80
    {
81
        return $this->parentPageId !== null;
82
    }
83
84
    public function getParentPageId()
85
    {
86
        return $this->parentPageId;
87
    }
88
89
    public function setParentPageId($value)
90
    {
91
        $this->parentPageId = $value;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param array $parameters
98
     *
99
     * @return string
100
     */
101
    public function getViewUrl(array $parameters = [])
102
    {
103
        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...
104
105
        return route('admin.model.index', $parameters, false);
106
    }
107
108
    public function getIcon()
109
    {
110
        return $this->icon;
111
    }
112
113
    public function setIcon($value)
114
    {
115
        $this->icon = $value;
116
117
        return $this;
118
    }
119
}
120