Total Complexity | 103 |
Total Lines | 539 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like CB 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 CB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CB |
||
20 | { |
||
21 | |||
22 | public function htmlHelper() { |
||
23 | return (new HTMLHelper()); |
||
24 | } |
||
25 | |||
26 | public function getRoleByName($roleName) { |
||
28 | } |
||
29 | |||
30 | public function fcm() { |
||
32 | } |
||
33 | |||
34 | public function sidebar() { |
||
36 | } |
||
37 | |||
38 | public function session() { |
||
40 | } |
||
41 | |||
42 | public function getDeveloperPath($path = null) { |
||
43 | $path = ($path)?"/".trim($path,"/"):null; |
||
44 | return "developer/".getSetting("developer_path").$path; |
||
45 | } |
||
46 | |||
47 | public function getDeveloperUrl($path = null) { |
||
48 | return url($this->getDeveloperPath($path)); |
||
49 | } |
||
50 | |||
51 | public function getProfileUrl() { |
||
52 | return $this->getAdminUrl()."/profile"; |
||
53 | } |
||
54 | |||
55 | public function getLogoutUrl() { |
||
56 | return $this->getAdminUrl()."/logout"; |
||
57 | } |
||
58 | |||
59 | public function getLoginUrl() { |
||
60 | return $this->getAdminUrl("login"); |
||
61 | } |
||
62 | |||
63 | public function getAdminPath() { |
||
64 | return getSetting("ADMIN_PATH","admin"); |
||
65 | } |
||
66 | |||
67 | public function getAdminUrl($path = null) { |
||
68 | $path = ($path)?"/".trim($path,"/"):null; |
||
69 | return url($this->getAdminPath()).$path; |
||
|
|||
70 | } |
||
71 | |||
72 | public function getAppName() { |
||
73 | return getSetting("APP_NAME", env("APP_NAME","CRUDBOOSTER")); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param $filename |
||
78 | * @param $extension |
||
79 | * @param $file |
||
80 | * @param null $resize_width |
||
81 | * @param null $resize_height |
||
82 | * @return string |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | private function uploadFileProcess($filename, $extension, $file, $encrypt = true, $resize_width = null, $resize_height = null) |
||
86 | { |
||
87 | if(in_array($extension,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
||
88 | $file_path = cbConfig("UPLOAD_PATH_FORMAT"); |
||
89 | $file_path = str_replace("{Y}",date('Y'), $file_path); |
||
90 | $file_path = str_replace("{m}", date('m'), $file_path); |
||
91 | $file_path = str_replace("{d}", date("d"), $file_path); |
||
92 | |||
93 | //Create Directory Base On Template |
||
94 | Storage::makeDirectory($file_path); |
||
95 | Storage::put($file_path."/index.html"," ","public"); |
||
96 | Storage::put($file_path."/.gitignore","!.gitignore","public"); |
||
97 | |||
98 | if ($encrypt == true) { |
||
99 | $filename = md5(strRandom(5)).'.'.$extension; |
||
100 | } else { |
||
101 | $filename = slug($filename, '_').'.'.$extension; |
||
102 | } |
||
103 | |||
104 | if($resize_width || $resize_height) { |
||
105 | $this->resizeImage($file, $file_path.'/'.$filename, $resize_width, $resize_height); |
||
106 | return $file_path.'/'.$filename; |
||
107 | }else{ |
||
108 | if (Storage::putFileAs($file_path, $file, $filename, 'public')) { |
||
109 | return $file_path.'/'.$filename; |
||
110 | } else { |
||
111 | throw new \Exception("Something went wrong, file can't upload!"); |
||
112 | } |
||
113 | } |
||
114 | }else{ |
||
115 | throw new \Exception("The file format is not allowed!"); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param $base64_value |
||
121 | * @param bool $encrypt |
||
122 | * @param null $resize_width |
||
123 | * @param null $resize_height |
||
124 | * @throws \Exception |
||
125 | */ |
||
126 | public function uploadBase64($filename, $base64_value, $encrypt = true, $resize_width = null, $resize_height = null) |
||
127 | { |
||
128 | $fileData = base64_decode($base64_value); |
||
129 | $mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
||
130 | if($mime_type) { |
||
131 | if($mime_type = explode('/', $mime_type)) { |
||
132 | $ext = $mime_type[1]; |
||
133 | if($filename && $ext) { |
||
134 | return $this->uploadFileProcess($filename, $ext, $fileData, $encrypt, $resize_width, $resize_height); |
||
135 | } |
||
136 | }else { |
||
137 | throw new \Exception("Mime type not found"); |
||
138 | } |
||
139 | }else{ |
||
140 | throw new \Exception("Mime type not found"); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param $name |
||
146 | * @param bool $encrypt |
||
147 | * @param int $resize_width |
||
148 | * @param null $resize_height |
||
149 | * @return string |
||
150 | * @throws \Exception |
||
151 | */ |
||
152 | public function uploadFile($name, $encrypt = true, $resize_width = null, $resize_height = null) |
||
153 | { |
||
154 | if (request()->hasFile($name)) { |
||
155 | $file = request()->file($name); |
||
156 | $filename = $file->getClientOriginalName(); |
||
157 | $ext = strtolower($file->getClientOriginalExtension()); |
||
158 | |||
159 | if($filename && $ext) { |
||
160 | return $this->uploadFileProcess($filename, $ext, $file, $encrypt, $resize_width, $resize_height); |
||
161 | } |
||
162 | |||
163 | } else { |
||
164 | throw new \Exception("There is no file send to server!"); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $file |
||
170 | * @param $fullFilePath |
||
171 | * @param null $resize_width |
||
172 | * @param null $resize_height |
||
173 | * @param int $qty |
||
174 | * @param int $thumbQty |
||
175 | * @throws \Exception |
||
176 | */ |
||
177 | public function resizeImage($file, $fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param $table |
||
223 | * @param $id |
||
224 | * @param $data |
||
225 | */ |
||
226 | public function update($table, $id, $data) |
||
227 | { |
||
228 | DB::table($table) |
||
229 | ->where($this->pk($table), $id) |
||
230 | ->update($data); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param $table |
||
235 | * @param $id |
||
236 | * @param $params |
||
237 | */ |
||
238 | public function updateCompact($table, $id, $params) { |
||
239 | $data = []; |
||
240 | foreach ($params as $param) { |
||
241 | $data[$param] = request($param); |
||
242 | } |
||
243 | $this->update($table, $id, $data); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param $table |
||
248 | * @param $id |
||
249 | */ |
||
250 | public function delete($table, $id) |
||
251 | { |
||
252 | DB::table($table)->where($this->pk($table), $id)->delete(); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param $table |
||
257 | * @param $id |
||
258 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|mixed|null|object |
||
259 | */ |
||
260 | public function find($table, $id) |
||
261 | { |
||
262 | if (is_array($id)) { |
||
263 | $idHash = md5("find".$table.serialize($id)); |
||
264 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
265 | |||
266 | $first = DB::table($table); |
||
267 | foreach ($id as $k => $v) { |
||
268 | $first->where($k, $v); |
||
269 | } |
||
270 | |||
271 | $data = $first->first(); |
||
272 | miscellanousSingleton()->setData($idHash,$data); |
||
273 | return $data; |
||
274 | } else { |
||
275 | $idHash = md5("find".$table.$id); |
||
276 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
277 | |||
278 | $pk = $this->pk($table); |
||
279 | $data = DB::table($table)->where($pk, $id)->first(); |
||
280 | miscellanousSingleton()->setData($idHash,$data); |
||
281 | return $data; |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $table |
||
287 | * @param callable|string|null $conditionOrCallback |
||
288 | * @return \Illuminate\Support\Collection|mixed |
||
289 | */ |
||
290 | public function findAll($table, $conditionOrCallback = null) |
||
291 | { |
||
292 | $data = []; |
||
293 | $idHash = null; |
||
294 | |||
295 | if(is_array($conditionOrCallback)) { |
||
296 | $idHash = md5("findAll".$table.serialize($conditionOrCallback)); |
||
297 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
298 | |||
299 | $data = DB::table($table)->where($conditionOrCallback)->get(); |
||
300 | } elseif (is_callable($conditionOrCallback)) { |
||
301 | $idHash = "findAll".$table.spl_object_hash($conditionOrCallback); |
||
302 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
303 | |||
304 | $data = DB::table($table); |
||
305 | $data = call_user_func($conditionOrCallback, $data); |
||
306 | $data = $data->get(); |
||
307 | } else { |
||
308 | $idHash = md5("findAll".$table.$conditionOrCallback); |
||
309 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
310 | |||
311 | $data = DB::table($table); |
||
312 | if($conditionOrCallback) { |
||
313 | $data = $data->whereRaw($conditionOrCallback); |
||
314 | } |
||
315 | $data = $data->get(); |
||
316 | } |
||
317 | |||
318 | if($idHash && $data) { |
||
319 | miscellanousSingleton()->setData($idHash, $data); |
||
320 | } |
||
321 | |||
322 | return $data; |
||
323 | } |
||
324 | |||
325 | public function listAllTable() |
||
332 | } |
||
333 | |||
334 | public function listAllColumns($table) |
||
335 | { |
||
336 | $idHash = md5("listAllColumns".$table); |
||
337 | if(miscellanousSingleton()->hasData($idHash)) return miscellanousSingleton()->getData($idHash); |
||
338 | $data = Schema::getColumnListing($table); |
||
339 | miscellanousSingleton()->setData($idHash, $data); |
||
340 | return $data; |
||
341 | } |
||
342 | |||
343 | public function redirectBack($message, $type = 'warning') |
||
344 | { |
||
345 | if (request()->ajax()) { |
||
346 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
||
347 | } else { |
||
348 | return redirect()->back()->withInput() |
||
349 | ->with(['message'=> $message, 'message_type'=> $type]); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | public function redirect($to, $message, $type = 'warning') |
||
359 | } |
||
360 | } |
||
361 | |||
362 | |||
363 | public function getCurrentMethod() |
||
364 | { |
||
365 | $action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
||
366 | $atloc = strpos($action, '@') + 1; |
||
367 | $method = substr($action, $atloc); |
||
368 | |||
369 | return $method; |
||
370 | } |
||
371 | |||
372 | public function stringBetween($string, $start, $end) |
||
373 | { |
||
374 | $string = ' '.$string; |
||
375 | $ini = strpos($string, $start); |
||
376 | if ($ini == 0) { |
||
377 | return ''; |
||
378 | } |
||
379 | $ini += strlen($start); |
||
380 | $len = strpos($string, $end, $ini) - $ini; |
||
381 | |||
382 | return substr($string, $ini, $len); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @param array $rules |
||
387 | * @param array $messages |
||
388 | * @throws CBValidationException |
||
389 | */ |
||
390 | public function validation($rules = [], $messages = []) |
||
406 | } |
||
407 | } |
||
408 | |||
409 | public function pk($table) |
||
410 | { |
||
411 | return $this->findPrimaryKey($table); |
||
412 | } |
||
413 | |||
414 | public function findPrimaryKey($table) |
||
415 | { |
||
416 | $pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
||
417 | if(!$pk) { |
||
418 | return null; |
||
419 | } |
||
420 | return $pk->getColumns()[0]; |
||
421 | } |
||
422 | |||
423 | public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
||
424 | { |
||
425 | $params = Request::all(); |
||
426 | $mainpath = trim(self::mainpath(), '/'); |
||
427 | $key = sanitizeXSS($key); |
||
428 | $type = sanitizeXSS($type); |
||
429 | $value = sanitizeXSS($value); |
||
430 | |||
431 | if ($params['filter_column'] && $singleSorting) { |
||
432 | foreach ($params['filter_column'] as $k => $filter) { |
||
433 | foreach ($filter as $t => $val) { |
||
434 | if ($t == 'sorting') { |
||
435 | unset($params['filter_column'][$k]['sorting']); |
||
436 | } |
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | |||
441 | $params['filter_column'][$key][$type] = $value; |
||
442 | |||
443 | if (isset($params)) { |
||
444 | return $mainpath.'?'.http_build_query($params); |
||
445 | } else { |
||
446 | return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
||
447 | } |
||
448 | } |
||
449 | |||
450 | |||
451 | public function getUrlParameters($exception = null) |
||
452 | { |
||
453 | $get = request()->all(); |
||
454 | $inputhtml = ''; |
||
455 | |||
456 | if ($get) { |
||
457 | if (is_array($exception)) { |
||
458 | foreach ($exception as $e) { |
||
459 | unset($get[$e]); |
||
460 | } |
||
461 | } |
||
462 | $string_parameters = http_build_query($get); |
||
463 | $string_parameters_array = explode('&', $string_parameters); |
||
464 | foreach ($string_parameters_array as $s) { |
||
465 | $part = explode('=', $s); |
||
466 | if(isset($part[0]) && isset($part[1])) { |
||
467 | $name = htmlspecialchars(urldecode($part[0])); |
||
468 | $name = strip_tags($name); |
||
469 | $value = htmlspecialchars(urldecode($part[1])); |
||
470 | $value = strip_tags($value); |
||
471 | if ($name) { |
||
472 | $inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | } |
||
477 | |||
478 | return $inputhtml; |
||
479 | } |
||
480 | |||
481 | |||
482 | public function routeGet($prefix, $controller) { |
||
483 | $alias = str_replace("@"," ", $controller); |
||
484 | $alias = ucwords($alias); |
||
485 | $alias = str_replace(" ","",$alias); |
||
486 | Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
||
487 | } |
||
488 | |||
489 | public function routePost($prefix, $controller) { |
||
490 | $alias = str_replace("@"," ", $controller); |
||
491 | $alias = ucwords($alias); |
||
492 | $alias = str_replace(" ","",$alias); |
||
493 | Route::post($prefix, ['uses' => $controller, 'as' => $alias]); |
||
494 | } |
||
495 | |||
496 | public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
||
497 | Route::group([ |
||
498 | 'middleware' => ['web', \crocodicstudio\crudbooster\middlewares\CBBackend::class], |
||
499 | 'prefix' => cb()->getAdminPath(), |
||
500 | 'namespace' => $namespace, |
||
501 | ], $callback); |
||
502 | } |
||
503 | |||
504 | public function routeGroupDeveloper(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
||
510 | } |
||
511 | |||
512 | /* |
||
513 | | -------------------------------------------------------------------------------------------------------------- |
||
514 | | Alternate route for Laravel Route::controller |
||
515 | | -------------------------------------------------------------------------------------------------------------- |
||
516 | | $prefix = path of route |
||
517 | | $controller = controller name |
||
518 | | |
||
519 | */ |
||
520 | public function routeController($prefix, $controller) |
||
558 | |||
559 | } |
||
560 | } |
||
561 | } |
||
562 |