Passed
Push — master ( b256e5...d776af )
by Peter
02:28
created

NavigationBuilder::createUserGroupItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Events\Listeners;
6
7
use AbterPhp\Admin\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\Navigation\Dropdown;
12
use AbterPhp\Framework\Navigation\Item;
13
use AbterPhp\Framework\Navigation\Navigation;
14
use AbterPhp\Framework\Navigation\UserBlock;
15
use Opulence\Sessions\ISession;
16
17
class NavigationBuilder
18
{
19
    const FIRST_ITEM_WEIGHT = 0;
20
21
    const DEFAULT_BASE_WEIGHT = 1000;
22
23
    /** @var ISession */
24
    protected $session;
25
26
    /** @var ButtonFactory */
27
    protected $buttonFactory;
28
29
    /**
30
     * NavigationRegistrar constructor.
31
     *
32
     * @param ISession      $session
33
     * @param ButtonFactory $buttonFactory
34
     */
35
    public function __construct(ISession $session, ButtonFactory $buttonFactory)
36
    {
37
        $this->session       = $session;
38
        $this->buttonFactory = $buttonFactory;
39
    }
40
41
    /**
42
     * @param NavigationReady $event
43
     *
44
     * @throws \Opulence\Routing\Urls\URLException
45
     */
46
    public function handle(NavigationReady $event)
47
    {
48
        $navigation = $event->getNavigation();
49
50
        if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) {
51
            return;
52
        }
53
54
        $this->insertFirstItem($navigation);
55
56
        $dropdown = new Dropdown();
57
        $dropdown[] = $this->createUserItem();
58
        $dropdown[] = $this->createUserGroupItem();
59
        $dropdown[] = $this->createLogoutItem();
60
61
        $item   = $this->createUserItem();
62
        $item->setIntent(Item::INTENT_DROPDOWN);
63
        $item->setAttribute(Html5::ATTR_ID, 'nav-users');
64
        $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

64
        $item[0]->/** @scrutinizer ignore-call */ 
65
                  setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
Loading history...
65
        $item[1] = $dropdown;
66
67
        $navigation->addItem($item, static::DEFAULT_BASE_WEIGHT);
68
    }
69
70
    /**
71
     * @param Navigation $navigation
72
     */
73
    protected function insertFirstItem(Navigation $navigation)
74
    {
75
        $firstItem = new Item(null, [UserBlock::class]);
76
77
        $firstItem[] = $this->createUserBlock();
78
        $firstItem[] = $this->createDropdown();
79
80
        $navigation->addItem($firstItem, static::FIRST_ITEM_WEIGHT);
81
    }
82
83
    /**
84
     * @return UserBlock
85
     */
86
    protected function createUserBlock(): UserBlock
87
    {
88
        return new UserBlock($this->session);
89
    }
90
91
    /**
92
     * @return Dropdown
93
     */
94
    protected function createDropdown(): Dropdown
95
    {
96
        $text = 'framework:logout';
97
98
        $button = $this->buttonFactory->createFromName($text, Routes::ROUTE_LOGOUT, []);
99
100
        return new Dropdown(new Item($button));
101
    }
102
103
    /**
104
     * @return Item
105
     * @throws \Opulence\Routing\Urls\UrlException
106
     */
107
    protected function createUserItem(): Item
108
    {
109
        $text = 'admin:users';
110
        $icon = 'person';
111
112
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_USERS, [], $icon);
113
        $resource = $this->getAdminResource(Routes::ROUTE_USERS);
114
115
        $item = new Item($button);
116
        $item->setResource($resource);
117
118
        return $item;
119
    }
120
121
    /**
122
     * @return Item
123
     * @throws \Opulence\Routing\Urls\UrlException
124
     */
125
    protected function createUserGroupItem(): Item
126
    {
127
        $text = 'admin:userGroups';
128
        $icon = 'group';
129
130
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_USER_GROUPS, [], $icon);
131
        $resource = $this->getAdminResource(Routes::ROUTE_USER_GROUPS);
132
133
        $item = new Item($button);
134
        $item->setResource($resource);
135
136
        return $item;
137
    }
138
139
    /**
140
     * @return Item
141
     * @throws \Opulence\Routing\Urls\UrlException
142
     */
143
    protected function createLogoutItem(): Item
144
    {
145
        $text = 'admin:logout';
146
        $icon = 'settings_power';
147
148
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_LOGOUT, [], $icon);
149
        $resource = $this->getAdminResource(Routes::ROUTE_LOGOUT);
150
151
        $item = new Item($button);
152
        $item->setResource($resource);
153
154
        return $item;
155
    }
156
157
    /**
158
     * @param string $resource
159
     *
160
     * @return string
161
     */
162
    protected function getAdminResource(string $resource): string
163
    {
164
        return sprintf('admin_resource_%s', $resource);
165
    }
166
}
167