Completed
Push — master ( c53116...d0984d )
by Jeremy
07:52
created

getSortedRolesWithPermissionsAndUsers()   B

Complexity

Conditions 9
Paths 21

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 21
nop 2
dl 0
loc 35
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace jeremykenedy\LaravelRoles\Traits;
4
5
use Illuminate\Support\Facades\DB;
6
7
trait RolesGUITraits
8
{
9
    /**
10
     * Retrieves permission roles.
11
     *
12
     * @param Permission $permission                The permission
13
     * @param Collection $permissionsAndRolesPivot  The permissions and roles pivot
14
     * @param Collection $sortedRolesWithUsers      The sorted roles with users
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelRoles\Traits\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...
15
     *
16
     * @return Collection of permission roles
17
     */
18
    private function retrievePermissionRoles($permission, $permissionsAndRolesPivot, $sortedRolesWithUsers)
19
    {
20
        $roles = [];
21
        foreach ($permissionsAndRolesPivot as $permissionAndRoleKey => $permissionAndRoleValue) {
22
            if ($permission->id === $permissionAndRoleValue->permission_id) {
23
                foreach ($sortedRolesWithUsers as $sortedRolesWithUsersItemKey => $sortedRolesWithUsersItemValue) {
24
                    if ($sortedRolesWithUsersItemValue['role']->id === $permissionAndRoleValue->role_id) {
25
                        $roles[] = $sortedRolesWithUsersItemValue['role'];
26
                    }
27
                }
28
29
            }
30
        }
31
        return collect($roles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($roles) returns the type Illuminate\Support\Collection which is incompatible with the documented return type jeremykenedy\LaravelRoles\Traits\Collection.
Loading history...
32
    }
33
34
    /**
35
     * Retrieves permission users.
36
     *
37
     * @param Permission $permission                The permission
38
     * @param Collection $permissionsAndRolesPivot  The permissions and roles pivot
39
     * @param Collection $sortedRolesWithUsers      The sorted roles with users
40
     *
41
     * @return Collection of Permission Users
42
     */
43
    private function retrievePermissionUsers($permission, $permissionsAndRolesPivot, $sortedRolesWithUsers)
44
    {
45
        $users = [];
46
        foreach ($permissionsAndRolesPivot as $permissionsAndRolesPivotItemKey => $permissionsAndRolesPivotItemValue) {
47
            if ($permission->id === $permissionsAndRolesPivotItemValue->permission_id) {
48
                foreach ($sortedRolesWithUsers as $sortedRolesWithUsersItemKey => $sortedRolesWithUsersItemValue) {
49
                    if ($permissionsAndRolesPivotItemValue->role_id === $sortedRolesWithUsersItemValue['role']->id) {
50
                        foreach ($sortedRolesWithUsersItemValue['users'] as $sortedRolesWithUsersItemValueUser) {
51
                            $users[] = $sortedRolesWithUsersItemValueUser;
52
                        }
53
                    }
54
                }
55
            }
56
        }
57
        return collect($users);
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($users) returns the type Illuminate\Support\Collection which is incompatible with the documented return type jeremykenedy\LaravelRoles\Traits\Collection.
Loading history...
58
    }
59
60
    /**
61
     * Gets the roles.
62
     *
63
     * @return collection The roles.
0 ignored issues
show
Bug introduced by
The type jeremykenedy\LaravelRoles\Traits\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...
64
     */
65
    public function getRoles()
66
    {
67
        return config('roles.models.role')::all();
68
    }
69
70
    /**
71
     * Gets the permissions.
72
     *
73
     * @return collection The permissions.
74
     */
75
    public function getPermissions()
76
    {
77
        return config('roles.models.permission')::all();
78
    }
79
80
    /**
81
     * Gets the users.
82
     *
83
     * @return collection The users.
84
     */
85
    public function getUsers()
86
    {
87
        return config('roles.models.defaultUser')::all();
88
    }
89
90
    /**
91
     * Gets the deleted roles.
92
     *
93
     * @return collection The deleted roles.
94
     */
95
    public function getDeletedRoles()
96
    {
97
        return config('roles.models.role')::onlyTrashed();
98
    }
99
100
    /**
101
     * Gets the sorted users with roles.
102
     *
103
     * @param collection $roles  The roles
104
     * @param collection $users  The users
105
     *
106
     * @return collection The sorted users with roles.
107
     */
108
    public function getSortedUsersWithRoles($roles, $users)
109
    {
110
        $sortedUsersWithRoles = [];
111
112
        foreach ($roles as $rolekey => $roleValue) {
113
            $sortedUsersWithRoles[] = [
114
                'role'   => $roleValue,
115
                'users'  => [],
116
            ];
117
            foreach ($users as $user) {
118
                foreach ($user->roles as $userRole) {
119
                    if ($userRole->id === $sortedUsersWithRoles[$rolekey]['role']['id']) {
120
                        $sortedUsersWithRoles[$rolekey]['users'][] = $user;
121
                    }
122
                }
123
            }
124
        }
125
        return collect($sortedUsersWithRoles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($sortedUsersWithRoles) returns the type Illuminate\Support\Collection which is incompatible with the documented return type jeremykenedy\LaravelRoles\Traits\collection.
Loading history...
126
    }
127
128
    /**
129
     * Gets the permissions with roles.
130
     *
131
     * @param collection $role   The role
132
     *
133
     * @return collection The permissions with roles.
134
     */
135
    public function getPermissionsWithRoles($role = null)
136
    {
137
        $query = DB::table(config('roles.permissionsRoleTable'));
138
        if ($role) {
139
            $query->where('role_id', '=', $role->id);
140
        }
141
        return $query->get();
142
    }
143
144
    /**
145
     * Gets the sorted roles with permissions.
146
     *
147
     * @param collection $sortedRolesWithUsers  The sorted roles with users
148
     * @param collection $permissions           The permissions
149
     *
150
     * @return collection The sorted roles with permissions.
151
     */
152
    public function getSortedRolesWithPermissionsAndUsers($sortedRolesWithUsers, $permissions)
153
    {
154
        $sortedRolesWithPermissions = [];
155
        $permissionsAndRoles        = $this->getPermissionsWithRoles();
156
157
        foreach ($sortedRolesWithUsers as $sortedRolekey => $sortedRoleValue) {
158
            $role = $sortedRoleValue['role'];
159
            $users = $sortedRoleValue['users'];
160
            $sortedRolesWithPermissions[] = [
161
                'role'          => $role,
162
                'permissions'   => collect([]),
163
                'users'         => collect([]),
164
            ];
165
166
            // Add Permission with Role
167
            foreach ($permissionsAndRoles as $permissionAndRole) {
168
                if ($permissionAndRole->role_id == $role->id) {
169
                    foreach ($permissions as $permissionKey => $permissionValue) {
170
                        if ($permissionValue->id == $permissionAndRole->permission_id) {
171
                            $sortedRolesWithPermissions[$sortedRolekey]['permissions'][] = $permissionValue;
172
                        }
173
                    }
174
                }
175
            }
176
177
            // Add Users with Role
178
            foreach ($users as $user) {
179
                foreach ($user->roles as $userRole) {
180
                    if ($userRole->id === $sortedRolesWithPermissions[$sortedRolekey]['role']['id']) {
181
                        $sortedRolesWithPermissions[$sortedRolekey]['users'][] = $user;
182
                    }
183
                }
184
            }
185
        }
186
        return collect($sortedRolesWithPermissions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($sortedRolesWithPermissions) returns the type Illuminate\Support\Collection which is incompatible with the documented return type jeremykenedy\LaravelRoles\Traits\collection.
Loading history...
187
    }
188
189
    /**
190
     * Gets the sorted permissons with roles and users.
191
     *
192
     * @param collection $sortedRolesWithUsers  The sorted roles with users
193
     * @param collection $permissions           The permissions
194
     *
195
     * @return collection The sorted permissons with roles and users.
196
     */
197
    public function getSortedPermissonsWithRolesAndUsers($sortedRolesWithUsers, $permissions)
198
    {
199
        $sortedPermissionsWithRoles = [];
200
        $permissionsAndRolesPivot   = $this->getPermissionsWithRoles();
201
202
        foreach ($permissions as $permissionKey => $permissionValue) {
203
            $sortedPermissionsWithRoles[] = [
204
                'permission'    => $permissionValue,
205
                'roles'         => $this->retrievePermissionRoles($permissionValue, $permissionsAndRolesPivot, $sortedRolesWithUsers),
206
                'users'         => $this->retrievePermissionUsers($permissionValue, $permissionsAndRolesPivot, $sortedRolesWithUsers),
207
            ];
208
        }
209
210
        return collect($sortedPermissionsWithRoles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect($sortedPermissionsWithRoles) returns the type Illuminate\Support\Collection which is incompatible with the documented return type jeremykenedy\LaravelRoles\Traits\collection.
Loading history...
211
    }
212
}
213