| Total Complexity | 55 |
| Total Lines | 500 |
| 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 | * @var int |
||
| 33 | */ |
||
| 34 | protected $DEFAULT_RESULT_PER_PAGE = 15; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $DEFAULT_PAGE_NUMBER = 1; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $pivotFields = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $uniqueFields = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool|string |
||
| 53 | */ |
||
| 54 | protected $modelName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $messagePrefix = 'messages.api.v1.'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * this model |
||
| 63 | */ |
||
| 64 | protected $model; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * index request validator rules |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $indexValidateArray = [ |
||
| 72 | // |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * array of relationship for eager loading |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $indexLoad = [ |
||
| 81 | // |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * array of relationship for eager loading |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $editLoad = [ |
||
| 90 | // |
||
| 91 | ]; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * array of relationship for eager loading |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $updateLoad = [ |
||
| 99 | // |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * array of relationship for eager loading |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $storeLoad = [ |
||
| 108 | // |
||
| 109 | ]; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * store request validator rules |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $storeValidateArray = [ |
||
| 117 | // |
||
| 118 | ]; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * update request validator rules |
||
| 122 | * |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected $updateValidateArray = [ |
||
| 126 | // |
||
| 127 | ]; |
||
| 128 | |||
| 129 | protected $middlewareParams = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * defaultController constructor. |
||
| 133 | */ |
||
| 134 | public function __construct() |
||
| 135 | { |
||
| 136 | // dd("I have closest relationship with all US celebrities"); |
||
| 137 | // init controller |
||
| 138 | $this->initController(); |
||
| 139 | |||
| 140 | // set local language |
||
| 141 | $this->setLocale(); |
||
| 142 | } |
||
| 143 | |||
| 144 | abstract public function initController(); |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Display a listing of the resource. |
||
| 148 | * |
||
| 149 | * @param Request $request |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function index(Request $request) |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | public function checkRequestValidation(Request $request, $validationArray) |
||
| 247 | { |
||
| 248 | $requestParams = $request->toArray(); |
||
| 249 | $validator = Validator::make($request->all(), $validationArray); |
||
| 250 | if ($validator->fails()) { |
||
| 251 | return $validator->errors(); |
||
| 252 | } |
||
| 253 | return null; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $status |
||
| 258 | * @return mixed |
||
| 259 | */ |
||
| 260 | public function message($status) |
||
| 261 | { |
||
| 262 | $key = $this->messagePrefix . $this->modelName . '.' . debug_backtrace()[1]['function'] . '.' . $status; |
||
| 263 | return $this->getMessageFromFile($key); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param $key |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function getMessageFromFile($key) |
||
| 271 | { |
||
| 272 | return config($key); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Show the form for creating a new resource. |
||
| 277 | * |
||
| 278 | * @return \Illuminate\Http\JsonResponse |
||
| 279 | */ |
||
| 280 | public function create() |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Store a newly created resource in storage. |
||
| 293 | * |
||
| 294 | * @param \Illuminate\Http\Request $request |
||
| 295 | * @return \Illuminate\Http\JsonResponse |
||
| 296 | */ |
||
| 297 | public function store(Request $request) |
||
| 298 | { |
||
| 299 | // Create Response Model |
||
| 300 | $response = new ResponseModel(); |
||
| 301 | |||
| 302 | if (!isset($userId)) { |
||
| 303 | $userId = 1; |
||
| 304 | } |
||
| 305 | |||
| 306 | //add author id into the request if doesn't exist |
||
| 307 | if (is_null($request->get('author_id'))) { |
||
| 308 | $request['author_id'] = $userId; |
||
| 309 | } |
||
| 310 | |||
| 311 | //add user id into the request if doesn't exist |
||
| 312 | if (is_null($request->get('user_id'))) { |
||
| 313 | $request['user_id'] = $userId; |
||
| 314 | } |
||
| 315 | |||
| 316 | $validationErrors = $this->checkRequestValidation($request, $this->storeValidateArray); |
||
| 317 | |||
| 318 | if ($validationErrors != null) { |
||
| 319 | if (env('APP_DEBUG', false)) { |
||
| 320 | $response->setData(collect($validationErrors->getMessages())); |
||
| 321 | } |
||
| 322 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||
| 323 | $response->setStatus(false); |
||
| 324 | return SmartResponse::response($response); |
||
| 325 | } |
||
| 326 | try { |
||
| 327 | // get result of model creation |
||
| 328 | $result = $this->model->create($request->all()); |
||
| 329 | // sync many to many relation |
||
| 330 | foreach ($this->pivotFields as $pivotField) { |
||
| 331 | if (collect($request[$pivotField])->count()) { |
||
| 332 | $pivotField = (new StringHelper())->toCamel($pivotField); |
||
| 333 | $this->model->find($result['id'])->$pivotField()->sync(json_decode($request[$pivotField])); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | $response->setMessage($this->getTrans('store', 'successful')); |
||
| 337 | |||
| 338 | |||
| 339 | $response->setData($this->model |
||
| 340 | ->where($this->model->getKeyName(), $result['id']) |
||
| 341 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||
| 342 | ->get()); |
||
| 343 | |||
| 344 | $response->setStatus(true); |
||
| 345 | } catch (QueryException $exception) { |
||
| 346 | $response->setError($exception->getCode()); |
||
| 347 | $response->setMessage($this->getTrans('store', 'failed')); |
||
| 348 | $response->setStatus(false); |
||
| 349 | if (env('APP_DEBUG', false)) { |
||
| 350 | $response->setData(collect($exception->getMessage())); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | return SmartResponse::response($response); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Display the specdefaultied resource. |
||
| 358 | * |
||
| 359 | * @param int $id |
||
| 360 | * @return \Illuminate\Http\JsonResponse |
||
| 361 | */ |
||
| 362 | public function show($id) |
||
| 363 | { |
||
| 364 | // Create Response Model |
||
| 365 | $response = new ResponseModel(); |
||
| 366 | |||
| 367 | // try to get data |
||
| 368 | try { |
||
| 369 | $response->setMessage($this->getTrans('show', 'successful')); |
||
| 370 | $response->setData(collect($this->model->findOrFail($id))); |
||
| 371 | |||
| 372 | // catch exception |
||
| 373 | } catch (ModelNotFoundException $exception) { |
||
| 374 | $response->setError($exception->getCode()); |
||
| 375 | $response->setMessage($this->getTrans('show', 'failed')); |
||
| 376 | $response->setStatus(false); |
||
| 377 | if (env('APP_DEBUG', false)) { |
||
| 378 | $response->setData(collect($exception->getMessage())); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | return SmartResponse::response($response); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Show the form for editing the specified resource. |
||
| 386 | * |
||
| 387 | * @param int $id |
||
| 388 | * @return \Illuminate\Http\JsonResponse |
||
| 389 | */ |
||
| 390 | public function edit($id) |
||
| 391 | { |
||
| 392 | // Create Response Model |
||
| 393 | $response = new ResponseModel(); |
||
| 394 | |||
| 395 | try { |
||
| 396 | $response->setMessage($this->getTrans('edit', 'successful')); |
||
| 397 | $response->setData($this->model |
||
| 398 | ->where($this->model->getKeyName(), $id) |
||
| 399 | ->with(collect($this->editLoad)->count() == 0 ? $this->indexLoad : $this->editLoad) |
||
| 400 | ->get()); |
||
| 401 | |||
| 402 | } catch (ModelNotFoundException $exception) { |
||
| 403 | $response->setError($exception->getCode()); |
||
| 404 | $response->setMessage($this->getTrans('edit', 'failed')); |
||
| 405 | $response->setStatus(false); |
||
| 406 | if (env('APP_DEBUG', false)) { |
||
| 407 | $response->setData(collect($exception->getMessage())); |
||
| 408 | } |
||
| 409 | |||
| 410 | } |
||
| 411 | |||
| 412 | return SmartResponse::response($response); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Update the specified resource in storage. |
||
| 417 | * |
||
| 418 | * @param \Illuminate\Http\Request $request |
||
| 419 | * @param int $id |
||
| 420 | * @return \Illuminate\Http\JsonResponse |
||
| 421 | */ |
||
| 422 | public function update(Request $request, $id) |
||
| 423 | { |
||
| 424 | // Create Response Model |
||
| 425 | $response = new ResponseModel(); |
||
| 426 | |||
| 427 | $validationErrors = $this->checkRequestValidation($request, $this->updateValidateArray); |
||
| 428 | if ($validationErrors != null) { |
||
| 429 | $response->setData(collect($validationErrors->toArray())); |
||
| 430 | $response->setMessage($this->getTrans('update', 'failed_validation')); |
||
| 431 | $response->setStatus(false); |
||
| 432 | $response->setError(99); |
||
| 433 | return SmartResponse::response($response); |
||
| 434 | |||
| 435 | } |
||
| 436 | |||
| 437 | try { |
||
| 438 | // sync many to many relation |
||
| 439 | foreach ($this->pivotFields as $pivotField) { |
||
| 440 | if (collect($request[$pivotField])->count()) { |
||
| 441 | $pivotMethod = (new StringHelper())->toCamel($pivotField); |
||
| 442 | $this->model->findOrFail($id)->$pivotMethod()->sync(json_decode($request[$pivotField], true)); |
||
| 443 | } |
||
| 444 | } |
||
| 445 | //get result of update |
||
| 446 | $result = $this->model->findOrFail($id)->update($request->all()); |
||
| 447 | |||
| 448 | // return response |
||
| 449 | $response->setData($this->model |
||
| 450 | ->where($this->model->getKeyName(), $id) |
||
| 451 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||
| 452 | ->get()); |
||
| 453 | $response->setMessage( |
||
| 454 | $this->getTrans('update', 'successful1') . |
||
| 455 | $result . |
||
| 456 | $this->getTrans('update', 'successful2') |
||
| 457 | ); |
||
| 458 | |||
| 459 | } catch (ModelNotFoundException $exception) { |
||
| 460 | $response->setStatus(false); |
||
| 461 | $response->setMessage($this->getTrans('update', 'model_not_found')); |
||
| 462 | $response->setError($exception->getCode()); |
||
| 463 | if (env('APP_DEBUG', false)) { |
||
| 464 | $response->setData(collect($exception->getMessage())); |
||
| 465 | } |
||
| 466 | |||
| 467 | } catch (QueryException $exception) { |
||
| 468 | $response->setStatus(false); |
||
| 469 | $response->setMessage($this->getTrans('update', 'failed')); |
||
| 470 | $response->setError($exception->getCode()); |
||
| 471 | if (env('APP_DEBUG', false)) { |
||
| 472 | $response->setData(collect($exception->getMessage())); |
||
| 473 | } |
||
| 474 | |||
| 475 | } |
||
| 476 | |||
| 477 | return SmartResponse::response($response); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Remove the specified resource from storage. |
||
| 482 | * |
||
| 483 | * @param int $id |
||
| 484 | * @return \Illuminate\Http\JsonResponse |
||
| 485 | */ |
||
| 486 | public function destroy($id) |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param $method |
||
| 510 | * @param $status |
||
| 511 | * @return array|\Illuminate\Contracts\Translation\Translator|null|string |
||
| 512 | */ |
||
| 513 | public function getTrans($method, $status) |
||
| 514 | { |
||
| 515 | return trans('laravel_smart_restful::' . 'controller.' . get_class($this->model) . '.' . $method . '.' . $status); |
||
| 516 | } |
||
| 517 | |||
| 518 | public function setLocale() |
||
| 522 | } |
||
| 523 | } |
||
| 524 | } |
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