Total Complexity | 96 |
Total Lines | 659 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class BaseController extends Controller |
||
23 | { |
||
24 | /** |
||
25 | * to use this class |
||
26 | * create message list as messages in message file |
||
27 | * override __constructor and define your model |
||
28 | * define your rules for index,store and update |
||
29 | */ |
||
30 | |||
31 | /** |
||
32 | * permission types 'admin'|'branch'|'own'|'guest' |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $DEFAULT_PERMISSION_TYPE = 'admin'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $LOCALE_PREFIX = 'controller'; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $DEFAULT_RESULT_PER_PAGE = 15; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $DEFAULT_PAGE_NUMBER = 1; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $pivotFields = []; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $uniqueFields = []; |
||
62 | |||
63 | /** |
||
64 | * @var bool|string |
||
65 | */ |
||
66 | protected $modelName; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $messagePrefix = 'messages.api.v1.'; |
||
72 | |||
73 | /** |
||
74 | * this model |
||
75 | */ |
||
76 | protected $model; |
||
77 | |||
78 | /** |
||
79 | * index request validator rules |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $indexValidateArray = [ |
||
84 | // |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * array of relationship for eager loading |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $indexLoad = [ |
||
93 | // |
||
94 | ]; |
||
95 | |||
96 | /** |
||
97 | * array of relationship for eager loading |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $editLoad = [ |
||
102 | // |
||
103 | ]; |
||
104 | |||
105 | /** |
||
106 | * array of relationship for eager loading |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $updateLoad = [ |
||
111 | // |
||
112 | ]; |
||
113 | |||
114 | /** |
||
115 | * array of relationship for eager loading |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | protected $storeLoad = [ |
||
120 | // |
||
121 | ]; |
||
122 | |||
123 | /** |
||
124 | * store request validator rules |
||
125 | * |
||
126 | * @var array |
||
127 | */ |
||
128 | protected $storeValidateArray = [ |
||
129 | // |
||
130 | ]; |
||
131 | |||
132 | /** |
||
133 | * update request validator rules |
||
134 | * |
||
135 | * @var array |
||
136 | */ |
||
137 | protected $updateValidateArray = [ |
||
138 | // |
||
139 | ]; |
||
140 | |||
141 | protected $middlewareParams = []; |
||
142 | |||
143 | /** |
||
144 | * defaultController constructor. |
||
145 | */ |
||
146 | public function __construct() |
||
147 | { |
||
148 | // dd("I have closest relationship with all US & UK celebrities"); |
||
149 | // init controller |
||
150 | $this->initController(); |
||
151 | |||
152 | // set local language |
||
153 | $this->setLocale(); |
||
154 | } |
||
155 | |||
156 | abstract public function initController(); |
||
157 | |||
158 | /** |
||
159 | * Display a listing of the resource. |
||
160 | * |
||
161 | * @param Request $request |
||
162 | * @return string |
||
163 | */ |
||
164 | public function index(Request $request) |
||
260 | } |
||
261 | } |
||
262 | |||
263 | public function checkRequestValidation(Request $request, $validationArray) |
||
264 | { |
||
265 | $requestParams = $request->toArray(); |
||
266 | $validator = Validator::make($request->all(), $validationArray); |
||
267 | if ($validator->fails()) { |
||
268 | return $validator->errors(); |
||
269 | } |
||
270 | return null; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param $status |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public function message($status) |
||
278 | { |
||
279 | $key = $this->messagePrefix . $this->modelName . '.' . debug_backtrace()[1]['function'] . '.' . $status; |
||
280 | return $this->getMessageFromFile($key); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param $key |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function getMessageFromFile($key) |
||
288 | { |
||
289 | return config($key); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Show the form for creating a new resource. |
||
294 | * |
||
295 | * @return \Illuminate\Http\JsonResponse |
||
296 | */ |
||
297 | public function create() |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Store a newly created resource in storage. |
||
310 | * |
||
311 | * @param \Illuminate\Http\Request $request |
||
312 | * @return \Illuminate\Http\JsonResponse |
||
313 | */ |
||
314 | public function store(Request $request) |
||
315 | { |
||
316 | // handle permission |
||
317 | $request = $this->handlePermission($request, __FUNCTION__); |
||
318 | |||
319 | // Create Response Model |
||
320 | $response = new ResponseModel(); |
||
321 | |||
322 | if (!isset($userId)) { |
||
323 | $userId = 1; |
||
324 | } |
||
325 | |||
326 | //add author id into the request if doesn't exist |
||
327 | if (is_null($request->get('author_id'))) { |
||
328 | $request['author_id'] = $userId; |
||
329 | } |
||
330 | |||
331 | //add user id into the request if doesn't exist |
||
332 | if (is_null($request->get('user_id'))) { |
||
333 | $request['user_id'] = $userId; |
||
334 | } |
||
335 | |||
336 | $validationErrors = $this->checkRequestValidation($request, $this->storeValidateArray); |
||
337 | |||
338 | if ($validationErrors != null) { |
||
339 | if (env('APP_DEBUG', false)) { |
||
340 | $response->setData(collect($validationErrors->toArray())); |
||
341 | } |
||
342 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||
343 | $response->setStatus(false); |
||
344 | $response->setError(99); |
||
345 | return SmartResponse::response($response); |
||
346 | |||
347 | } |
||
348 | |||
349 | try { |
||
350 | // get result of model creation |
||
351 | $result = $this->model->create($request->all()); |
||
352 | // sync many to many relation |
||
353 | foreach ($this->pivotFields as $pivotField) { |
||
354 | if (collect($request[$pivotField])->count()) { |
||
355 | $pivotField = (new StringHelper())->toCamel($pivotField); |
||
356 | $this->model->find($result['id'])->$pivotField()->sync(json_decode($request[$pivotField])); |
||
357 | } |
||
358 | } |
||
359 | $response->setMessage($this->getTrans('store', 'successful')); |
||
360 | |||
361 | |||
362 | $response->setData($this->model |
||
363 | ->where($this->model->getKeyName(), $result['id']) |
||
364 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||
365 | ->get()); |
||
366 | |||
367 | $response->setStatus(true); |
||
368 | } catch (QueryException $exception) { |
||
369 | $response->setError($exception->getCode()); |
||
370 | $response->setMessage($this->getTrans('store', 'failed')); |
||
371 | $response->setStatus(false); |
||
372 | if (env('APP_DEBUG', false)) { |
||
373 | $response->setData(collect($exception->getMessage())); |
||
374 | } |
||
375 | } |
||
376 | return SmartResponse::response($response); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Display the specdefaultied resource. |
||
381 | * |
||
382 | * @param int $id |
||
383 | * @return \Illuminate\Http\JsonResponse |
||
384 | */ |
||
385 | public function show($id) |
||
386 | { |
||
387 | // Create Response Model |
||
388 | $response = new ResponseModel(); |
||
389 | |||
390 | // try to get data |
||
391 | try { |
||
392 | $response->setMessage($this->getTrans('show', 'successful')); |
||
393 | $response->setData(collect($this->model->findOrFail($id))); |
||
394 | |||
395 | // catch exception |
||
396 | } catch (ModelNotFoundException $exception) { |
||
397 | $response->setError($exception->getCode()); |
||
398 | $response->setMessage($this->getTrans('show', 'failed')); |
||
399 | $response->setStatus(false); |
||
400 | if (env('APP_DEBUG', false)) { |
||
401 | $response->setData(collect($exception->getMessage())); |
||
402 | } |
||
403 | } |
||
404 | return SmartResponse::response($response); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Show the form for editing the specified resource. |
||
409 | * |
||
410 | * @param int $id |
||
411 | * @return \Illuminate\Http\JsonResponse |
||
412 | */ |
||
413 | public function edit($id) |
||
414 | { |
||
415 | // Create Response Model |
||
416 | $response = new ResponseModel(); |
||
417 | |||
418 | try { |
||
419 | $response->setMessage($this->getTrans('edit', 'successful')); |
||
420 | $response->setData($this->model |
||
421 | ->where($this->model->getKeyName(), $id) |
||
422 | ->with(collect($this->editLoad)->count() == 0 ? $this->indexLoad : $this->editLoad) |
||
423 | ->get()); |
||
424 | |||
425 | } catch (ModelNotFoundException $exception) { |
||
426 | $response->setError($exception->getCode()); |
||
427 | $response->setMessage($this->getTrans('edit', 'failed')); |
||
428 | $response->setStatus(false); |
||
429 | if (env('APP_DEBUG', false)) { |
||
430 | $response->setData(collect($exception->getMessage())); |
||
431 | } |
||
432 | |||
433 | } |
||
434 | |||
435 | return SmartResponse::response($response); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Update the specified resource in storage. |
||
440 | * |
||
441 | * @param \Illuminate\Http\Request $request |
||
442 | * @param int $id |
||
443 | * @return \Illuminate\Http\JsonResponse |
||
444 | */ |
||
445 | public function update(Request $request, $id) |
||
446 | { |
||
447 | |||
448 | |||
449 | // Create Response Model |
||
450 | $response = new ResponseModel(); |
||
451 | |||
452 | $modelFilter = [ |
||
453 | [$this->model->getKeyName(), '=', $id], |
||
454 | ]; |
||
455 | |||
456 | $validationErrors = $this->checkRequestValidation($request, $this->updateValidateArray); |
||
457 | if ($validationErrors != null) { |
||
458 | if (env('APP_DEBUG', false)) { |
||
459 | $response->setData(collect($validationErrors->toArray())); |
||
460 | } |
||
461 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||
462 | $response->setStatus(false); |
||
463 | $response->setError(99); |
||
464 | return SmartResponse::response($response); |
||
465 | |||
466 | } |
||
467 | |||
468 | try { |
||
469 | // sync many to many relation |
||
470 | foreach ($this->pivotFields as $pivotField) { |
||
471 | if (collect($request[$pivotField])->count()) { |
||
472 | $pivotMethod = (new StringHelper())->toCamel($pivotField); |
||
473 | $this->model->findOrFail($id)->$pivotMethod()->sync(json_decode($request[$pivotField], true)); |
||
474 | } |
||
475 | } |
||
476 | //get result of update |
||
477 | $result = $this->model->findOrFail($id)->update($request->all()); |
||
478 | |||
479 | // return response |
||
480 | $response->setData($this->model |
||
481 | ->where($modelFilter) |
||
482 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||
483 | ->get()); |
||
484 | $response->setMessage( |
||
485 | $this->getTrans('update', 'successful1') . |
||
486 | $result . |
||
487 | $this->getTrans('update', 'successful2') |
||
488 | ); |
||
489 | |||
490 | } catch (ModelNotFoundException $exception) { |
||
491 | $response->setStatus(false); |
||
492 | $response->setMessage($this->getTrans('update', 'model_not_found')); |
||
493 | $response->setError($exception->getCode()); |
||
494 | if (env('APP_DEBUG', false)) { |
||
495 | $response->setData(collect($exception->getMessage())); |
||
496 | } |
||
497 | |||
498 | } catch (QueryException $exception) { |
||
499 | $response->setStatus(false); |
||
500 | $response->setMessage($this->getTrans('update', 'failed')); |
||
501 | $response->setError($exception->getCode()); |
||
502 | if (env('APP_DEBUG', false)) { |
||
503 | $response->setData(collect($exception->getMessage())); |
||
504 | } |
||
505 | |||
506 | } |
||
507 | |||
508 | return SmartResponse::response($response); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Remove the specified resource from storage. |
||
513 | * |
||
514 | * @param int $id |
||
515 | * @return \Illuminate\Http\JsonResponse |
||
516 | */ |
||
517 | public function destroy($id) |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @param $method |
||
541 | * @param $status |
||
542 | * @return array|\Illuminate\Contracts\Translation\Translator|null|string |
||
543 | */ |
||
544 | public function getTrans($method, $status) |
||
545 | { |
||
546 | return trans('laravel_smart_restful::' . $this->LOCALE_PREFIX . '.' . get_class($this->model) . '.' . $method . '.' . $status); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * set local |
||
551 | */ |
||
552 | public function setLocale() |
||
553 | { |
||
554 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
555 | \App::setLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
||
556 | } |
||
557 | } |
||
558 | |||
559 | public function handlePermission(Request $request, $functionName, $params = []) |
||
575 | } |
||
576 | } |
||
577 | |||
578 | public function handleAdminPermission(Request $request, $functionName, $params = []) |
||
579 | { |
||
580 | switch ($functionName) { |
||
581 | case 'index': |
||
582 | case 'store': |
||
583 | case 'show': |
||
584 | case 'edit': |
||
585 | case 'create': |
||
586 | case 'update': |
||
587 | case 'destroy': |
||
588 | default: |
||
589 | return $params; |
||
590 | } |
||
591 | } |
||
592 | |||
593 | public function handleBranchPermission(Request $request, $functionName, $params) |
||
594 | { |
||
595 | switch ($functionName) { |
||
596 | case 'index': |
||
597 | case 'store': |
||
598 | case 'show': |
||
599 | case 'edit': |
||
600 | case 'create': |
||
601 | case 'update': |
||
602 | case 'destroy': |
||
603 | default: |
||
604 | return $params; |
||
605 | } |
||
606 | } |
||
607 | |||
608 | public function handleOwnPermission(Request $request, $functionName, $params) |
||
609 | { |
||
610 | switch ($functionName) { |
||
611 | case 'index': |
||
612 | if (isset($request['filters'])) { |
||
613 | $filters = json_decode($request['filters'], true); |
||
614 | } else { |
||
615 | $filters = []; |
||
616 | } |
||
617 | array_push($filters, [ |
||
618 | "key" => "owner_id", |
||
619 | "operator" => "=", |
||
620 | "value" => auth()->id(), |
||
621 | ]); |
||
622 | $request['filters'] = json_encode($filters); |
||
623 | return $request; |
||
624 | case 'store': |
||
625 | $request['owner_id'] = auth()->id(); |
||
626 | return $request; |
||
627 | case 'show': |
||
628 | $filters = []; |
||
629 | array_push($filters, [ |
||
630 | "key" => "owner_id", |
||
631 | "operator" => "=", |
||
632 | "value" => auth()->id(), |
||
633 | ]); |
||
634 | $request['permission_filters'] = json_encode($filters); |
||
635 | return $request; |
||
636 | case 'edit': |
||
637 | $filters = []; |
||
638 | array_push($filters, [ |
||
639 | "key" => "owner_id", |
||
640 | "operator" => "=", |
||
641 | "value" => auth()->id(), |
||
642 | ]); |
||
643 | $request['permission_filters'] = json_encode($filters); |
||
644 | return $request; |
||
645 | case 'create': |
||
646 | case 'update': |
||
647 | $filters = []; |
||
648 | array_push($filters, [ |
||
649 | "key" => "owner_id", |
||
650 | "operator" => "=", |
||
651 | "value" => auth()->id(), |
||
652 | ]); |
||
653 | $request['permission_filters'] = json_encode($filters); |
||
654 | return $request; |
||
655 | case 'destroy': |
||
656 | $filters = []; |
||
657 | array_push($filters, [ |
||
658 | "key" => "owner_id", |
||
659 | "operator" => "=", |
||
660 | "value" => auth()->id(), |
||
661 | ]); |
||
662 | $request['permission_filters'] = json_encode($filters); |
||
663 | return $request; |
||
664 | default: |
||
665 | return $params; |
||
666 | } |
||
667 | } |
||
668 | |||
669 | public function handleGuestPermission(Request $request, $functionName, $params) |
||
681 | } |
||
682 | } |
||
683 | |||
684 | } |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths