Navbar::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\PropellerAdmin\Decorator\Navigation;
6
7
use AbterPhp\Admin\Constant\Route;
8
use AbterPhp\Framework\Constant\Html5;
9
use AbterPhp\Framework\Decorator\Decorator;
10
use AbterPhp\Framework\Decorator\Rule;
11
use AbterPhp\Framework\Html\Component;
12
use AbterPhp\Framework\Html\Component\Button;
13
use AbterPhp\Framework\Navigation\Navigation;
14
use AbterPhp\PropellerAdmin\Navigation\Header;
15
use Opulence\Routing\Urls\UrlGenerator;
16
17
class Navbar extends Decorator
18
{
19
    const NAVBAR_CONTAINER_CLASS = 'navbar navbar-inverse navbar-fixed-top pmd-navbar pmd-z-depth';
20
    const NAVBAR_CLASS           = 'container-fluid';
21
22
    const HEADER_WEIGHT = -100000;
23
24
    /** @var UrlGenerator */
25
    protected $urlGenerator;
26
27
    /**
28
     * Primary constructor.
29
     *
30
     * @param UrlGenerator $urlGenerator
31
     */
32
    public function __construct(UrlGenerator $urlGenerator)
33
    {
34
        $this->urlGenerator = $urlGenerator;
35
    }
36
37
38
    /**
39
     * @return $this
40
     */
41
    public function init(): Decorator
42
    {
43
        $this->rules[] = new Rule(
44
            [Navigation::INTENT_NAVBAR],
45
            Navigation::class,
46
            [static::NAVBAR_CLASS],
47
            [],
48
            [$this, 'decorateNavigation']
49
        );
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param Navigation $navigation
56
     */
57
    public function decorateNavigation(Navigation $navigation)
58
    {
59
        $navigation->setTag(Html5::TAG_DIV);
60
61
        $wrapperAttribs = [Html5::ATTR_CLASS => static::NAVBAR_CONTAINER_CLASS];
62
        $navigation->setWrapper(new Component('', [], $wrapperAttribs, Html5::TAG_NAV));
63
64
        // Add header
65
        $dashboardUrl = $this->urlGenerator->createFromName(Route::DASHBOARD_VIEW);
66
        $header       = new Header(new Button(null, [], [Html5::ATTR_HREF => $dashboardUrl], Html5::TAG_A));
67
        $navigation->addItem($header, static::HEADER_WEIGHT);
68
    }
69
}
70