1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @Author: bantenprov |
5
|
|
|
* @Date: 2017-11-28 00:12:29 |
6
|
|
|
* @Last Modified by: bantenprov |
7
|
|
|
* @Last Modified time: 2017-11-28 09:47:54 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Http\Controllers; |
11
|
|
|
|
12
|
|
|
use App\Http\Controllers\Controller; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use App\ApiKeys; |
15
|
|
|
use Bantenprov\Workflow\Models\WorkflowModel; |
16
|
|
|
use Bantenprov\Workflow\Models\WorkflowState; |
17
|
|
|
use Bantenprov\Workflow\Models\WorkflowTransition; |
18
|
|
|
use Bantenprov\Workflow\Models\History; |
19
|
|
|
use That0n3guy\Transliteration; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
22
|
|
|
use GuzzleHttp\Client; |
23
|
|
|
use GuzzleHttp\Pool; |
24
|
|
|
use GuzzleHttp\Psr7; |
25
|
|
|
|
26
|
|
|
use Validator, Image, Session, File, Response, Redirect, Exception; |
27
|
|
|
use Auth; |
28
|
|
|
|
29
|
|
|
class ApiManagerController extends Controller |
30
|
|
|
{ |
31
|
|
|
public function index(Request $request) |
32
|
|
|
{ |
33
|
|
|
try { |
34
|
|
|
if($request->get('search') != ''){ |
35
|
|
|
$data['data'] = ApiKeys::with('getUserName')->where('client', 'like', '%'.$request->get('search').'%') |
|
|
|
|
36
|
|
|
->orderBy('id', 'desc') |
37
|
|
|
->paginate(env('PAGINATE', 10)); |
38
|
|
|
} else{ |
39
|
|
|
$data['data'] = ApiKeys::with('getUserName')->orderBy('id', 'desc')->paginate(env('PAGINATE', 10)); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} catch (Exception $e) { |
42
|
|
|
$data['data'] = []; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
return view('api_manager.index', $data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function create() |
48
|
|
|
{ |
49
|
|
|
return view('api_manager.create'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function store(Request $request) |
53
|
|
|
{ |
54
|
|
|
$validator = Validator::make($request->all(), [ |
55
|
|
|
'client' => 'required|unique:api_keys,client', |
56
|
|
|
'description' => 'required', |
57
|
|
|
]); |
58
|
|
|
if($validator->fails()) |
59
|
|
|
{ |
60
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
61
|
|
|
return redirect()->back() |
62
|
|
|
->withErrors($validator) |
63
|
|
|
->withInput(); |
64
|
|
|
} |
65
|
|
|
if(Auth::guest()){ $current_user = 1; } |
66
|
|
|
else{ $current_user = Auth::user()->id; } |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$token = $this->token(); |
70
|
|
|
$api = New ApiKeys; |
71
|
|
|
$api->client = str_replace(array('https://', 'http://'), array('',''),$request->client); |
72
|
|
|
$api->api_key = $token; |
73
|
|
|
$api->description = $request->description; |
74
|
|
|
$api->user_id = $current_user; |
75
|
|
|
|
76
|
|
|
//create history default |
77
|
|
|
$model = "ApiKeys"; |
78
|
|
|
$fromState = "propose"; |
79
|
|
|
$toState = "propose"; |
80
|
|
|
$workflow = $this->getWorkflow($model); |
81
|
|
|
$statesFrom = $this->getState($fromState); |
82
|
|
|
$statesTo = $this->getState($toState); |
83
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
84
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
85
|
|
|
return Redirect::to('api-manager'); |
86
|
|
|
}elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
87
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
88
|
|
|
return Redirect::to('api-manager'); |
89
|
|
|
}else{ |
90
|
|
|
$api->save(); |
91
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
92
|
|
|
|
93
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly'); |
94
|
|
|
return Redirect::to('api-manager'); |
95
|
|
|
} |
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
98
|
|
|
return Redirect::to('api-manager'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function show($id) |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$transition = WorkflowTransition::all(); |
106
|
|
|
$data['transition'] = $transition; |
|
|
|
|
107
|
|
|
$history = History::with('getApiKeys') |
108
|
|
|
->with('getWorkflow') |
109
|
|
|
->with('getStateFrom') |
110
|
|
|
->with('getStateTo') |
111
|
|
|
->with('getUserName') |
112
|
|
|
->where('content_id', $id) |
113
|
|
|
->get(); |
114
|
|
|
|
115
|
|
|
$data['history'] = $history; |
116
|
|
|
$data['id'] = $id; |
117
|
|
|
foreach ($history as $value) { |
118
|
|
|
$workstateto = $value->getStateTo->label; |
119
|
|
|
} |
120
|
|
|
$data['workflowstateto'] = $workstateto; |
|
|
|
|
121
|
|
|
$data['data'] = ApiKeys::where('id', $id)->first(); |
122
|
|
|
return view('api_manager.show', $data); |
123
|
|
|
} catch (Exception $e) { |
124
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
125
|
|
|
return Redirect::to('api-manager'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function edit(Request $request, $id) |
130
|
|
|
{ |
131
|
|
|
$data['data'] = ApiKeys::findOrFail($id); |
|
|
|
|
132
|
|
|
return view('api_manager.edit', $data); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function update(Request $request, $id) |
136
|
|
|
{ |
137
|
|
|
$validator = Validator::make($request->all(), [ |
138
|
|
|
'client' => 'required|unique:api_keys,client,'.$id, |
139
|
|
|
'description' => 'required', |
140
|
|
|
]); |
141
|
|
|
if($validator->fails()) |
142
|
|
|
{ |
143
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
144
|
|
|
return redirect()->back() |
145
|
|
|
->withErrors($validator) |
146
|
|
|
->withInput(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
try { |
150
|
|
|
$api = ApiKeys::findOrFail($id); |
151
|
|
|
$api->client = str_replace(array('https://', 'http://'),array('',''),$request->client); |
152
|
|
|
$api->description = $request->description; |
153
|
|
|
$api->save(); |
154
|
|
|
Session::flash('message', 'Api Keys Data Update Successfuly'); |
155
|
|
|
return Redirect::to('api-manager'); |
156
|
|
|
} catch (Exception $e) { |
157
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
158
|
|
|
return Redirect::to('api-manager'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function token() |
163
|
|
|
{ |
164
|
|
|
$length = 70; |
165
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
166
|
|
|
$charactersLength = strlen($characters); |
167
|
|
|
$randomString = ''; |
168
|
|
|
for ($i = 0; $i < $length; $i++) { |
169
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
170
|
|
|
} |
171
|
|
|
return $randomString; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function destroy($id) |
175
|
|
|
{ |
176
|
|
|
try { |
177
|
|
|
ApiKeys::destroy($id); |
178
|
|
|
Session::flash('message', 'Api Keys Data Deleted Successfuly'); |
179
|
|
|
return Redirect::to('api-manager'); |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
182
|
|
|
return Redirect::to('api-manager'); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
private function getWorkflow($model){ |
187
|
|
|
$data = WorkflowModel::where('content_type', 'like', '%' . $model . '%'); |
188
|
|
|
return $data; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function getState($state){ |
192
|
|
|
$name = \Transliteration::clean_filename(strtolower($state)); |
193
|
|
|
$data = WorkflowState::where('status', 1)->where('name', 'like', '%' . $name . '%'); |
194
|
|
|
return $data; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function getHistory($content_id){ |
198
|
|
|
$data = History::with('getApiKeys') |
199
|
|
|
->with('getWorkflow') |
200
|
|
|
->with('getStateFrom') |
201
|
|
|
->with('getStateTo') |
202
|
|
|
->with('getUserName') |
203
|
|
|
->where('content_id', $content_id); |
204
|
|
|
return $data; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
private function saveHistory($api, $workflow, $statesFrom, $statesTo, $user_id = ""){ |
208
|
|
|
if(Auth::guest()){ $current_user = 1; } |
209
|
|
|
else{ |
210
|
|
|
if($user_id == ""){ $current_user = Auth::user()->id; } |
211
|
|
|
else { $current_user = $user_id; } |
212
|
|
|
} |
213
|
|
|
$history = New History; |
214
|
|
|
$history->content_id = $api->id; |
215
|
|
|
$history->Workflow_id = $workflow->id; |
216
|
|
|
$history->from_state = $statesFrom->id; |
217
|
|
|
$history->to_state = $statesTo->id; |
218
|
|
|
$history->user_id = $current_user; |
219
|
|
|
$history->save(); |
220
|
|
|
return $history; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function request(Request $request) |
224
|
|
|
{ |
225
|
|
|
$validator = Validator::make($request->all(), [ |
226
|
|
|
'client' => 'required', |
227
|
|
|
'request' => 'required', |
228
|
|
|
'user_id' => 'required', |
229
|
|
|
]); |
230
|
|
|
|
231
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
return Response::json(array( |
234
|
|
|
'title' => 'Error', |
235
|
|
|
'type' => 'error', |
236
|
|
|
'message' => $validator->errors()->all() |
237
|
|
|
)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
try { |
241
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
242
|
|
|
$requests = ucwords($request->input('request')); |
243
|
|
|
$user_id = $request->input('user_id'); |
244
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
245
|
|
|
if($data->count() == 0){ |
246
|
|
|
$token = $this->token(); |
247
|
|
|
$api = New ApiKeys; |
248
|
|
|
$api->client = $client; |
249
|
|
|
$api->api_key = $token; |
250
|
|
|
$api->description = $requests; |
251
|
|
|
$api->user_id = $user_id; |
252
|
|
|
|
253
|
|
|
//create history default |
254
|
|
|
$model = "ApiKeys"; |
255
|
|
|
$fromState = "propose"; |
256
|
|
|
$toState = "propose"; |
257
|
|
|
$workflow = $this->getWorkflow($model); |
258
|
|
|
$statesFrom = $this->getState($fromState); |
259
|
|
|
$statesTo = $this->getState($toState); |
260
|
|
|
if($workflow->count() == 0){ |
261
|
|
|
$error = true; |
262
|
|
|
$statusCode = 404; |
263
|
|
|
$title = 'Error'; |
264
|
|
|
$type = 'error'; |
265
|
|
|
$message = 'Error Workflow not found'; |
266
|
|
|
$result = 'Not Found'; |
267
|
|
|
} |
268
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
269
|
|
|
$error = true; |
270
|
|
|
$statusCode = 404; |
271
|
|
|
$title = 'Error'; |
272
|
|
|
$type = 'error'; |
273
|
|
|
$message = 'Error State not active or State not found'; |
274
|
|
|
$result = 'Not Found'; |
275
|
|
|
} |
276
|
|
|
else{ |
277
|
|
|
$api->save(); |
278
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
279
|
|
View Code Duplication |
if($requests == 'Request'){ |
|
|
|
|
280
|
|
|
$model = "ApiKeys"; |
281
|
|
|
$fromState = "propose"; |
282
|
|
|
$toState = $requests; |
283
|
|
|
$workflow = $this->getWorkflow($model); |
284
|
|
|
$statesFrom = $this->getState($fromState); |
285
|
|
|
$statesTo = $this->getState($toState); |
286
|
|
|
if($workflow->count() == 0){ |
287
|
|
|
$error = true; |
288
|
|
|
$statusCode = 404; |
289
|
|
|
$title = 'Error'; |
290
|
|
|
$type = 'error'; |
291
|
|
|
$message = 'Error Workflow not found'; |
292
|
|
|
$result = 'Not Found'; |
293
|
|
|
} |
294
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
295
|
|
|
$error = true; |
296
|
|
|
$statusCode = 404; |
297
|
|
|
$title = 'Error'; |
298
|
|
|
$type = 'error'; |
299
|
|
|
$message = 'Error State not active or State not found'; |
300
|
|
|
$result = 'Not Found'; |
301
|
|
|
} |
302
|
|
|
else{ |
303
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
304
|
|
|
$error = false; |
305
|
|
|
$statusCode = 200; |
306
|
|
|
$title = 'Success'; |
307
|
|
|
$type = 'success'; |
308
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
309
|
|
|
$result = $request->all(); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
else{ |
313
|
|
|
$error = true; |
314
|
|
|
$statusCode = 404; |
315
|
|
|
$title = 'Error'; |
316
|
|
|
$type = 'error'; |
317
|
|
|
$message = 'Value Request must be Request.'; |
318
|
|
|
$result = $request->all(); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
else { |
323
|
|
|
$get = $data->first(); |
324
|
|
|
$history = $this->getHistory($get->id)->get(); |
325
|
|
|
foreach ($history as $value) { |
326
|
|
|
$workstateto = $value->getStateTo->label; |
327
|
|
|
} |
328
|
|
|
if($workstateto == $requests){ |
329
|
|
|
$error = true; |
330
|
|
|
$statusCode = 404; |
331
|
|
|
$title = 'Error'; |
332
|
|
|
$type = 'error'; |
333
|
|
|
$message = 'Data has already been taken.'; |
334
|
|
|
$result = $request->all(); |
335
|
|
|
} |
336
|
|
View Code Duplication |
elseif($workstateto == 'Approved'){ |
|
|
|
|
337
|
|
|
$error = true; |
338
|
|
|
$statusCode = 404; |
339
|
|
|
$title = 'Error'; |
340
|
|
|
$type = 'error'; |
341
|
|
|
$message = 'Data has already been Approved.'; |
342
|
|
|
$result = $request->all(); |
343
|
|
|
} |
344
|
|
View Code Duplication |
elseif($workstateto == 'Rejected'){ |
|
|
|
|
345
|
|
|
$error = true; |
346
|
|
|
$statusCode = 404; |
347
|
|
|
$title = 'Error'; |
348
|
|
|
$type = 'error'; |
349
|
|
|
$message = 'Data has already been Rejected.'; |
350
|
|
|
$result = $request->all(); |
351
|
|
|
} |
352
|
|
View Code Duplication |
else { |
|
|
|
|
353
|
|
|
if($requests == 'Request'){ |
354
|
|
|
$model = "ApiKeys"; |
355
|
|
|
$fromState = "propose"; |
356
|
|
|
$toState = $requests; |
357
|
|
|
$workflow = $this->getWorkflow($model); |
358
|
|
|
$statesFrom = $this->getState($fromState); |
359
|
|
|
$statesTo = $this->getState($toState); |
360
|
|
|
if($workflow->count() == 0){ |
361
|
|
|
$error = true; |
362
|
|
|
$statusCode = 404; |
363
|
|
|
$title = 'Error'; |
364
|
|
|
$type = 'error'; |
365
|
|
|
$message = 'Error Workflow not found'; |
366
|
|
|
$result = 'Not Found'; |
367
|
|
|
} |
368
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
369
|
|
|
$error = true; |
370
|
|
|
$statusCode = 404; |
371
|
|
|
$title = 'Error'; |
372
|
|
|
$type = 'error'; |
373
|
|
|
$message = 'Error State not active or State not found'; |
374
|
|
|
$result = 'Not Found'; |
375
|
|
|
} |
376
|
|
|
else{ |
377
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
378
|
|
|
$error = false; |
379
|
|
|
$statusCode = 200; |
380
|
|
|
$title = 'Success'; |
381
|
|
|
$type = 'success'; |
382
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
383
|
|
|
$result = $request->all(); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
else { |
387
|
|
|
$error = true; |
388
|
|
|
$statusCode = 404; |
389
|
|
|
$title = 'Error'; |
390
|
|
|
$type = 'error'; |
391
|
|
|
$message = 'Value Request must be Request.'; |
392
|
|
|
$result = $request->all(); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
} catch (Exception $e) { |
397
|
|
|
$error = true; |
398
|
|
|
$statusCode = 404; |
399
|
|
|
$title = 'Error'; |
400
|
|
|
$type = 'error'; |
401
|
|
|
$message = 'Error'; |
402
|
|
|
$result = 'Not Found'; |
403
|
|
|
} finally { |
404
|
|
|
return Response::json(array( |
405
|
|
|
'error' => $error, |
|
|
|
|
406
|
|
|
'status' => $statusCode, |
|
|
|
|
407
|
|
|
'title' => $title, |
|
|
|
|
408
|
|
|
'type' => $type, |
|
|
|
|
409
|
|
|
'message' => $message, |
|
|
|
|
410
|
|
|
'result' => $result |
|
|
|
|
411
|
|
|
)); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
public function transition(Request $request) |
416
|
|
|
{ |
417
|
|
|
$validator = Validator::make($request->all(), [ |
418
|
|
|
'client' => 'required', |
419
|
|
|
'request' => 'required', |
420
|
|
|
]); |
421
|
|
|
|
422
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
423
|
|
|
{ |
424
|
|
|
return Response::json(array( |
425
|
|
|
'title' => 'Error', |
426
|
|
|
'type' => 'error', |
427
|
|
|
'message' => $validator->errors()->all() |
428
|
|
|
)); |
429
|
|
|
} |
430
|
|
|
if(Auth::guest()){ $current_user = 1; } |
431
|
|
|
else{ $current_user = Auth::user()->id; } |
432
|
|
|
|
433
|
|
|
try { |
434
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
435
|
|
|
$requests = ucwords($request->input('request')); |
436
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
437
|
|
|
if($data->count() == 0){ |
438
|
|
|
$token = $this->token(); |
439
|
|
|
$api = New ApiKeys; |
440
|
|
|
$api->client = $client; |
441
|
|
|
$api->api_key = $token; |
442
|
|
|
$api->description = $requests; |
443
|
|
|
$api->user_id = $current_user; |
444
|
|
|
|
445
|
|
|
//create history default |
446
|
|
|
$model = "ApiKeys"; |
447
|
|
|
$fromState = "propose"; |
448
|
|
|
$toState = "propose"; |
449
|
|
|
$workflow = $this->getWorkflow($model); |
450
|
|
|
$statesFrom = $this->getState($fromState); |
451
|
|
|
$statesTo = $this->getState($toState); |
452
|
|
|
if($workflow->count() == 0){ |
453
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
454
|
|
|
return Redirect::to('api-manager'); |
455
|
|
|
} |
456
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
457
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
458
|
|
|
return Redirect::to('api-manager'); |
459
|
|
|
} |
460
|
|
|
else{ |
461
|
|
|
$api->save(); |
462
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
463
|
|
|
if($requests == 'Request'){ |
464
|
|
|
$model = "ApiKeys"; |
465
|
|
|
$fromState = "propose"; |
466
|
|
|
$toState = $requests; |
467
|
|
|
$workflow = $this->getWorkflow($model); |
468
|
|
|
$statesFrom = $this->getState($fromState); |
469
|
|
|
$statesTo = $this->getState($toState); |
470
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
471
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
472
|
|
|
return Redirect::to('api-manager'); |
473
|
|
|
} |
474
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
475
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
476
|
|
|
return Redirect::to('api-manager'); |
477
|
|
|
} |
478
|
|
|
else{ |
479
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
480
|
|
|
|
481
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
482
|
|
|
return Redirect::to('api-manager'); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
else{ |
486
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
487
|
|
|
return Redirect::to('api-manager'); |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
else { |
492
|
|
|
$get = $data->first(); |
493
|
|
|
$history = $this->getHistory($get->id)->get(); |
494
|
|
|
foreach ($history as $value) { |
495
|
|
|
$workstateto = $value->getStateTo->label; |
496
|
|
|
} |
497
|
|
|
if($workstateto == $requests){ |
498
|
|
|
// kirim ke client |
499
|
|
|
$error = true; |
500
|
|
|
$statusCode = 404; |
501
|
|
|
$title = 'Error'; |
502
|
|
|
$type = 'error'; |
503
|
|
|
$message = 'Data has already been taken.'; |
504
|
|
|
$result = $request->all(); |
505
|
|
|
|
506
|
|
|
Session::flash('message', 'Error 404 #error Data has already been taken.'); |
507
|
|
|
} |
508
|
|
View Code Duplication |
elseif($workstateto == 'Approved'){ |
|
|
|
|
509
|
|
|
// kirim ke client |
510
|
|
|
$error = true; |
511
|
|
|
$statusCode = 404; |
512
|
|
|
$title = 'Error'; |
513
|
|
|
$type = 'error'; |
514
|
|
|
$message = 'Data has already been Approved.'; |
515
|
|
|
$result = $request->all(); |
516
|
|
|
|
517
|
|
|
Session::flash('message', 'Error 101 #error Data has already been Approved.'); |
518
|
|
|
} |
519
|
|
View Code Duplication |
elseif($workstateto == 'Rejected'){ |
|
|
|
|
520
|
|
|
// kirim ke client |
521
|
|
|
$error = true; |
522
|
|
|
$statusCode = 404; |
523
|
|
|
$title = 'Error'; |
524
|
|
|
$type = 'error'; |
525
|
|
|
$message = 'Data has already been Rejected.'; |
526
|
|
|
$result = $request->all(); |
527
|
|
|
|
528
|
|
|
Session::flash('message', 'Error 101 #error Data has already been Rejected.'); |
529
|
|
|
} |
530
|
|
|
else { |
531
|
|
|
$model = "ApiKeys"; |
532
|
|
|
$fromState = $workstateto; |
533
|
|
|
$toState = $requests; |
534
|
|
|
$workflow = $this->getWorkflow($model); |
535
|
|
|
$statesFrom = $this->getState($fromState); |
536
|
|
|
$statesTo = $this->getState($toState); |
537
|
|
|
if($workflow->count() == 0){ |
538
|
|
|
// kirim ke client |
539
|
|
|
$error = true; |
540
|
|
|
$statusCode = 404; |
541
|
|
|
$title = 'Error'; |
542
|
|
|
$type = 'error'; |
543
|
|
|
$message = 'workflow not found.'; |
544
|
|
|
$result = $request->all(); |
545
|
|
|
|
546
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
547
|
|
|
} |
548
|
|
View Code Duplication |
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
|
|
|
549
|
|
|
// kirim ke client |
550
|
|
|
$error = true; |
551
|
|
|
$statusCode = 404; |
552
|
|
|
$title = 'Error'; |
553
|
|
|
$type = 'error'; |
554
|
|
|
$message = 'state not active or state not found.'; |
555
|
|
|
$result = $request->all(); |
556
|
|
|
|
557
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
558
|
|
|
} |
559
|
|
|
else{ |
560
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
561
|
|
|
// kirim ke client |
562
|
|
|
$error = false; |
563
|
|
|
$statusCode = 200; |
564
|
|
|
$title = 'Success'; |
565
|
|
|
$type = 'success'; |
566
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
567
|
|
|
$result = $get; |
568
|
|
|
|
569
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
570
|
|
|
} |
571
|
|
|
} |
572
|
|
|
$history = $this->getHistory($get->id)->get(); |
573
|
|
|
foreach ($history as $value) { |
574
|
|
|
$workstatefromid = $value->getStateFrom->id; |
|
|
|
|
575
|
|
|
$workstatetoid = $value->getStateTo->id; |
|
|
|
|
576
|
|
|
$workstatefrom = $value->getStateFrom->label; |
577
|
|
|
$workstateto = $value->getStateTo->label; |
578
|
|
|
} |
579
|
|
|
$state = $workstateto; |
580
|
|
|
$transition = $workstatefrom.' To '.$workstateto; |
|
|
|
|
581
|
|
|
$this->SendClient($client, $error, $statusCode, $title, $type, $message, $result, $state, $transition); |
582
|
|
|
return Redirect::to('api-manager'); |
583
|
|
|
} |
584
|
|
|
} catch (Exception $e) { |
585
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
586
|
|
|
return Redirect::to('api-manager'); |
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
private function SendClient($client, $error, $statusCode, $title, $type, $message, $result, $state, $transition){ |
591
|
|
|
if(Auth::guest()){ $current_user = 1; } |
592
|
|
|
else{ $current_user = Auth::user()->id; } |
593
|
|
|
$headers = ['Content-Type' => 'application/json']; |
594
|
|
|
$data = [ |
595
|
|
|
'error' => $error, |
596
|
|
|
'status' => $statusCode, |
597
|
|
|
'title' => $title, |
598
|
|
|
'type' => $type, |
599
|
|
|
'message' => $message, |
600
|
|
|
'result' => $result, |
601
|
|
|
'hostname' => $client, |
602
|
|
|
'keys' => $result->api_key, |
603
|
|
|
'state' => $state, |
604
|
|
|
'transition' => $transition, |
605
|
|
|
'user_id' => $current_user |
606
|
|
|
]; |
607
|
|
|
$body = json_encode($data); |
608
|
|
|
|
609
|
|
|
//kalo udah rilis |
610
|
|
|
$urlget = $client."/api/v1/host-keys/".$client."/get"; |
611
|
|
|
|
612
|
|
|
//untuk local |
613
|
|
|
// $url = "bloger.local/api/v1/host-keys"; |
|
|
|
|
614
|
|
|
|
615
|
|
|
$clients = new \GuzzleHttp\Client(); |
616
|
|
|
$resget = $clients->request('GET', $urlget,['headers'=>$headers]); |
617
|
|
|
$responseget = $resget->getBody(); |
618
|
|
|
$responsesget = json_decode($responseget); |
619
|
|
|
|
620
|
|
|
if($responsesget->result != 'Not Found'){ |
621
|
|
|
$clients = new \GuzzleHttp\Client(); |
622
|
|
|
$url = $client."/api/v1/host-keys/".$responsesget->id; |
623
|
|
|
$res = $clients->request('PUT', $url,['headers'=>$headers,'body'=>$body]); |
624
|
|
|
$response = $res->getBody(); |
625
|
|
|
$responses = json_decode($response); |
626
|
|
|
}else { |
627
|
|
|
$clients = new \GuzzleHttp\Client(); |
628
|
|
|
$url = $client."/api/v1/host-keys"; |
629
|
|
|
$res = $clients->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
630
|
|
|
$response = $res->getBody(); |
631
|
|
|
$responses = json_decode($response); |
632
|
|
|
} |
633
|
|
|
return $responses; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
public function receive(Request $request){ |
637
|
|
|
return Response::json(array( |
638
|
|
|
'error' => $request->error, |
639
|
|
|
'status' => $request->status, |
640
|
|
|
'title' => $request->title, |
641
|
|
|
'type' => $request->type, |
642
|
|
|
'message' => $request->message, |
643
|
|
|
'result' => $request->result |
644
|
|
|
)); |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
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:
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 thebar
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.