UsersComposer::getUserPermDetails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.7
1
<?php
2
3
namespace App\Http\ViewComposers;
4
5
use App\Models\User;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\View\View;
8
9
class UsersComposer
10
{
11
    private $_user;
0 ignored issues
show
introduced by
The private property $_user is not used, and could be removed.
Loading history...
12
    private $_userRole;
0 ignored issues
show
introduced by
The private property $_userRole is not used, and could be removed.
Loading history...
13
14
    public function __construct()
15
    {
16
        $currentUser = \Auth::user();
17
        $this->user = $currentUser;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
19
        if ($currentUser) {
20
            $this->userRole = strtolower($currentUser->roles[0]->name);
0 ignored issues
show
Bug Best Practice introduced by
The property userRole does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
Accessing roles on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
21
        }
22
    }
23
24
    /**
25
     * Bind data to the view.
26
     *
27
     * @param View $view
28
     *
29
     * @return void
30
     */
31
    public function compose(View $view)
32
    {
33
        $user = $this->user;
34
        if (!$user) {
35
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
36
        }
37
38
        $userRole = $this->userRole;
39
        $totalUsers = $this->getTotalUsers($user);
40
        $userCardBg = $this->getUserBg($userRole);
41
        $userLevelData = $this->getUserLevelData($userRole);
42
        $accessLevelBgs = $this->getAccessLevelBgs();
43
        $userPermDetails = $this->getUserPermDetails();
44
45
        $data = [
46
            'totalUsers'       => $totalUsers,
47
            'userCardBg'       => $userCardBg,
48
            'levelIcon'        => $userLevelData['levelIcon'],
49
            'levelName'        => $userLevelData['levelName'],
50
            'levelBgClass'     => $userLevelData['levelBgClass'],
51
            'leveIconlBgClass' => $userLevelData['leveIconlBgClass'],
52
            'accessLevel1Bg'   => $accessLevelBgs['accessLevel1Bg'],
53
            'accessLevel2Bg'   => $accessLevelBgs['accessLevel2Bg'],
54
            'accessLevel3Bg'   => $accessLevelBgs['accessLevel3Bg'],
55
            'accessLevel4Bg'   => $accessLevelBgs['accessLevel4Bg'],
56
            'accessLevel5Bg'   => $accessLevelBgs['accessLevel5Bg'],
57
            'userPermDetails'  => $userPermDetails,
58
        ];
59
60
        $view->with($data);
61
    }
62
63
    /*
64
     * Get the Users level details
65
     * @param string $userRole
66
     * @return array
67
     */
68
    private function getUserLevelData($userRole)
69
    {
70
        if ($userRole == 'admin') {
71
            return [
72
                'levelIcon'        => 'supervisor_account',
73
                'levelName'        => 'Admin Access',
74
                'levelBgClass'     => 'mdl-color--red-200',
75
                'leveIconlBgClass' => 'mdl-color--red-500',
76
            ];
77
        }
78
79
        return [
80
            'levelIcon'        => 'perm_identity',
81
            'levelName'        => 'User Access',
82
            'levelBgClass'     => 'mdl-color--blue-200',
83
            'leveIconlBgClass' => 'mdl-color--blue-500',
84
        ];
85
    }
86
87
    /*
88
     * Get a count of the total number of users
89
     * @param collection $user
0 ignored issues
show
Bug introduced by
The type App\Http\ViewComposers\collection 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...
90
     * @return int
91
     */
92
    private function getTotalUsers($user)
93
    {
94
        if (Auth::check() && $user->hasRole('admin')) {
95
            return count($this->getAllUsers());
96
        }
97
    }
98
99
    /*
100
     * Get the Users Bage based on role
101
     * @param string $userRole
102
     * @return string
103
     */
104
    private function getUserBg($userRole)
105
    {
106
        switch ($userRole) {
107
            case 'admin':
108
                $userCardBg = 'mdl-color--primary';
109
                break;
110
111
            case 'user':
112
            default:
113
                $userCardBg = 'mdl-color--primary';
114
                break;
115
        }
116
117
        return $userCardBg;
118
    }
119
120
    /*
121
     * Get a count of the total number of users
122
     * @return collection
123
     */
124
    private function getAllUsers()
125
    {
126
        return User::all();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\User::all() returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type App\Http\ViewComposers\collection.
Loading history...
127
    }
128
129
    /*
130
     * Get user access level background colors
131
     * @param (optional) string $baseColor
132
     * @return array
133
     */
134
    public function getAccessLevelBgs($baseColor = null)
135
    {
136
        $baseColor = $baseColor ?: 'green';
137
138
        return [
139
            'accessLevel5Bg' => 'mdl-color--'.$baseColor.'-900',
140
            'accessLevel4Bg' => 'mdl-color--'.$baseColor.'-700',
141
            'accessLevel3Bg' => 'mdl-color--'.$baseColor.'-600',
142
            'accessLevel2Bg' => 'mdl-color--'.$baseColor.'-400',
143
            'accessLevel1Bg' => 'mdl-color--'.$baseColor.'-200',
144
        ];
145
    }
146
147
    /*
148
     * Get user permissions details
149
     * @return array
150
     */
151
    public function getUserPermDetails()
152
    {
153
        return [
154
            'view'    => [
155
                'icon'   => 'visibility',
156
                'bg'     => 'mdl-color--blue-400',
157
                'iconBg' => 'mdl-color--blue-700',
158
            ],
159
            'create'  => [
160
                'icon'   => 'note_add',
161
                'bg'     => 'mdl-color--green-400',
162
                'iconBg' => 'mdl-color--green-800',
163
            ],
164
            'edit'    => [
165
                'icon'   => 'build',
166
                'bg'     => 'mdl-color--yellow-700',
167
                'iconBg' => 'mdl-color--yellow-900',
168
            ],
169
            'delete'  => [
170
                'icon'   => 'delete_forever',
171
                'bg'     => 'mdl-color--red-300',
172
                'iconBg' => 'mdl-color--red-700',
173
            ],
174
        ];
175
    }
176
}
177