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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 15 | abstract class BaseController extends LumenController |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * HTTP header status code. |
||
| 19 | * |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $statusCode = 200; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Fractal Manager instance. |
||
| 26 | * |
||
| 27 | * @var Manager |
||
| 28 | */ |
||
| 29 | protected $fractal; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Eloquent model instance. |
||
| 33 | * |
||
| 34 | * @var \Illuminate\Database\Eloquent\Model; |
||
| 35 | */ |
||
| 36 | protected $model; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Fractal Transformer instance. |
||
| 40 | * |
||
| 41 | * @var \League\Fractal\TransformerAbstract |
||
| 42 | */ |
||
| 43 | protected $transformer; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Illuminate\Http\Request instance. |
||
| 47 | * |
||
| 48 | * @var Request |
||
| 49 | */ |
||
| 50 | protected $request; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Do we need to unguard the model before create/update? |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $unguard = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Number of items displayed at once if not specified. |
||
| 61 | * There is no limit if it is 0 or false. |
||
| 62 | * |
||
| 63 | * @var int|bool |
||
| 64 | */ |
||
| 65 | protected $defaultLimit = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Maximum limit that can be set via $_GET['limit']. |
||
| 69 | * |
||
| 70 | * @var int|bool |
||
| 71 | */ |
||
| 72 | protected $maximumLimit = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Resource key for an item. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $resourceKeySingular = 'data'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Resource key for a collection. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $resourceKeyPlural = 'data'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Constructor. |
||
| 90 | * |
||
| 91 | * @param Request $request |
||
| 92 | */ |
||
| 93 | public function __construct(Request $request) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Eloquent model. |
||
| 110 | * |
||
| 111 | * @return \Illuminate\Database\Eloquent\Model |
||
| 112 | */ |
||
| 113 | abstract protected function model(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Transformer for the current model. |
||
| 117 | * |
||
| 118 | * @return \League\Fractal\TransformerAbstract |
||
| 119 | */ |
||
| 120 | abstract protected function transformer(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Serializer for the current model. |
||
| 124 | * |
||
| 125 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
| 126 | */ |
||
| 127 | protected function serializer() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Display a listing of the resource. |
||
| 134 | * GET /api/{resource}. |
||
| 135 | * |
||
| 136 | * @return Response |
||
| 137 | */ |
||
| 138 | public function index() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Store a newly created resource in storage. |
||
| 153 | * POST /api/{resource}. |
||
| 154 | * |
||
| 155 | * @return Response |
||
| 156 | */ |
||
| 157 | public function store() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Display the specified resource. |
||
| 179 | * GET /api/{resource}/{id}. |
||
| 180 | * |
||
| 181 | * @param int $id |
||
| 182 | * |
||
| 183 | * @return Response |
||
| 184 | */ |
||
| 185 | public function show($id) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Update the specified resource in storage. |
||
| 199 | * PUT /api/{resource}/{id}. |
||
| 200 | * |
||
| 201 | * @param int $id |
||
| 202 | * |
||
| 203 | * @return Response |
||
| 204 | */ |
||
| 205 | public function update($id) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Remove the specified resource from storage. |
||
| 233 | * DELETE /api/{resource}/{id}. |
||
| 234 | * |
||
| 235 | * @param int $id |
||
| 236 | * |
||
| 237 | * @return Response |
||
| 238 | */ |
||
| 239 | public function destroy($id) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Show the form for creating the specified resource. |
||
| 254 | * |
||
| 255 | * @return Response |
||
| 256 | */ |
||
| 257 | public function create() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Show the form for editing the specified resource. |
||
| 264 | * |
||
| 265 | * @param int $id |
||
| 266 | * |
||
| 267 | * @return Response |
||
| 268 | */ |
||
| 269 | public function edit($id) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Getter for statusCode. |
||
| 276 | * |
||
| 277 | * @return int |
||
| 278 | */ |
||
| 279 | protected function getStatusCode() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Setter for statusCode. |
||
| 286 | * |
||
| 287 | * @param int $statusCode Value to set |
||
| 288 | * |
||
| 289 | * @return self |
||
| 290 | */ |
||
| 291 | protected function setStatusCode($statusCode) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Respond with a given item. |
||
| 300 | * |
||
| 301 | * @param $item |
||
| 302 | * |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | protected function respondWithItem($item) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Respond with a given collection. |
||
| 316 | * |
||
| 317 | * @param $collection |
||
| 318 | * @param int $skip |
||
| 319 | * @param int $limit |
||
| 320 | * |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | protected function respondWithCollection($collection, $skip = 0, $limit = 0) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Respond with a given array of items. |
||
| 339 | * |
||
| 340 | * @param array $array |
||
| 341 | * @param array $headers |
||
| 342 | * |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | protected function respondWithArray(array $array, array $headers = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Response with the current error. |
||
| 352 | * |
||
| 353 | * @param string $message |
||
| 354 | * |
||
| 355 | * @return mixed |
||
| 356 | */ |
||
| 357 | protected function respondWithError($message) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Prepare root scope and set some meta information. |
||
| 369 | * |
||
| 370 | * @param Item|Collection $resource |
||
| 371 | * |
||
| 372 | * @return \League\Fractal\Scope |
||
| 373 | */ |
||
| 374 | protected function prepareRootScope($resource) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the validation rules for create. |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | protected function rulesForCreate() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the validation rules for update. |
||
| 394 | * |
||
| 395 | * @param int $id |
||
| 396 | * |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | protected function rulesForUpdate($id) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Generate a Response with a 403 HTTP header and a given message. |
||
| 406 | * |
||
| 407 | * @param $message |
||
| 408 | * |
||
| 409 | * @return Response |
||
| 410 | */ |
||
| 411 | protected function errorForbidden($message = 'Forbidden') |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Generate a Response with a 500 HTTP header and a given message. |
||
| 418 | * |
||
| 419 | * @param string $message |
||
| 420 | * |
||
| 421 | * @return Response |
||
| 422 | */ |
||
| 423 | protected function errorInternalError($message = 'Internal Error') |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Generate a Response with a 404 HTTP header and a given message. |
||
| 430 | * |
||
| 431 | * @param string $message |
||
| 432 | * |
||
| 433 | * @return Response |
||
| 434 | */ |
||
| 435 | protected function errorNotFound($message = 'Resource Not Found') |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Generate a Response with a 401 HTTP header and a given message. |
||
| 442 | * |
||
| 443 | * @param string $message |
||
| 444 | * |
||
| 445 | * @return Response |
||
| 446 | */ |
||
| 447 | protected function errorUnauthorized($message = 'Unauthorized') |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Generate a Response with a 400 HTTP header and a given message. |
||
| 454 | * |
||
| 455 | * @param string$message |
||
| 456 | * |
||
| 457 | * @return Response |
||
| 458 | */ |
||
| 459 | protected function errorWrongArgs($message = 'Wrong Arguments') |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Generate a Response with a 501 HTTP header and a given message. |
||
| 466 | * |
||
| 467 | * @param string $message |
||
| 468 | * |
||
| 469 | * @return Response |
||
| 470 | */ |
||
| 471 | protected function errorNotImplemented($message = 'Not implemented') |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Specify relations for eager loading. |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | protected function getEagerLoad() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get item according to mode. |
||
| 492 | * |
||
| 493 | * @param int $id |
||
| 494 | * @param array $with |
||
| 495 | * |
||
| 496 | * @return mixed |
||
| 497 | */ |
||
| 498 | protected function findItem($id, array $with = []) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Unguard eloquent model if needed. |
||
| 509 | */ |
||
| 510 | protected function unguardIfNeeded() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Calculates limit for a number of items displayed in list. |
||
| 519 | * |
||
| 520 | * @return int |
||
| 521 | */ |
||
| 522 | protected function calculateLimit() |
||
| 528 | } |