Passed
Push — dev6 ( 406f7e...fbbbb9 )
by Ron
16:38
created

BuildNavbar::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Actions\General;
4
5
use Illuminate\Support\Facades\Gate;
6
use Nwidart\Modules\Facades\Module;
7
8
class BuildNavbar
9
{
10
    protected $user;
11
12 23
    public function build($user)
13
    {
14 23
        $this->user = $user;
15
16 23
        $admin  = $this->getAdminNavbar();
17 23
        $navBar = $this->getPrimaryNavbar();
18
19 23
        $modules = $this->getModules();
20
21 23
        return array_merge($navBar, $admin, $modules);
22
    }
23
24
    /*
25
    *   Main navigation menu that is always included
26
    */
27 23
    protected function getPrimaryNavbar()
28
    {
29
        return [
30
            [
31 23
                'name'  => 'Dashboard',
32 23
                'route' => route('dashboard'),
33 23
                'icon'  => 'fas fa-tachometer-alt',
34
            ],
35
            [
36 23
                'name'  => 'Customers',
37 23
                'route' => route('customers.index'),
38 23
                'icon'  => 'fas fa-user-tie',
39
            ],
40
            [
41
                'name'  => 'Tech Tips',
42
                'route' => '#',
43
                'icon'  => 'fas fa-tools',
44
            ],
45
        ];
46
    }
47
48
    /*
49
    *   If the user has any admin abilities, show the admin link
50
    */
51 23
    protected function getAdminNavbar()
52
    {
53 23
        if(Gate::allows('admin-link', $this->user))
54
        {
55
            return [[
56 17
                'name'  => 'Administration',
57 17
                'route' => route('admin.index'),
58 17
                'icon'  => 'fas fa-user-shield',
59
            ]];
60
        }
61
62 6
        return [];
63
    }
64
65
    //  Get landing routes for all active modules
66 23
    protected function getModules()
67
    {
68 23
        $nav     = [];
69 23
        $modules = Module::allEnabled();
70
71 23
        foreach($modules as $module)
72
        {
73
            $name = $module->getLowerName();
74
            $navData = config($name.'.navbar');
75
76
            if($navData['enable'])
77
            {
78
                $nav[] = [
79
                    'name'  => $navData['name'],
80
                    'route' => route($navData['route']),
81
                    'icon'  => $navData['icon'],
82
                ];
83
            }
84
        }
85
86 23
        return $nav;
87
    }
88
}
89