Passed
Push — master ( 769df9...bd651d )
by Peter
02:25
created

AdminBuilder::setPrimeNav()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Views\Builders;
6
7
use AbterPhp\Admin\Constant\Event;
8
use AbterPhp\Admin\Events\AdminReady;
9
use AbterPhp\Framework\Assets\AssetManager;
10
use AbterPhp\Framework\Constant\Env;
11
use AbterPhp\Framework\Constant\Session;
12
use AbterPhp\Framework\Constant\View;
13
use AbterPhp\Framework\Navigation\Navigation;
14
use Opulence\Environments\Environment;
15
use Opulence\Events\Dispatchers\IEventDispatcher;
16
use Opulence\Sessions\ISession;
17
use Opulence\Views\Factories\IViewBuilder;
18
use Opulence\Views\IView;
19
20
/**
21
 * Defines a view builder for the admin pages
22
 */
23
class AdminBuilder implements IViewBuilder
24
{
25
    /** @var ISession */
26
    protected $session;
27
28
    /** @var AssetManager */
29
    protected $assetManager;
30
31
    /** @var IEventDispatcher */
32
    protected $eventDispatcher;
33
34
    /** @var Navigation|null */
35
    protected $primaryNav;
36
37
    /** @var Navigation|null */
38
    protected $navbar;
39
40
    /**
41
     * AdminBuilder constructor.
42
     *
43
     * @param ISession         $session
44
     * @param AssetManager     $assetManager
45
     * @param IEventDispatcher $eventDispatcher
46
     * @param Navigation|null  $primaryNav
47
     * @param Navigation|null  $navbar
48
     */
49
    public function __construct(
50
        ISession $session,
51
        AssetManager $assetManager,
52
        IEventDispatcher $eventDispatcher,
53
        ?Navigation $primaryNav,
54
        ?Navigation $navbar
55
    ) {
56
        $this->session         = $session;
57
        $this->assetManager    = $assetManager;
58
        $this->eventDispatcher = $eventDispatcher;
59
        $this->primaryNav      = $primaryNav;
60
        $this->navbar          = $navbar;
61
    }
62
63
    /**
64
     * @param Navigation|null $navigation
65
     *
66
     * @return AdminBuilder
67
     */
68
    public function setPrimeNav(?Navigation $navigation): self
69
    {
70
        $this->primaryNav = $navigation;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param Navigation|null $navigation
77
     *
78
     * @return AdminBuilder
79
     */
80
    public function setNavbar(?Navigation $navigation): self
81
    {
82
        $this->navbar = $navigation;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function build(IView $view): IView
91
    {
92
        $this->assetManager->addJs(View::ASSET_HEADER, '/admin-assets/vendor/jquery/jquery.min.js');
93
        $this->assetManager->addJs(View::ASSET_HEADER, '/admin-assets/js/navigation.js');
94
95
        $view->setVar('env', Environment::getVar(Env::ENV_NAME));
96
        $view->setVar('title', 'Admin');
97
        $view->setVar('username', $this->session->get(Session::USERNAME));
98
        $view->setVar('primaryNav', $this->primaryNav);
99
        $view->setVar('navbar', $this->navbar);
100
101
        $view->setVar('preHeader', '');
102
        $view->setVar('header', '');
103
        $view->setVar('postHeader', '');
104
105
        $view->setVar('preFooter', '');
106
        $view->setVar('footer', '');
107
        $view->setVar('postFooter', '');
108
109
        $this->eventDispatcher->dispatch(Event::ADMIN_READY, new AdminReady($view));
110
111
        $this->assetManager->addJs(View::ASSET_FOOTER, '/admin-assets/js/alerts.js');
112
113
        return $view;
114
    }
115
}
116