Passed
Push — master ( 7d9253...14d345 )
by Peter
02:08
created

NavigationBuilder::createItem()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 21
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Events\Listeners;
6
7
use AbterPhp\Files\Constant\Routes;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Events\NavigationReady;
10
use AbterPhp\Framework\Html\Component\ButtonFactory;
11
use AbterPhp\Framework\Html\ITag;
12
use AbterPhp\Framework\Navigation\Dropdown;
13
use AbterPhp\Framework\Navigation\Item;
14
use AbterPhp\Framework\Navigation\Navigation;
15
16
class NavigationBuilder
17
{
18
    const BASE_WEIGHT = 600;
19
20
    /** @var ButtonFactory */
21
    protected $buttonFactory;
22
23
    /**
24
     * NavigationRegistrar constructor.
25
     *
26
     * @param ButtonFactory $buttonFactory
27
     */
28
    public function __construct(ButtonFactory $buttonFactory)
29
    {
30
        $this->buttonFactory = $buttonFactory;
31
    }
32
33
    /**
34
     * @param NavigationReady $event
35
     */
36
    /**
37
     * @param NavigationReady $event
38
     *
39
     * @throws \Opulence\Routing\Urls\UrlException
40
     */
41
    public function handle(NavigationReady $event)
42
    {
43
        $navigation = $event->getNavigation();
44
45
        if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) {
46
            return;
47
        }
48
49
        $item = $this->createItem();
50
51
        $navigation->addItem($item, static::BASE_WEIGHT);
52
    }
53
54
    /**
55
     * @return Item
56
     * @throws \Opulence\Routing\Urls\UrlException
57
     */
58
    protected function createItem(): Item
59
    {
60
        $text = 'files:files';
61
        $icon = 'attachment';
62
63
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILES, [], $icon);
64
        $resource = $this->getAdminResource(Routes::ROUTE_FILES);
65
66
        $item = new Item($button);
67
        $item->setResource($resource);
68
69
        $item->setIntent(Item::INTENT_DROPDOWN);
70
        $item->setAttribute(Html5::ATTR_ID, 'nav-files');
71
72
        if (!empty($item[0]) && $item[0] instanceof ITag) {
73
            $item[0]->setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $item[0]->/** @scrutinizer ignore-call */ 
74
                      setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');

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...
Bug introduced by
The method setAttribute() does not exist on AbterPhp\Framework\Html\INode. It seems like you code against a sub-type of AbterPhp\Framework\Html\INode such as AbterPhp\Framework\Html\ITag or AbterPhp\Framework\Grid\Row\Row or AbterPhp\Framework\Html\Tag or AbterPhp\Framework\Html\IComponent or AbterPhp\Framework\Html\Component. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $item[0]->/** @scrutinizer ignore-call */ 
74
                      setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
Loading history...
74
        }
75
76
        $item[] = $this->createDropdown();
77
78
        return $item;
79
    }
80
81
    /**
82
     * @return Dropdown
83
     * @throws \Opulence\Routing\Urls\UrlException
84
     */
85
    protected function createDropdown(): Dropdown
86
    {
87
        $dropdown   = new Dropdown();
88
        $dropdown[] = $this->createFileCategoriesItem();
89
        $dropdown[] = $this->createFilesItem();
90
        $dropdown[] = $this->createFileDownloadsItem();
91
92
        return $dropdown;
93
    }
94
95
    /**
96
     * @return Item
97
     * @throws \Opulence\Routing\Urls\UrlException
98
     */
99
    protected function createFileCategoriesItem(): Item
100
    {
101
        $text = 'files:fileCategories';
102
        $icon = 'folder';
103
104
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_CATEGORIES, [], $icon);
105
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_CATEGORIES);
106
107
        $item = new Item($button);
108
        $item->setResource($resource);
109
110
        return $item;
111
    }
112
113
    /**
114
     * @return Item
115
     * @throws \Opulence\Routing\Urls\UrlException
116
     */
117
    protected function createFilesItem(): Item
118
    {
119
        $text = 'files:files';
120
        $icon = 'file';
121
122
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILES, [], $icon);
123
        $resource = $this->getAdminResource(Routes::ROUTE_FILES);
124
125
        $item = new Item($button);
126
        $item->setResource($resource);
127
128
        return $item;
129
    }
130
131
    /**
132
     * @return Item
133
     * @throws \Opulence\Routing\Urls\UrlException
134
     */
135
    protected function createFileDownloadsItem(): Item
136
    {
137
        $text = 'files:fileDownloads';
138
        $icon = 'file_download';
139
140
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_DOWNLOADS, [], $icon);
141
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_DOWNLOADS);
142
143
        $item = new Item($button);
144
        $item->setResource($resource);
145
146
        return $item;
147
    }
148
149
    /**
150
     * @param string $resource
151
     *
152
     * @return string
153
     */
154
    protected function getAdminResource(string $resource): string
155
    {
156
        return sprintf('admin_resource_%s', $resource);
157
    }
158
}
159