Passed
Push — master ( 1babca...ba4eaa )
by Peter
02:19
created

NavigationBuilder::createBlockItem()   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\Website\Events\Listeners;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Events\NavigationReady;
9
use AbterPhp\Framework\Html\Component\ButtonFactory;
10
use AbterPhp\Framework\Navigation\Dropdown;
11
use AbterPhp\Framework\Navigation\Extended;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Framework\Navigation\Extended was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use AbterPhp\Framework\Navigation\Item;
13
use AbterPhp\Framework\Navigation\Navigation;
14
use AbterPhp\Website\Constant\Routes;
15
16
class NavigationBuilder
17
{
18
    const BASE_WEIGHT = 400;
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->createPageItem();
46
        $dropdown[] = $this->createPageLayoutItem();
47
        $dropdown[] = $this->createBlockItem();
48
        $dropdown[] = $this->createBlockLayoutItem();
49
50
        $item   = $this->createPageItem();
51
        $item->setIntent(Item::INTENT_DROPDOWN);
52
        $item->setAttribute(Html5::ATTR_ID, 'nav-pages');
53
        $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

53
        $item[0]->/** @scrutinizer ignore-call */ 
54
                  setAttribute(Html5::ATTR_HREF, 'javascript:void(0);');
Loading history...
54
        $item[1] = $dropdown;
55
56
        $navigation->addItem($item, static::BASE_WEIGHT);
57
    }
58
59
    /**
60
     * @return Item
61
     * @throws \Opulence\Routing\Urls\UrlException
62
     */
63
    protected function createPageItem(): Item
64
    {
65
        $text = 'website:pages';
66
        $icon = 'text_format';
67
68
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_PAGES, [], $icon);
69
        $resource = $this->getAdminResource(Routes::ROUTE_PAGES);
70
71
        $item = new Item($button);
72
        $item->setResource($resource);
0 ignored issues
show
Bug introduced by
The method setResource() does not exist on AbterPhp\Framework\Navigation\Item. ( Ignorable by Annotation )

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

72
        $item->/** @scrutinizer ignore-call */ 
73
               setResource($resource);

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...
73
74
        return $item;
75
    }
76
77
    /**
78
     * @return Item
79
     * @throws \Opulence\Routing\Urls\UrlException
80
     */
81
    protected function createPageLayoutItem(): Item
82
    {
83
        $text = 'website:pageLayouts';
84
        $icon = 'view_quilt';
85
86
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_PAGE_LAYOUTS, [], $icon);
87
        $resource = $this->getAdminResource(Routes::ROUTE_PAGE_LAYOUTS);
88
89
        $item = new Item($button);
90
        $item->setResource($resource);
91
92
        return $item;
93
    }
94
95
    /**
96
     * @return Item
97
     * @throws \Opulence\Routing\Urls\UrlException
98
     */
99
    protected function createBlockItem(): Item
100
    {
101
        $text = 'website:blocks';
102
        $icon = 'view_module';
103
104
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_BLOCKS, [], $icon);
105
        $resource = $this->getAdminResource(Routes::ROUTE_BLOCKS);
106
107
        $item = new Item($button);
108
        $item->setResource($resource);
109
110
        return $item;
111
    }
112
113
    /**
114
     * @return Item
115
     * @throws \Opulence\Routing\Urls\UrlException
116
     */
117
    protected function createBlockLayoutItem(): Item
118
    {
119
        $text = 'website:blockLayouts';
120
        $icon = 'view_quilt';
121
122
        $button   = $this->buttonFactory->createFromName($text, Routes::ROUTE_BLOCK_LAYOUTS, [], $icon);
123
        $resource = $this->getAdminResource(Routes::ROUTE_BLOCK_LAYOUTS);
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