Completed
Push — master ( 170523...1fc389 )
by
unknown
03:03 queued 01:32
created

VueGuardController::update()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 2
dl 28
loc 28
rs 8.8571
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
21
    protected $vueGuard;
22
    protected $workflow;
23
    protected $transition;
24
    protected $permission;
25
26
27
    /**
28
     * VueGuardController constructor.
29
     * @param VueGuardModel $vueGuard
30
     * @param Workflow $workflow
31
     * @param Transition $transition
32
     * @param Permission $permission
33
     */
34
    public function __construct(VueGuardModel $vueGuard, Workflow $workflow, Transition $transition, Permission $permission){
35
        $this->vueGuard     = $vueGuard;
36
        $this->workflow     = $workflow;
37
        $this->transition   = $transition;
38
        $this->permission   = $permission;
39
    }
40
41
42
    public function demo()
43
    {
44
        return VueGuard::welcome();
45
    }
46
47
    /**
48
     * @param Request $request
49
     * @return \Illuminate\Http\JsonResponse
50
     */
51
    public function index(Request $request){
52
53
        if (request()->has('sort')) {
54
            list($sortCol, $sortDir) = explode('|', request()->sort);
55
56
            $query = $this->vueGuard->orderBy($sortCol, $sortDir);
57
        } else {
58
            $query = $this->vueGuard->orderBy('id', 'asc');
59
        }
60
61
        if ($request->exists('filter')) {
62
            $query->where(function($q) use($request) {
63
                $value = "%{$request->filter}%";
64
                $q->where('label', 'like', $value)
65
                    ->orWhere('name', 'like', $value);
66
            });
67
        }
68
69
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
70
        $response = $query->paginate($perPage);
71
72
        foreach($response as $guard){
73
74
            array_set($guard, 'workflow_label', $guard->workflow->label);
75
            array_set($guard, 'permission_name', $guard->permission->display_name);
76
            array_set($guard, 'transition_label', $guard->transition->label);
77
        }
78
79
        return response()->json($response);
80
81
    }
82
83
    /**
84
     * @param $id
85
     * @return \Illuminate\Http\JsonResponse
86
     */
87
    public function show($id){
88
        $check = $this->vueGuard->find($id)->count();
89
90
        if($check > 0){
91
            $response = $this->vueGuard->findOrFail($id);
92
            $response['workflow']   = $response->workflow;
93
            $response['transition'] = $response->transition;
94
            $response['permission'] = $response->permission;
95
            $response['status'] = true;
96 View Code Duplication
        }else{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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['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...
98
            $response['transition'] = '';
99
            $response['permission'] = '';
100
            $response['status'] = true;
101
        }
102
103
        return response()->json($response);
104
    }
105
106
    /**
107
     * @param $id
108
     * @return \Illuminate\Http\JsonResponse
109
     */
110
    public function edit($id){
111
        $check = $this->vueGuard->find($id)->count();
112
113
        if($check > 0){
114
            $response = $this->vueGuard->findOrFail($id);
115
            $response['workflow']   = $response->workflow;
116
            $response['transition'] = $response->transition;
117
118
            array_add($response->permission, 'label', $response->permission->display_name);
119
            $response['permission'] = $response->permission;
120
121
            $response['status'] = true;
122 View Code Duplication
        }else{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123
            $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...
124
            $response['transition'] = '';
125
            $response['permission'] = '';
126
            $response['status'] = true;
127
        }
128
129
        return response()->json($response);
130
    }
131
132
    /**
133
     * @param Request $request
134
     * @return \Illuminate\Http\JsonResponse
135
     */
136
    public function create(Request $request){
137
        $workflows   = $this->workflow->all();
138
        $transitions = $this->transition->all();
139
        $permissions = $this->permission->all();
140
141
        foreach($permissions as $permission){
142
            array_set($permission, 'label', $permission->display_name);
143
        }
144
145
        $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...
146
        $response['permissions'] = $permissions;
147
        $response['transitions'] = $transitions;
148
149
        return response()->json($response);
150
    }
151
152
153
    /**
154
     * @param Request $request
155
     * @return \Illuminate\Http\JsonResponse
156
     */
157 View Code Duplication
    public function store(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...
158
159
        $validator = Validator::make($request->all(),[
160
            'workflow_id'   => 'required',
161
            'permission_id' => 'required',
162
            'transition_id' => 'required',
163
            'name'          => 'required',
164
            'label'         => 'required'
165
        ]);
166
167
        if($validator->fails()){
168
            $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...
169
            $response['status']     = 'false';
170
        }else{
171
            $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...
172
            $response['status']     = true;
173
174
            $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...
175
            $save['permission_id']  = $request->permission_id;
176
            $save['transition_id']  = $request->transition_id;
177
            $save['name']           = $this->macineName($request->name);
178
            $save['label']          = $request->label;
179
180
            $this->vueGuard->create($save);
181
        }
182
183
        return response()->json($response);
184
    }
185
186
    /**
187
     * @param Request $request
188
     * @param $id
189
     * @return \Illuminate\Http\JsonResponse
190
     */
191 View Code Duplication
    public function update(Request $request, $id){
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...
192
193
        $validator = Validator::make($request->all(),[
194
            'workflow_id'   => 'required',
195
            'permission_id' => 'required',
196
            'transition_id' => 'required',
197
            'name'          => 'required',
198
            'label'         => 'required'
199
        ]);
200
201
        if($validator->fails()){
202
            $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...
203
            $response['status']     = 'false';
204
        }else{
205
            $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...
206
            $response['status']     = true;
207
208
            $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...
209
            $save['permission_id']  = $request->permission_id;
210
            $save['transition_id']  = $request->transition_id;
211
            $save['name']           = $this->macineName($request->name);
212
            $save['label']          = $request->label;
213
214
            $this->vueGuard->findOrFail($id)->update($save);
215
        }
216
217
        return response()->json($response);
218
    }
219
220
    /**
221
     * @param $id
222
     * @param Request $request
223
     * @return \Illuminate\Http\JsonResponse
224
     * @throws \Exception
225
     */
226
    public function destroy($id, Request $request){
227
228
        $execute = $this->vueGuard->findOrFail($id);
229
230
        $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...
231
        $response['message']    = "success delete [ " . $execute->label . " ]";
232
        $execute->delete();
233
234
        return response()->json($response);
235
    }
236
237
    /**
238
     * @param $id
239
     * @return \Illuminate\Http\JsonResponse
240
     */
241
    public function getTransition($id){
242
243
        $transitions = $this->transition->where('workflow_id', $id)->get();
244
245
        return response()->json($transitions);
246
    }
247
248
249
    /**
250
     * @param $val
251
     * @return mixed
252
     */
253
    protected function macineName($val){
254
255
        $first = strtolower($val);
256
        $final = str_replace(' ', '-', $first);
257
258
        return $final;
259
    }
260
261
}
262