VueTrustController::permissionEdit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Bantenprov\VueTrust\Http\Controllers;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Http\Request;
5
use Bantenprov\VueTrust\Facades\VueTrust;
6
use Bantenprov\VueTrust\Models\VueTrustModel;
7
use App\Role;
8
use App\Permission;
9
10
use Validator;
11
12
/**
13
 * The VueTrustController class.
14
 *
15
 * @package Bantenprov\VueTrust
16
 * @author  bantenprov <[email protected]>
17
 */
18
class VueTrustController extends Controller
19
{
20
21
    protected $role;
22
    protected $permission;
23
    
24
    
25
26
    //[Function] __construct
27
    public function __construct(Role $role, Permission $permission){
28
        
29
        $this->role         = $role;
30
        $this->permission   = $permission;
31
32
    }    
33
34
    public function demo()
35
    {
36
        return VueTrust::welcome();
37
    }
38
39
    //[Function] roleIndex
40 View Code Duplication
    public function roleIndex(Request $request){        
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
42
        $response = array();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
44
        if (request()->has('sort')) {
45
            list($sortCol, $sortDir) = explode('|', request()->sort);
46
47
            $query = $this->role->orderBy($sortCol, $sortDir);
48
        } else {
49
            $query = $this->role->orderBy('id', 'asc');
50
        }
51
52
        if ($request->exists('filter')) {
53
            $query->where(function($q) use($request) {
54
                $value = "%{$request->filter}%";
55
                $q->where('name', 'like', $value)
56
                    ->orWhere('description', 'like', $value);
57
            });
58
        }
59
60
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
61
        $response = $query->paginate($perPage);                
62
63
        return response()->json($response);
64
    }
65
66
    //[Function] roleStore
67 View Code Duplication
    public function roleStore(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
        
69
        $response = array();
70
        $save = array();
71
72
        $validator = Validator::make($request->all(), [
73
            'name'          => 'required',
74
            'display_name'  => 'required',
75
            'description'   => 'required'
76
        ]);
77
78
        if($validator->fails()){
79
            $response['message']    = "failed add role";
80
            $response['status']     = false;
81
        }else{
82
            $response['message']    = "success add new role";
83
            $response['status']     = true;
84
85
            $save['name']           = $this->macineName($request->name);
86
            $save['display_name']   = title_case($request->display_name);
87
            $save['description']    = $request->description;
88
89
            $this->role->create($save);
90
        }
91
        
92
        return response()->json($response);
93
    }
94
95
    //[Function] roleShow
96 View Code Duplication
    public function roleShow($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
        $response = array();
98
99
        $role = $this->role->findOrFail($id);
100
101
        $response['status'] = true;
102
        $response['role']   = $role;
103
        
104
        return response()->json($response);
105
    }
106
107
    //[Function] roleEdit
108 View Code Duplication
    public function roleEdit($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
        $response = array();
110
111
        $role = $this->role->findOrFail($id);
112
113
        $response['status'] = true;
114
        $response['role']   = $role;
115
        
116
        return response()->json($response);
117
    }
118
119
    //[Function] roleUpdate
120 View Code Duplication
    public function roleUpdate($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
        $response = array();
122
        $save = array();
123
        $validator = Validator::make($request->all(), [
124
            'name'          => 'required',
125
            'display_name'  => 'required',
126
            'description'   => 'required'
127
        ]);
128
129
        if($validator->fails()){
130
            $response['message']    = "failed update role";
131
            $response['status']     = false;
132
        }else{
133
134
            $save['name']           = $this->macineName($request->name);
135
            $save['display_name']   = title_case($request->display_name);
136
            $save['description']    = $request->description; 
137
138
            $this->role->findOrFail($id)->update($save);
139
140
            $response['message']    = "success update role";
141
            $response['status']     = true;                       
142
143
        }
144
        
145
        return response()->json($response);
146
    }   
147
    
148
    //[Function] roleDestroy
149
    public function roleDestroy($id, Request $request){
150
        $response = array();
151
152
        $role = $this->role->findOrFail($id);
153
154
        $response['message']    = "success delete role [" . $role->name . "]";
155
        $response['status']     = true;
156
157
        $role->delete();
158
        
159
        return response()->json($response);
160
    }
161
162
    //[Function] permissionIndex
163 View Code Duplication
    public function permissionIndex(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
        $response = array();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
165
166
        if (request()->has('sort')) {
167
            list($sortCol, $sortDir) = explode('|', request()->sort);
168
169
            $query = $this->permission->orderBy($sortCol, $sortDir);
170
        } else {
171
            $query = $this->permission->orderBy('id', 'asc');
172
        }
173
174
        if ($request->exists('filter')) {
175
            $query->where(function($q) use($request) {
176
                $value = "%{$request->filter}%";
177
                $q->where('name', 'like', $value)
178
                    ->orWhere('description', 'like', $value);
179
            });
180
        }
181
182
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
183
        $response = $query->paginate($perPage);                
184
185
        return response()->json($response);
186
    }
187
188
    //[Function] permissionStore
189 View Code Duplication
    public function permissionStore(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
        $response = array();
191
        $save = array();
192
193
        $validator = Validator::make($request->all(), [
194
            'name'          => 'required',
195
            'display_name'  => 'required',
196
            'description'   => 'required'
197
        ]);
198
199
        if($validator->fails()){
200
            $response['message']    = "failed add permission";
201
            $response['status']     = false;
202
        }else{
203
            $response['message']    = "success add new permission";
204
            $response['status']     = true;
205
206
            $save['name']           = $this->macineName($request->name);
207
            $save['display_name']   = title_case($request->display_name);
208
            $save['description']    = $request->description;
209
210
            $this->permission->create($save);
211
        }
212
        
213
        return response()->json($response);
214
    }
215
216
    //[Function] permissionShow
217 View Code Duplication
    public function permissionShow($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
        $response = array();
219
220
        $role = $this->permission->findOrFail($id);
221
222
        $response['status']       = true;
223
        $response['permission']   = $role;
224
        
225
        return response()->json($response);
226
    }
227
228
    //[Function] permissionEdit
229 View Code Duplication
    public function permissionEdit($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
        $response = array();
231
232
        $role = $this->permission->findOrFail($id);
233
234
        $response['status'] = true;
235
        $response['permission']   = $role;
236
        
237
        return response()->json($response);
238
    }
239
240
    //[Function] setPermission
241
    public function setPermission($id, Request $request){
242
        $response = array();
243
244
        $role = $this->role->findOrFail($id);
245
        $current_permissions = $role->permissions()->get();
246
        $permissions = $this->permission->all();
247
        
248
        foreach ($permissions as $permission) {
249
250
            array_set($permission, 'disable', false);
251
252
            foreach($current_permissions as $current_permision){
253
                if($permission->id == $current_permision->id){
254
                    
255
                    if($permission->id == $current_permision->id){
256
                        array_set($permission, 'disable', true);
257
                    }
258
                }else{
259
                    if($permission->id == $current_permision->id){
260
                        array_set($permission, 'disable', false);
261
                    }
262
                }
263
            }
264
        }
265
        
266
267
        $response['status']                 = true;
268
        $response['current_permissions']    = $current_permissions;
269
        $response['permissions']            = $permissions; 
270
        
271
        return response()->json($response);
272
    }
273
274
    //[Function] setPermissionStore
275
    public function setPermissionStore($id, Request $request){
276
        $response = array();
277
278
        $validator = Validator::make($request->all(), [
279
            'permission_id'          => 'required',
280
        ]);
281
282
        if($validator->fails()){
283
            $response['message']    = "failed add permission";
284
            $response['status']     = false;
285
        }else{
286
            $response['message']    = "success add new permission";
287
            $response['status']     = true;   
288
289
            $current_permissions = $this->role->findOrFail($id)->permissions()->get();   
290
            $save = $this->removeSamePermission($request->permission_id, $current_permissions);
291
            $this->role->findOrFail($id)->attachPermissions($save);
292
        }
293
        
294
        return response()->json($response);
295
    }
296
297
    //[Function] removePermissionFromRole
298
    public function removePermissionFromRole($id, Request $request){
299
        $response = array();
300
301
        $validator = Validator::make($request->all(), [
302
            'permission_id'          => 'required',
303
        ]);
304
305
        if($validator->fails()){
306
            $response['message']    = "failed remove permission";
307
            $response['status']     = false;
308
        }else{
309
            $response['message']    = "success remove new permission";
310
            $response['status']     = true;   
311
             
312
            $data = $request->permission_id;
313
            
314
            $this->role->findOrFail($id)->detachPermission($data);
315
        }
316
        
317
        return response()->json($response);
318
    }
319
320
    //[Function] removeSamePermission
321
    protected function removeSamePermission($array, $search){                
322
        
323
        foreach($search as $value1){   
324
            print_r("[".$value1->id."]");   
325
                
326
            $key = array_search($value1->id, $array);
327
            if(!empty($key)){
328
                unset($array[$key]);
329
            }                        
330
        }
331
        return $array;        
332
    }
333
    
334
    
335
336
    //[Function] macineName
337
    protected function macineName($text){
338
        
339
        $final = strtolower(str_replace(' ', '_', $text));
340
341
        return $final;
342
343
    }
344
    
345
    
346
}
347