Total Complexity | 80 |
Total Lines | 501 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CrudController 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 CrudController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class CrudController extends Controller |
||
13 | { |
||
14 | |||
15 | public function index() |
||
16 | { |
||
17 | return view('dbm::app'); |
||
18 | } |
||
19 | |||
20 | public function all(Request $request) |
||
21 | { |
||
22 | if ($request->ajax()) { |
||
23 | |||
24 | if (($response = DBM::authorize('crud.browse')) !== true) { |
||
25 | return $response; |
||
26 | } |
||
27 | |||
28 | $perPage = (int) $request->perPage; |
||
29 | $query = $request->q; |
||
30 | |||
31 | $userPermissions = DBM::userPermissions(); |
||
32 | $tables = Table::paginate($perPage, null, [], $query); |
||
33 | $objects = DBM::Object()->all(); |
||
34 | |||
35 | $newTables = []; |
||
36 | |||
37 | foreach ($tables as $table) { |
||
38 | foreach ($objects as $object) { |
||
39 | if ($table == $object->name) { |
||
40 | $newTables[] = [ |
||
41 | 'name' => $table, |
||
42 | 'isCrud' => true, |
||
43 | ]; |
||
44 | |||
45 | continue 2; |
||
46 | } |
||
47 | } |
||
48 | |||
49 | $newTables[] = [ |
||
50 | 'name' => $table, |
||
51 | 'isCrud' => false, |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | return response()->json([ |
||
56 | 'success' => true, |
||
57 | 'tables' => $newTables, |
||
58 | 'userPermissions' => $userPermissions, |
||
59 | 'pagination' => $tables, |
||
60 | ]); |
||
61 | } |
||
62 | |||
63 | return response()->json(['success' => false]); |
||
64 | } |
||
65 | |||
66 | public function getObjectDetails(Request $request) |
||
67 | { |
||
68 | if ($request->ajax()) { |
||
69 | |||
70 | try |
||
71 | { |
||
72 | if (!Table::exists($request->table)) { |
||
73 | |||
74 | throw new \Exception("Sorry! There is no table", 1); |
||
75 | |||
76 | } |
||
77 | |||
78 | $tableName = $request->table; |
||
79 | $namespace = DBM::getModelNamespace(); |
||
80 | $relationship_tables = Table::all(); |
||
81 | $isCrudExists = false; |
||
82 | |||
83 | if ($object = DBM::Object()->where('name', $tableName)->first()) { |
||
84 | |||
85 | if (($response = DBM::authorize('crud.update')) !== true) { |
||
86 | return $response; |
||
87 | } |
||
88 | |||
89 | $isCrudExists = true; |
||
90 | |||
91 | if ($object->model == null) { |
||
92 | $namespace = DBM::getModelNamespace(); |
||
93 | |||
94 | $modelName = ucfirst(Str::singular($object->name)); |
||
95 | |||
96 | $model = $namespace . '\\' . $modelName; |
||
97 | $object->model = $model; |
||
98 | } else { |
||
99 | $model = $object->model; |
||
100 | } |
||
101 | |||
102 | $fields = $object->fields()->orderBy('order', 'ASC')->get(); |
||
103 | } else { |
||
104 | if (($response = DBM::authorize('crud.create')) !== true) { |
||
105 | return $response; |
||
106 | } |
||
107 | $table = Table::getTable($tableName); |
||
108 | |||
109 | $modelName = ucfirst(Str::singular($table['name'])); |
||
110 | $model = $namespace . '\\' . $modelName; |
||
111 | |||
112 | $object = new \stdClass; |
||
113 | $object->name = $table['name']; |
||
114 | $object->slug = Str::slug($table['name']); |
||
115 | $object->display_name = ucfirst($table['name']); |
||
116 | $object->model = $model; |
||
117 | $object->controller = ''; |
||
118 | |||
119 | $fields = []; |
||
120 | $order = 1; |
||
121 | |||
122 | foreach ($table['columns'] as $column) { |
||
123 | |||
124 | $fields[] = (object) [ |
||
125 | 'name' => $column->name, |
||
126 | 'display_name' => ucfirst($column->name), |
||
127 | 'type' => DatabaseController::getInputType($column->type['name']), |
||
128 | // 'required' => ($column->autoincrement) ? true : false, |
||
129 | 'create' => ($column->autoincrement) ? false : true, |
||
130 | 'read' => ($column->autoincrement) ? false : true, |
||
131 | 'edit' => ($column->autoincrement) ? false : true, |
||
132 | 'delete' => ($column->autoincrement) ? false : true, |
||
133 | 'function_name' => '', |
||
134 | 'order' => $order, |
||
135 | 'settings' => '{ }', |
||
136 | ]; |
||
137 | |||
138 | $order++; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $object->makeModel = false; |
||
143 | |||
144 | $relationshipDetails = (object) [ |
||
145 | 'type' => 'hasOne', |
||
146 | 'foreignTableDetails' => Table::getTable($relationship_tables[0]), |
||
147 | 'localTableDetails' => Table::getTable($tableName), |
||
148 | |||
149 | ]; |
||
150 | |||
151 | } catch (\Exception $e) { |
||
152 | return response()->json([ |
||
153 | 'success' => false, |
||
154 | 'errors' => [$e->getMessage()], |
||
155 | ], 400); |
||
156 | } |
||
157 | |||
158 | return response()->json([ |
||
159 | 'success' => true, |
||
160 | 'relationship_tables' => $relationship_tables, |
||
161 | 'relationship_details' => $relationshipDetails, |
||
162 | 'object' => $object, |
||
163 | 'fields' => $fields, |
||
164 | 'isCrudExists' => $isCrudExists, |
||
165 | 'userPermissions' => DBM::userPermissions(), |
||
166 | 'driver' => Driver::getConnectionName(), |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | return response()->json(['success' => false]); |
||
171 | } |
||
172 | |||
173 | public function storeOrUpdate(Request $request) |
||
174 | { |
||
175 | if ($request->ajax()) { |
||
176 | |||
177 | if (($response = DBM::authorize('crud.browse')) !== true) { |
||
178 | return $response; |
||
179 | } |
||
180 | |||
181 | $table = $request->object; |
||
182 | $columns = $request->fields; |
||
183 | $action = ($request->isCrudExists) ? 'edit' : 'add'; |
||
184 | |||
185 | // return response()->json(['columns' => $columns, 'action' => $action]); |
||
186 | |||
187 | if ($action == 'add') { |
||
188 | |||
189 | if (($response = DBM::authorize('crud.create')) !== true) { |
||
190 | return $response; |
||
191 | } |
||
192 | |||
193 | try |
||
194 | { |
||
195 | if ($table['model'] == '' || $table['model'] == null) { |
||
196 | return response()->json([ |
||
197 | 'success' => false, |
||
198 | 'errors' => ["Model Must be provided"], |
||
199 | ], 400); |
||
200 | |||
201 | } |
||
202 | |||
203 | if ($table['makeModel'] == true && !class_exists($table['model'])) { |
||
204 | \DBM::makeModel($table['model'], $table['name']); |
||
205 | } else if ($table['makeModel'] == false && !class_exists($table['model'])) { |
||
206 | return response()->json([ |
||
207 | 'success' => false, |
||
208 | 'errors' => ["Create model '" . $table['model'] . "' first or checked create model option"], |
||
209 | ], 400); |
||
210 | } |
||
211 | |||
212 | if (!class_exists($table['controller'])) { |
||
213 | \DBM::makeController($table['controller']); |
||
214 | } |
||
215 | |||
216 | $object = DBM::Object(); |
||
217 | $object->name = $table['name']; |
||
218 | $object->slug = Str::slug($table['slug']); |
||
219 | $object->display_name = ucfirst($table['display_name']); |
||
220 | $object->model = $table['model']; |
||
221 | $object->controller = $table['controller']; |
||
222 | $object->details = [ |
||
223 | 'findColumn' => $table['findColumn'], |
||
224 | 'searchColumn' => $table['searchColumn'], |
||
225 | 'perPage' => $table['perPage'], |
||
226 | 'orderColumn' => $table['orderColumn'], |
||
227 | 'orderDisplayColumn' => $table['orderDisplayColumn'], |
||
228 | 'orderDirection' => $table['orderDirection'], |
||
229 | ]; |
||
230 | |||
231 | if ($object->save()) { |
||
232 | |||
233 | foreach ($columns as $column) { |
||
234 | |||
235 | $field = DBM::Field(); |
||
236 | $field->dbm_object_id = $object->id; |
||
237 | $field->name = $column['name']; |
||
238 | $field->display_name = ucfirst($column['display_name']); |
||
239 | $field->type = $column['type']; |
||
240 | // $field->required = isset($column['required']) ? $column['required'] : false; |
||
241 | $field->create = isset($column['create']) ? $column['create'] : false; |
||
242 | $field->read = isset($column['read']) ? $column['read'] : false; |
||
243 | $field->edit = isset($column['edit']) ? $column['edit'] : false; |
||
244 | $field->delete = isset($column['delete']) ? $column['delete'] : false; |
||
245 | $field->order = $column['order']; |
||
246 | $field->function_name = $column['function_name']; |
||
247 | $field->settings = json_decode($column['settings']); |
||
248 | |||
249 | $field->save(); |
||
250 | } |
||
251 | |||
252 | } |
||
253 | |||
254 | } catch (\Exception $e) { |
||
255 | return $this->generateError([$e->getMessage()]); |
||
256 | } |
||
257 | } else { |
||
258 | |||
259 | if (($response = DBM::authorize('crud.update')) !== true) { |
||
260 | return $response; |
||
261 | } |
||
262 | |||
263 | try |
||
264 | { |
||
265 | |||
266 | if ($table['model'] == '' || $table['model'] == null) { |
||
267 | return $this->generateError(["Model Must be provided"]); |
||
268 | |||
269 | } |
||
270 | |||
271 | if ($table['makeModel'] == true) { |
||
272 | \DBM::makeModel($table['model'], $table['name']); |
||
273 | } else if ($table['makeModel'] == false && !class_exists($table['model'])) { |
||
274 | return $this->generateError(["Create mode '" . $table['model'] . "' first."]); |
||
275 | } |
||
276 | |||
277 | if (!class_exists($table['controller'])) { |
||
278 | \DBM::makeController($table['controller']); |
||
279 | } |
||
280 | |||
281 | $object = DBM::Object()->where('name', $table['name'])->first(); |
||
282 | $object->slug = Str::slug($table['slug']); |
||
283 | $object->display_name = ucfirst($table['display_name']); |
||
284 | $object->model = $table['model']; |
||
285 | $object->controller = $table['controller']; |
||
286 | $object->details = [ |
||
287 | 'findColumn' => $table['findColumn'], |
||
288 | 'searchColumn' => $table['searchColumn'], |
||
289 | 'perPage' => $table['perPage'], |
||
290 | 'orderColumn' => $table['orderColumn'], |
||
291 | 'orderDisplayColumn' => $table['orderDisplayColumn'], |
||
292 | 'orderDirection' => $table['orderDirection'], |
||
293 | ]; |
||
294 | |||
295 | if ($object->update()) { |
||
296 | |||
297 | foreach ($columns as $column) { |
||
298 | |||
299 | $field = DBM::Field()->where([ |
||
300 | 'dbm_object_id' => $object->id, |
||
301 | 'name' => $column['name'], |
||
302 | ])->first(); |
||
303 | |||
304 | $field->display_name = ucfirst($column['display_name']); |
||
305 | $field->type = $column['type']; |
||
306 | // $field->required = isset($column['required']) ? $column['required'] : false; |
||
307 | $field->create = isset($column['create']) ? $column['create'] : false; |
||
308 | $field->read = isset($column['read']) ? $column['read'] : false; |
||
309 | $field->edit = isset($column['edit']) ? $column['edit'] : false; |
||
310 | $field->delete = isset($column['delete']) ? $column['delete'] : false; |
||
311 | $field->order = $column['order']; |
||
312 | $field->function_name = isset($column['function_name']) ? $column['function_name'] : ""; |
||
313 | $field->settings = json_decode($column['settings']); |
||
314 | |||
315 | $field->update(); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | } catch (\Exception $e) { |
||
320 | return $this->generateError([$e->getMessage()]); |
||
321 | } |
||
322 | } |
||
323 | |||
324 | return response()->json([ |
||
325 | 'success' => true, |
||
326 | 'object' => $request->object, |
||
327 | 'fields' => $request->fields, |
||
328 | ]); |
||
329 | } |
||
330 | |||
331 | return response()->json(['success' => false]); |
||
332 | } |
||
333 | |||
334 | public function delete(Request $request) |
||
335 | { |
||
336 | if ($request->ajax()) { |
||
337 | |||
338 | if (($response = DBM::authorize('crud.delete')) !== true) { |
||
339 | return $response; |
||
340 | } |
||
341 | |||
342 | $object = DBM::Object()->where('name', $request->table)->first(); |
||
343 | if ($object) { |
||
344 | $object->fields()->delete(); |
||
345 | $object->delete(); |
||
346 | return response()->json(['success' => true]); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | return response()->json(['success' => false]); |
||
351 | } |
||
352 | /* |
||
353 | * RelationShip |
||
354 | */ |
||
355 | public function getRelation(Request $request) |
||
356 | { |
||
357 | if ($request->ajax()) { |
||
358 | |||
359 | if (($response = DBM::authorize('relationship.update')) !== true) { |
||
360 | return $response; |
||
361 | } |
||
362 | |||
363 | $tableName = $request->table; |
||
364 | $field = json_decode($request->field); |
||
365 | |||
366 | $object = DBM::Object()->where('name', $tableName)->first(); |
||
367 | $fields = $object->fields; |
||
368 | |||
369 | $prefix = (Driver::isMongoDB()) ? "_" : ""; |
||
370 | |||
371 | foreach ($fields as $fld) { |
||
372 | if ($fld->id == $field->{$prefix . "id"}) { |
||
373 | |||
374 | $relationship = $fld->settings; |
||
375 | $localTable = $relationship['localTable']; |
||
376 | $foreignTable = $relationship['foreignTable']; |
||
377 | $pivotTable = $relationship['pivotTable']; |
||
378 | |||
379 | $field->localFields = Table::getTable($localTable); |
||
380 | $field->foreignFields = Table::getTable($foreignTable); |
||
381 | $field->pivotFields = Table::getTable($pivotTable); |
||
382 | $field->relationship = $relationship; |
||
383 | } |
||
384 | } |
||
385 | |||
386 | return response()->json(['success' => true, 'field' => $field]); |
||
387 | } |
||
388 | |||
389 | return response()->json(['success' => false]); |
||
390 | } |
||
391 | |||
392 | public function addRelation(Request $request) |
||
393 | { |
||
394 | if ($request->ajax()) { |
||
395 | |||
396 | if (($response = DBM::authorize('relationship.create')) !== true) { |
||
397 | return $response; |
||
398 | } |
||
399 | |||
400 | $relationship = $request->relationship; |
||
401 | |||
402 | // return response()->json(['success' => true, 'relationship' => $relationship['displayLevel']]); |
||
403 | |||
404 | if (!class_exists($relationship['localModel'])) { |
||
405 | |||
406 | $error = $relationship['localModel'] . " Model not found. Please create the " . $relationship['localModel'] . " model first"; |
||
407 | return $this->generateError([$error]); |
||
408 | } |
||
409 | |||
410 | if (!class_exists($relationship['foreignModel'])) { |
||
411 | |||
412 | $error = $relationship['foreignModel'] . " Model not found. Please create the " . $relationship['foreignModel'] . " model first"; |
||
413 | return $this->generateError([$error]); |
||
414 | } |
||
415 | |||
416 | $fieldName = strtolower(Str::singular($relationship['localTable']) . '_' . $relationship['type'] . '_' . Str::singular($relationship['foreignTable']) . '_relationship'); |
||
417 | $settings = [ |
||
418 | 'relationType' => $relationship['type'], |
||
419 | 'localModel' => $relationship['localModel'], |
||
420 | 'localTable' => $relationship['localTable'], |
||
421 | 'localKey' => $relationship['localKey'], |
||
422 | 'foreignModel' => $relationship['foreignModel'], |
||
423 | 'foreignTable' => $relationship['foreignTable'], |
||
424 | 'foreignKey' => $relationship['foreignKey'], |
||
425 | 'displayLabel' => $relationship['displayLabel'], |
||
426 | 'pivotTable' => $relationship['pivotTable'], |
||
427 | 'parentPivotKey' => $relationship['parentPivotKey'], |
||
428 | 'relatedPivotKey' => $relationship['relatedPivotKey'], |
||
429 | ]; |
||
430 | |||
431 | $object = DBM::Object()->where('name', $relationship['localTable'])->first(); |
||
432 | $order = DBM::Field()->where('dbm_object_id', $object->id)->max('order'); |
||
433 | |||
434 | $field = DBM::Field(); |
||
435 | $field->dbm_object_id = $object->id; |
||
436 | $field->name = $fieldName; |
||
437 | $field->type = 'relationship'; |
||
438 | $field->display_name = ucfirst($relationship['foreignTable']); |
||
439 | $field->order = $order + 1; |
||
440 | $field->settings = $settings; |
||
441 | if ($field->save()) { |
||
442 | return response()->json(['success' => true]); |
||
443 | } |
||
444 | } |
||
445 | |||
446 | return response()->json(['success' => false]); |
||
447 | } |
||
448 | |||
449 | public function updateRelation(Request $request) |
||
484 | } |
||
485 | |||
486 | public function deleteRelation(Request $request) |
||
487 | { |
||
505 | } |
||
506 | |||
507 | protected function generateError($errors) |
||
513 | } |
||
514 | } |
||
515 |
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