Complex classes like ApiController 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 ApiController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class ApiController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Http status code. |
||
| 18 | * |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $statusCode = Response::HTTP_OK; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Fractal Manager instance. |
||
| 25 | * |
||
| 26 | * @var Manager |
||
| 27 | */ |
||
| 28 | protected $fractal; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Model instance. |
||
| 32 | * |
||
| 33 | * @var \Illuminate\Database\Eloquent\Model; |
||
| 34 | */ |
||
| 35 | protected $model; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Fractal Transformer instance. |
||
| 39 | * |
||
| 40 | * @var \League\Fractal\TransformerAbstract |
||
| 41 | */ |
||
| 42 | protected $transformer; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Illuminate\Http\Request instance. |
||
| 46 | * |
||
| 47 | * @var Request |
||
| 48 | */ |
||
| 49 | protected $request; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Do we need to unguard the model before create/update? |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $unguard = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Number of items displayed at once if not specified. |
||
| 60 | * There is no limit if it is 0 or false. |
||
| 61 | * |
||
| 62 | * @var int|bool |
||
| 63 | */ |
||
| 64 | protected $defaultLimit = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Maximum limit that can be set via $_GET['limit']. |
||
| 68 | * |
||
| 69 | * @var int|bool |
||
| 70 | */ |
||
| 71 | protected $maximumLimit = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Resource key for an item. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $resourceKeySingular = 'data'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Resource key for a collection. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $resourceKeyPlural = 'data'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Constructor. |
||
| 89 | * |
||
| 90 | * @param Request $request |
||
| 91 | */ |
||
| 92 | public function __construct(Request $request) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Eloquent model. |
||
| 109 | * |
||
| 110 | * @return \October\Rain\Database\Model |
||
| 111 | */ |
||
| 112 | abstract protected function model(); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Transformer for the current model. |
||
| 116 | * |
||
| 117 | * @return \League\Fractal\TransformerAbstract |
||
| 118 | */ |
||
| 119 | abstract protected function transformer(); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Serializer for the current model. |
||
| 123 | * |
||
| 124 | * @return \League\Fractal\Serializer\SerializerAbstract |
||
| 125 | */ |
||
| 126 | protected function serializer() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Display a listing of the resource. |
||
| 133 | * GET /api/{resource}. |
||
| 134 | * |
||
| 135 | * @return Response |
||
| 136 | */ |
||
| 137 | public function index() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Store a newly created resource in storage. |
||
| 152 | * POST /api/{resource}. |
||
| 153 | * |
||
| 154 | * @return Response |
||
| 155 | */ |
||
| 156 | public function store() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Display the specified resource. |
||
| 178 | * GET /api/{resource}/{id}. |
||
| 179 | * |
||
| 180 | * @param int $id |
||
| 181 | * |
||
| 182 | * @return Response |
||
| 183 | */ |
||
| 184 | public function show($id) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Update the specified resource in storage. |
||
| 198 | * PUT /api/{resource}/{id}. |
||
| 199 | * |
||
| 200 | * @param int $id |
||
| 201 | * |
||
| 202 | * @return Response |
||
| 203 | */ |
||
| 204 | public function update($id) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Remove the specified resource from storage. |
||
| 232 | * DELETE /api/{resource}/{id}. |
||
| 233 | * |
||
| 234 | * @param int $id |
||
| 235 | * |
||
| 236 | * @return Response |
||
| 237 | */ |
||
| 238 | public function destroy($id) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Show the form for creating the specified resource. |
||
| 253 | * |
||
| 254 | * @return Response |
||
| 255 | */ |
||
| 256 | public function create() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Show the form for editing the specified resource. |
||
| 263 | * |
||
| 264 | * @param int $id |
||
| 265 | * |
||
| 266 | * @return Response |
||
| 267 | */ |
||
| 268 | public function edit($id) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Respond with a given item. |
||
| 275 | * |
||
| 276 | * @param $item |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | protected function respondWithItem($item) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Respond with a given collection. |
||
| 291 | * |
||
| 292 | * @param $collection |
||
| 293 | * @param int $skip |
||
| 294 | * @param int $limit |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | protected function respondWithCollection($collection, $skip = 0, $limit = 0) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the http status code. |
||
| 314 | * |
||
| 315 | * @return int |
||
| 316 | */ |
||
| 317 | protected function getStatusCode() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the http status code. |
||
| 324 | * |
||
| 325 | * @param int $statusCode |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | protected function setStatusCode($statusCode) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Send the response as json. |
||
| 338 | * |
||
| 339 | * @param array $data |
||
| 340 | * @param array $headers |
||
| 341 | * |
||
| 342 | * @return \Illuminate\Http\JsonResponse |
||
| 343 | */ |
||
| 344 | protected function respond($data = [], array $headers = []) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Send the error response as json. |
||
| 351 | * |
||
| 352 | * @param string $message |
||
| 353 | * |
||
| 354 | * @return \Illuminate\Http\JsonResponse |
||
| 355 | */ |
||
| 356 | protected function respondWithError($message) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Prepare root scope and set some meta information. |
||
| 368 | * |
||
| 369 | * @param Item|Collection $resource |
||
| 370 | * |
||
| 371 | * @return \League\Fractal\Scope |
||
| 372 | */ |
||
| 373 | protected function prepareRootScope($resource) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the validation rules for create. |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | protected function rulesForCreate() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the validation rules for update. |
||
| 390 | * |
||
| 391 | * @param int $id |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | protected function rulesForUpdate($id) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Generate a Response with a 400 HTTP header and a given message. |
||
| 402 | * |
||
| 403 | * @param string $message |
||
| 404 | * |
||
| 405 | * @return Response |
||
| 406 | */ |
||
| 407 | protected function errorWrongArgs($message = 'Wrong Arguments') |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Generate a Response with a 401 HTTP header and a given message. |
||
| 414 | * |
||
| 415 | * @param string $message |
||
| 416 | * |
||
| 417 | * @return Response |
||
| 418 | */ |
||
| 419 | protected function errorUnauthorized($message = 'Unauthorized') |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Generate a Response with a 403 HTTP header and a given message. |
||
| 426 | * |
||
| 427 | * @param string $message |
||
| 428 | * |
||
| 429 | * @return Response |
||
| 430 | */ |
||
| 431 | protected function errorForbidden($message = 'Forbidden') |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Generate a Response with a 404 HTTP header and a given message. |
||
| 438 | * |
||
| 439 | * @param string $message |
||
| 440 | * |
||
| 441 | * @return Response |
||
| 442 | */ |
||
| 443 | protected function errorNotFound($message = 'Resource Not Found') |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Generate a Response with a 405 HTTP header and a given message. |
||
| 450 | * |
||
| 451 | * @param string $message |
||
| 452 | * |
||
| 453 | * @return Response |
||
| 454 | */ |
||
| 455 | protected function errorNotAllowed($message = 'Method Not Allowed') |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Generate a Response with a 500 HTTP header and a given message. |
||
| 462 | * |
||
| 463 | * @param string $message |
||
| 464 | * |
||
| 465 | * @return Response |
||
| 466 | */ |
||
| 467 | protected function errorInternalError($message = 'Internal Error') |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Generate a Response with a 501 HTTP header and a given message. |
||
| 474 | * |
||
| 475 | * @param string $message |
||
| 476 | * |
||
| 477 | * @return Response |
||
| 478 | */ |
||
| 479 | protected function errorNotImplemented($message = 'Not implemented') |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Specify relations for eager loading. |
||
| 486 | * |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | protected function getEagerLoad() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get item according to mode. |
||
| 498 | * |
||
| 499 | * @param int $id |
||
| 500 | * @param array $with |
||
| 501 | * |
||
| 502 | * @return mixed |
||
| 503 | */ |
||
| 504 | protected function findItem($id, array $with = []) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Unguard eloquent model if needed. |
||
| 515 | */ |
||
| 516 | protected function unguardIfNeeded() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Calculates limit for a number of items displayed in list. |
||
| 525 | * |
||
| 526 | * @return int |
||
| 527 | */ |
||
| 528 | protected function calculateLimit() |
||
| 534 | } |
||
| 535 |