NavigationBuilder::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Events\Listeners;
6
7
use AbterPhp\Contact\Constant\Resource;
8
use AbterPhp\Contact\Constant\Route;
9
use AbterPhp\Framework\Constant\Html5;
10
use AbterPhp\Framework\Events\NavigationReady;
11
use AbterPhp\Framework\Html\Component\ButtonFactory;
12
use AbterPhp\Framework\Html\ITag;
13
use AbterPhp\Framework\Navigation\Dropdown;
14
use AbterPhp\Framework\Navigation\Item;
15
use AbterPhp\Framework\Navigation\Navigation;
16
17
class NavigationBuilder
18
{
19
    const BASE_WEIGHT = 900;
20
21
    /** @var ButtonFactory */
22
    protected $buttonFactory;
23
24
    /**
25
     * NavigationRegistrar constructor.
26
     *
27
     * @param ButtonFactory $buttonFactory
28
     */
29
    public function __construct(ButtonFactory $buttonFactory)
30
    {
31
        $this->buttonFactory = $buttonFactory;
32
    }
33
34
    /**
35
     * @param NavigationReady $event
36
     */
37
    public function handle(NavigationReady $event)
38
    {
39
        $navigation = $event->getNavigation();
40
41
        if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) {
42
            return;
43
        }
44
45
        $item   = $this->createContactItem();
46
47
        $navigation->addItem($item, static::BASE_WEIGHT);
48
    }
49
50
    /**
51
     * @return Item
52
     * @throws \Opulence\Routing\Urls\UrlException
53
     */
54
    protected function createFormsItem(): Item
55
    {
56
        $text = 'contact:forms';
57
        $icon = 'assignment';
58
59
        $button   = $this->buttonFactory->createFromName($text, Route::CONTACT_FORMS_LIST, [], $icon);
60
        $resource = $this->getAdminResource(Resource::CONTACT_FORMS);
61
62
        $item = new Item($button);
63
        $item->setResource($resource);
64
65
        return $item;
66
    }
67
68
    /**
69
     * @return Item
70
     * @throws \Opulence\Routing\Urls\UrlException
71
     */
72
    protected function createContactItem(): Item
73
    {
74
        $text = 'contact:contact';
75
        $icon = 'contacts';
76
77
        $button   = $this->buttonFactory->createFromName($text, Route::CONTACT_FORMS_LIST, [], $icon);
78
        $resource = $this->getAdminResource(Resource::CONTACT_FORMS);
79
80
        $item = new Item($button);
81
        $item->setResource($resource);
82
83
        $item->setIntent(Item::INTENT_DROPDOWN);
84
        $item->setAttribute(Html5::ATTR_ID, 'nav-contact');
85
86
        if (!empty($item[0]) && $item[0] instanceof ITag) {
87
            $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

87
            $item[0]->/** @scrutinizer ignore-call */ 
88
                      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

87
            $item[0]->/** @scrutinizer ignore-call */ 
88
                      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...
88
        }
89
90
        $item[1] = $this->createDropdown();
91
92
        return $item;
93
    }
94
95
    /**
96
     * @return Dropdown
97
     * @throws \Opulence\Routing\Urls\UrlException
98
     */
99
    protected function createDropdown(): Dropdown
100
    {
101
        $dropdown = new Dropdown();
102
        $dropdown[] = $this->createFormsItem();
103
104
        return $dropdown;
105
    }
106
107
    /**
108
     * @param string $resource
109
     *
110
     * @return string
111
     */
112
    protected function getAdminResource(string $resource): string
113
    {
114
        return sprintf('admin_resource_%s', $resource);
115
    }
116
}
117