Passed
Push — master ( 7aacbf...ad9236 )
by Peter
07:41
created

NavigationBuilder::createDropdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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->createFilesItem();
50
51
        $navigation->addItem($item, static::BASE_WEIGHT);
52
    }
53
54
    /**
55
     * @return Item
56
     * @throws \Opulence\Routing\Urls\UrlException
57
     */
58
    protected function createFileCategoriesItem(): Item
59
    {
60
        $text = 'files:fileCategories';
61
        $icon = 'folder';
62
63
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_CATEGORIES, [], $icon);
64
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_CATEGORIES);
65
66
        $item = new Item($button);
67
        $item->setResource($resource);
68
69
        return $item;
70
    }
71
72
    /**
73
     * @return Item
74
     * @throws \Opulence\Routing\Urls\UrlException
75
     */
76
    protected function createFilesItem(): Item
77
    {
78
        $text = 'files:files';
79
        $icon = 'attachment';
80
81
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILES, [], $icon);
82
        $resource = $this->getAdminResource(Routes::ROUTE_FILES);
83
84
        $item = new Item($button);
85
        $item->setResource($resource);
86
87
        $item->setIntent(Item::INTENT_DROPDOWN);
88
        $item->setAttribute(Html5::ATTR_ID, 'nav-files');
89
90
        if (count($item) > 0 && $item instanceof ITag) {
91
            $item[0]->setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
0 ignored issues
show
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

91
            $item[0]->/** @scrutinizer ignore-call */ 
92
                      setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
Loading history...
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

91
            $item[0]->/** @scrutinizer ignore-call */ 
92
                      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...
92
        }
93
94
        $item[] = $this->createDropdown();
95
96
        return $item;
97
    }
98
99
    /**
100
     * @return Dropdown
101
     * @throws \Opulence\Routing\Urls\UrlException
102
     */
103
    protected function createDropdown(): Dropdown
104
    {
105
        $dropdown = new Dropdown();
106
        $dropdown[] = $this->createFileCategoriesItem();
107
        $dropdown[] = $this->createFilesItem();
108
        $dropdown[] = $this->createFileDownloadsItem();
109
110
        return $dropdown;
111
    }
112
113
    /**
114
     * @return Item
115
     * @throws \Opulence\Routing\Urls\UrlException
116
     */
117
    protected function createFileDownloadsItem(): Item
118
    {
119
        $text = 'files:fileDownloads';
120
        $icon = 'file_download';
121
122
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_DOWNLOADS, [], $icon);
123
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_DOWNLOADS);
124
125
        $item = new Item($button);
126
        $item->setResource($resource);
127
128
        return $item;
129
    }
130
131
    /**
132
     * @param string $resource
133
     *
134
     * @return string
135
     */
136
    protected function getAdminResource(string $resource): string
137
    {
138
        return sprintf('admin_resource_%s', $resource);
139
    }
140
}
141