Passed
Push — master ( 322b59...05d3bb )
by Peter
02:21
created

NavigationBuilder::createFileDownloadsItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\I18n\ITranslator;
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
    public function handle(NavigationReady $event)
37
    {
38
        $navigation = $event->getNavigation();
39
40
        if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) {
41
            return;
42
        }
43
44
        $dropdown = new Dropdown();
45
        $dropdown[] = $this->createFileCategoriesItem();
46
        $dropdown[] = $this->createFilesItem();
47
        $dropdown[] = $this->createFileDownloadsItem();
48
49
        $item   = $this->createFilesItem();
50
        $item->setIntent(Item::INTENT_DROPDOWN);
51
        $item->setAttribute(Html5::ATTR_ID, 'nav-files');
52
        $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

52
        $item[0]->/** @scrutinizer ignore-call */ 
53
                  setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
Loading history...
53
        $item[1] = $dropdown;
54
55
        $navigation->addItem($item, static::BASE_WEIGHT);
56
    }
57
58
    /**
59
     * @return Item
60
     * @throws \Opulence\Routing\Urls\UrlException
61
     */
62
    protected function createFileCategoriesItem(): Item
63
    {
64
        $text = 'files:fileCategories';
65
        $icon = 'folder';
66
67
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_CATEGORIES, [], $icon);
68
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_CATEGORIES);
69
70
        $item = new Item($button);
71
        $item->setResource($resource);
72
73
        return $item;
74
    }
75
76
    /**
77
     * @return Item
78
     * @throws \Opulence\Routing\Urls\UrlException
79
     */
80
    protected function createFilesItem(): Item
81
    {
82
        $text = 'files:files';
83
        $icon = 'attachment';
84
85
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILES, [], $icon);
86
        $resource = $this->getAdminResource(Routes::ROUTE_FILES);
87
88
        $item = new Item($button);
89
        $item->setResource($resource);
90
91
        return $item;
92
    }
93
94
    /**
95
     * @return Item
96
     * @throws \Opulence\Routing\Urls\UrlException
97
     */
98
    protected function createFileDownloadsItem(): Item
99
    {
100
        $text = 'files:fileDownloads';
101
        $icon = 'file_download';
102
103
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_FILE_DOWNLOADS, [], $icon);
104
        $resource = $this->getAdminResource(Routes::ROUTE_FILE_DOWNLOADS);
105
106
        $item = new Item($button);
107
        $item->setResource($resource);
108
109
        return $item;
110
    }
111
112
    /**
113
     * @param string $resource
114
     *
115
     * @return string
116
     */
117
    protected function getAdminResource(string $resource): string
118
    {
119
        return sprintf('admin_resource_%s', $resource);
120
    }
121
}
122