Completed
Push — master ( 468632...220694 )
by
unknown
01:28
created

VueGuardController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Bantenprov\VueGuard\Http\Controllers;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Http\Request;
5
use Bantenprov\VueGuard\Facades\VueGuard;
6
use Bantenprov\VueGuard\Models\VueGuardModel;
7
use Bantenprov\VueWorkflow\Models\Workflow;
8
use Bantenprov\VueWorkflow\Models\Transition;
9
use App\Permission;
10
11
use Validator;
12
/**
13
 * The VueGuardController class.
14
 *
15
 * @package Bantenprov\VueGuard
16
 * @author  bantenprov <[email protected]>
17
 */
18
class VueGuardController extends Controller
19
{
20
    protected $vueGuard;
21
    protected $workflow;
22
    protected $transition;
23
    protected $permission;
24
    
25
    
26
    //[Function] __construct
27
    public function __construct(VueGuardModel $vueGuard, Workflow $workflow, Transition $transition, Permission $permission){
28
        $this->vueGuard     = $vueGuard;
29
        $this->workflow     = $workflow;
30
        $this->transition   = $transition;
31
        $this->permission   = $permission;
32
    }
33
    
34
35
    public function demo()
36
    {        
37
        return VueGuard::welcome();
38
    }
39
40
    //[Function] index
41
    public function index(Request $request){
42
        
43
        if (request()->has('sort')) {
44
            list($sortCol, $sortDir) = explode('|', request()->sort);
45
46
            $query = $this->vueGuard->orderBy($sortCol, $sortDir);
47
        } else {
48
            $query = $this->vueGuard->orderBy('id', 'asc');
49
        }
50
51
        if ($request->exists('filter')) {
52
            $query->where(function($q) use($request) {
53
                $value = "%{$request->filter}%";
54
                $q->where('label', 'like', $value)
55
                    ->orWhere('name', 'like', $value);
56
            });
57
        }
58
59
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
60
        $response = $query->paginate($perPage);
61
        
62
        foreach($response as $guard){            
63
            
64
            array_set($guard, 'workflow_label', $guard->workflow->label);
65
            array_set($guard, 'permission_name', $guard->permission->display_name);
66
            array_set($guard, 'transition_label', $guard->transition->label);
67
        }
68
69
        return response()->json($response);
70
    }
71
72
    //[Function] show
73
    public function show($id){
74
        $check = $this->vueGuard->find($id)->count();
75
        
76
        if($check > 0){
77
            $response = $this->vueGuard->findOrFail($id);
78
            $response['workflow']   = $response->workflow;
79
            $response['transition'] = $response->transition;
80
            $response['permission'] = $response->permission;
81
            $response['status'] = true;
82
        }else{
83
            $response['workflow']   = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
84
            $response['transition'] = '';
85
            $response['permission'] = '';
86
            $response['status'] = true;
87
        }
88
        
89
        return response()->json($response);
90
    }
91
92
    //[Function] create
93
    public function create(Request $request){
94
        $workflows   = $this->workflow->all();
95
        $transitions = $this->transition->all();
96
        $permissions = $this->permission->all();
97
98
        foreach($permissions as $permission){
99
            array_set($permission, 'label', $permission->display_name);
100
        }
101
102
        $response['workflows'] = $workflows;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
103
        $response['permissions'] = $permissions;
104
        $response['transitions'] = $transitions;
105
106
        return response()->json($response);
107
    }
108
    
109
110
    //[Function] store
111
    public function store(Request $request){        
112
113
        $validator = Validator::make($request->all(),[
114
            'workflow_id'   => 'required',
115
            'permission_id' => 'required',
116
            'transition_id' => 'required',
117
            'name'          => 'required',
118
            'label'         => 'required'
119
        ]);
120
121
        if($validator->fails()){
122
            $response['message']    = 'add new guard failed';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
            $response['status']     = 'false';
124
        }else{
125
            $response['message']    = 'add guard success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
126
            $response['status']     = true;
127
            
128
            $save['workflow_id']    = $request->workflow_id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
129
            $save['permission_id']  = $request->permission_id;
130
            $save['transition_id']  = $request->transition_id;
131
            $save['name']           = $this->macineName($request->name);
132
            $save['label']          = $request->label;
133
134
            $this->vueGuard->create($save);
135
        }  
136
137
        return response()->json($response);
138
    }
139
140
    //[Function] destroy
141
    public function destroy($id, Request $request){
142
        
143
        $execute = $this->vueGuard->findOrFail($id);
144
145
        $response['status']     = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
146
        $response['message']    = "success delete [ " . $execute->label . " ]";
147
        $execute->delete();
148
149
        return response()->json($response);
150
    }
151
    
152
    //[Function] getTransition
153
    public function getTransition($id){
154
        
155
        $transitions = $this->transition->where('workflow_id', $id)->get();
156
157
        return response()->json($transitions);
158
    }
159
    
160
161
    //[Function] macineName
162
    protected function macineName($val){
163
        
164
        $first = strtolower($val);
165
        $final = str_replace(' ', '-', $first);        
166
167
        return $final;
168
    }
169
    
170
    
171
    
172
    
173
174
175
176
}
177