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