NavigationBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 62
c 3
b 0
f 0
dl 0
loc 182
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdminResource() 0 3 1
A createUserBlock() 0 3 1
A createUserBlockDropdown() 0 8 1
A createProfileItem() 0 8 1
A handle() 0 24 2
A createApiClientsItem() 0 12 1
A createUserGroupItem() 0 12 1
A insertFirstItem() 0 8 1
A createLogoutItem() 0 8 1
A __construct() 0 4 1
A createUserItem() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Events\Listeners;
6
7
use AbterPhp\Admin\Constant\Resource;
8
use AbterPhp\Admin\Constant\Route;
9
use AbterPhp\Framework\Constant\Html5;
10
use AbterPhp\Framework\Events\NavigationReady;
11
use AbterPhp\Framework\Html\Attribute;
12
use AbterPhp\Framework\Html\Factory\Button as ButtonFactory;
13
use AbterPhp\Framework\Navigation\Dropdown;
14
use AbterPhp\Framework\Navigation\Item;
15
use AbterPhp\Framework\Navigation\Navigation;
16
use AbterPhp\Framework\Navigation\UserBlock;
17
use Opulence\Sessions\ISession;
18
19
class NavigationBuilder
20
{
21
    public const FIRST_ITEM_WEIGHT = 0;
22
23
    public const DEFAULT_BASE_WEIGHT = 1000;
24
25
    /** @var ISession */
26
    protected ISession $session;
27
28
    /** @var ButtonFactory */
29
    protected ButtonFactory $buttonFactory;
30
31
    /**
32
     * NavigationBuilder constructor.
33
     *
34
     * @param ISession      $session
35
     * @param ButtonFactory $buttonFactory
36
     */
37
    public function __construct(ISession $session, ButtonFactory $buttonFactory)
38
    {
39
        $this->session       = $session;
40
        $this->buttonFactory = $buttonFactory;
41
    }
42
43
    /**
44
     * @param NavigationReady $event
45
     *
46
     * @throws \Opulence\Routing\Urls\URLException
47
     */
48
    public function handle(NavigationReady $event)
49
    {
50
        $navigation = $event->getNavigation();
51
52
        if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) {
53
            return;
54
        }
55
56
        $this->insertFirstItem($navigation);
57
58
        $dropdown   = new Dropdown();
59
        $dropdown[] = $this->createUserItem();
60
        $dropdown[] = $this->createUserGroupItem();
61
62
        $mainItem = $this->createUserItem();
63
        $mainItem->setIntent(Item::INTENT_DROPDOWN);
64
        $mainItem->setAttribute(new Attribute(Html5::ATTR_ID, 'nav-users'));
65
        $mainItem[0]->setAttribute(new Attribute(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

65
        $mainItem[0]->/** @scrutinizer ignore-call */ 
66
                      setAttribute(new Attribute(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\Html\Tag. ( Ignorable by Annotation )

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

65
        $mainItem[0]->/** @scrutinizer ignore-call */ 
66
                      setAttribute(new Attribute(Html5::ATTR_HREF, 'javascript:void(0);'));
Loading history...
66
        $mainItem[1] = $dropdown;
67
68
        $logout = $this->createLogoutItem();
69
70
        $navigation->addWithWeight(static::DEFAULT_BASE_WEIGHT, $mainItem);
71
        $navigation->addWithWeight(static::DEFAULT_BASE_WEIGHT, $logout);
72
    }
73
74
    /**
75
     * @param Navigation $navigation
76
     *
77
     * @throws \Opulence\Routing\Urls\UrlException
78
     */
79
    protected function insertFirstItem(Navigation $navigation)
80
    {
81
        $firstItem = new Item(null, [UserBlock::class]);
82
83
        $firstItem[] = $this->createUserBlock();
84
        $firstItem[] = $this->createUserBlockDropdown();
85
86
        $navigation->addWithWeight(static::FIRST_ITEM_WEIGHT, $firstItem);
87
    }
88
89
    /**
90
     * @return UserBlock
91
     */
92
    protected function createUserBlock(): UserBlock
93
    {
94
        return new UserBlock($this->session);
95
    }
96
97
    /**
98
     * @return Dropdown
99
     * @throws \Opulence\Routing\Urls\UrlException
100
     */
101
    protected function createUserBlockDropdown(): Dropdown
102
    {
103
        $items   = [];
104
        $items[] = $this->createProfileItem();
105
        $items[] = $this->createApiClientsItem();
106
        $items[] = $this->createLogoutItem();
107
108
        return new Dropdown($items);
109
    }
110
111
    /**
112
     * @return Item
113
     * @throws \Opulence\Routing\Urls\UrlException
114
     */
115
    protected function createApiClientsItem(): Item
116
    {
117
        $text = 'admin:apiClients';
118
        $icon = 'vpn_key';
119
120
        $button   = $this->buttonFactory->createFromName($text, Route::API_CLIENTS_LIST, [], $icon);
121
        $resource = $this->getAdminResource(Resource::API_CLIENTS);
122
123
        $item = new Item($button);
124
        $item->setResource($resource);
125
126
        return $item;
127
    }
128
129
    /**
130
     * @return Item
131
     * @throws \Opulence\Routing\Urls\UrlException
132
     */
133
    protected function createProfileItem(): Item
134
    {
135
        $text = 'admin:profile';
136
        $icon = 'account_box';
137
138
        $button = $this->buttonFactory->createFromName($text, Route::PROFILE_EDIT, [], $icon);
139
140
        return new Item($button);
141
    }
142
143
    /**
144
     * @return Item
145
     * @throws \Opulence\Routing\Urls\UrlException
146
     */
147
    protected function createUserItem(): Item
148
    {
149
        $text = 'admin:users';
150
        $icon = 'person';
151
152
        $button   = $this->buttonFactory->createFromName($text, Route::USERS_LIST, [], $icon);
153
        $resource = $this->getAdminResource(Resource::USERS);
154
155
        $item = new Item($button);
156
        $item->setResource($resource);
157
158
        return $item;
159
    }
160
161
    /**
162
     * @return Item
163
     * @throws \Opulence\Routing\Urls\UrlException
164
     */
165
    protected function createUserGroupItem(): Item
166
    {
167
        $text = 'admin:userGroups';
168
        $icon = 'group';
169
170
        $button   = $this->buttonFactory->createFromName($text, Route::USER_GROUPS_LIST, [], $icon);
171
        $resource = $this->getAdminResource(Resource::USER_GROUPS);
172
173
        $item = new Item($button);
174
        $item->setResource($resource);
175
176
        return $item;
177
    }
178
179
    /**
180
     * @return Item
181
     * @throws \Opulence\Routing\Urls\UrlException
182
     */
183
    protected function createLogoutItem(): Item
184
    {
185
        $text = 'admin:logout';
186
        $icon = 'settings_power';
187
188
        $button = $this->buttonFactory->createFromName($text, Route::LOGOUT_EXECUTE, [], $icon);
189
190
        return new Item($button);
191
    }
192
193
    /**
194
     * @param string $resource
195
     *
196
     * @return string
197
     */
198
    protected function getAdminResource(string $resource): string
199
    {
200
        return sprintf('admin_resource_%s', $resource);
201
    }
202
}
203