AdminBuilder::setPrimeNav()   A
last analyzed

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