Complex classes like BaseApiController 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 BaseApiController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class BaseApiController extends Controller |
||
11 | { |
||
12 | /** |
||
13 | * The config implementation. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $config; |
||
18 | |||
19 | /** |
||
20 | * The relations implementation. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $relations; |
||
25 | |||
26 | /** |
||
27 | * The repo implementation. |
||
28 | * |
||
29 | * @var object |
||
30 | */ |
||
31 | protected $repo; |
||
32 | |||
33 | /** |
||
34 | * Init new object. |
||
35 | * |
||
36 | * @param mixed $repo |
||
37 | * @param CoreConfig $config |
||
38 | * @param string $modelResource |
||
39 | * @return void |
||
|
|||
40 | */ |
||
41 | public function __construct($repo, $config, $modelResource) |
||
57 | |||
58 | /** |
||
59 | * Fetch all records with relations from storage. |
||
60 | * |
||
61 | * @param string $sortBy The name of the column to sort by. |
||
62 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
63 | * @return \Illuminate\Http\Response |
||
64 | */ |
||
65 | public function index($sortBy = 'created_at', $desc = 1) |
||
69 | |||
70 | /** |
||
71 | * Fetch the single object with relations from storage. |
||
72 | * |
||
73 | * @param integer $id Id of the requested model. |
||
74 | * @return \Illuminate\Http\Response |
||
75 | */ |
||
76 | public function find($id) |
||
80 | |||
81 | /** |
||
82 | * Paginate all records with relations from storage |
||
83 | * that matche the given query. |
||
84 | * |
||
85 | * @param string $query The search text. |
||
86 | * @param integer $perPage Number of rows per page default 15. |
||
87 | * @param string $sortBy The name of the column to sort by. |
||
88 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
89 | * @return \Illuminate\Http\Response |
||
90 | */ |
||
91 | public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
95 | |||
96 | /** |
||
97 | * Fetch records from the storage based on the given |
||
98 | * condition. |
||
99 | * |
||
100 | * @param \Illuminate\Http\Request $request |
||
101 | * @param string $sortBy The name of the column to sort by. |
||
102 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
103 | * @return \Illuminate\Http\Response |
||
104 | */ |
||
105 | public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
||
109 | |||
110 | /** |
||
111 | * Fetch the first record from the storage based on the given |
||
112 | * condition. |
||
113 | * |
||
114 | * @param \Illuminate\Http\Request $request |
||
115 | * @return \Illuminate\Http\Response |
||
116 | */ |
||
117 | public function first(Request $request) |
||
121 | |||
122 | /** |
||
123 | * Paginate all records with relations from storage. |
||
124 | * |
||
125 | * @param integer $perPage Number of rows per page default 15. |
||
126 | * @param string $sortBy The name of the column to sort by. |
||
127 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
128 | * @return \Illuminate\Http\Response |
||
129 | */ |
||
130 | public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
134 | |||
135 | /** |
||
136 | * Fetch all records with relations based on |
||
137 | * the given condition from storage in pages. |
||
138 | * |
||
139 | * @param \Illuminate\Http\Request $request |
||
140 | * @param integer $perPage Number of rows per page default 15. |
||
141 | * @param string $sortBy The name of the column to sort by. |
||
142 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
143 | * @return \Illuminate\Http\Response |
||
144 | */ |
||
145 | public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
149 | |||
150 | /** |
||
151 | * Save the given model to storage. |
||
152 | * |
||
153 | * @param \Illuminate\Http\Request $request |
||
154 | * @return \Illuminate\Http\Response |
||
155 | */ |
||
156 | public function save(Request $request) |
||
174 | |||
175 | /** |
||
176 | * Delete by the given id from storage. |
||
177 | * |
||
178 | * @param integer $id Id of the deleted model. |
||
179 | * @return \Illuminate\Http\Response |
||
180 | */ |
||
181 | public function delete($id) |
||
185 | |||
186 | /** |
||
187 | * Return the deleted models in pages based on the given conditions. |
||
188 | * |
||
189 | * @param \Illuminate\Http\Request $request |
||
190 | * @param integer $perPage Number of rows per page default 15. |
||
191 | * @param string $sortBy The name of the column to sort by. |
||
192 | * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
||
193 | * @return \Illuminate\Http\Response |
||
194 | */ |
||
195 | public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
||
199 | |||
200 | /** |
||
201 | * Restore the deleted model. |
||
202 | * |
||
203 | * @param integer $id Id of the restored model. |
||
204 | * @return \Illuminate\Http\Response |
||
205 | */ |
||
206 | public function restore($id) |
||
210 | |||
211 | /** |
||
212 | * Check if the logged in user can do the given permission. |
||
213 | * |
||
214 | * @param string $permission |
||
215 | * @return void |
||
216 | */ |
||
217 | private function checkPermission($permission) |
||
239 | |||
240 | /** |
||
241 | * Set sessions based on the given headers in the request. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | private function setSessions() |
||
272 | |||
273 | /** |
||
274 | * Set relation based on the called api. |
||
275 | * |
||
276 | * @param string $route |
||
277 | * @return void |
||
278 | */ |
||
279 | private function setRelations($route) |
||
285 | |||
286 | /** |
||
287 | * Update the logged in user locale and time zone. |
||
288 | * |
||
289 | * @param object $user |
||
290 | * @return void |
||
291 | */ |
||
292 | private function updateLocaleAndTimezone($user) |
||
311 | } |
||
312 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.