| Total Complexity | 86 |
| Total Lines | 443 |
| 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 getRoleByName($roleName) { |
||
| 23 | return $this->find("cb_roles",['name'=>$roleName]); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function fcm() { |
||
| 27 | return new FCM(); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function sidebar() { |
||
| 31 | return new SidebarMenus(); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function session() { |
||
| 35 | return new UserSession(); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getDeveloperUrl($path = null) { |
||
| 39 | $path = ($path)?"/".trim($path,"/"):null; |
||
| 40 | return url("developer/".getSetting("developer_path")).$path; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function getProfileUrl() { |
||
| 44 | return $this->getAdminUrl()."/profile"; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function getLogoutUrl() { |
||
| 48 | return $this->getAdminUrl()."/logout"; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getLoginUrl() { |
||
| 52 | return $this->getAdminUrl("login"); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getAdminUrl($path = null) { |
||
| 56 | $path = ($path)?"/".trim($path,"/"):null; |
||
| 57 | return url(cbConfig("ADMIN_PATH")).$path; |
||
|
|
|||
| 58 | } |
||
| 59 | |||
| 60 | public function getAppName() { |
||
| 61 | return env("APP_NAME","CRUDBOOSTER"); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param $filename |
||
| 66 | * @param $extension |
||
| 67 | * @param null $resize_width |
||
| 68 | * @param null $resize_height |
||
| 69 | * @return string |
||
| 70 | * @throws \Exception |
||
| 71 | */ |
||
| 72 | private function uploadFileProcess($filename, $extension, $encrypt = true, $resize_width = null, $resize_height = null) |
||
| 73 | { |
||
| 74 | if(in_array($extension,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
||
| 75 | $filename = slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)); |
||
| 76 | $file_path = cbConfig("UPLOAD_PATH_FORMAT"); |
||
| 77 | $file_path = str_replace("{Y}",date('Y'), $file_path); |
||
| 78 | $file_path = str_replace("{m}", date('m'), $file_path); |
||
| 79 | $file_path = str_replace("{d}", date("d"), $file_path); |
||
| 80 | |||
| 81 | //Create Directory Base On Template |
||
| 82 | Storage::makeDirectory($file_path); |
||
| 83 | Storage::put($file_path."/index.html"," ","public"); |
||
| 84 | Storage::put($file_path."/.gitignore","!.gitignore","public"); |
||
| 85 | |||
| 86 | if ($encrypt == true) { |
||
| 87 | $filename = md5(strRandom(5)).'.'.$extension; |
||
| 88 | } else { |
||
| 89 | $filename = slug($filename, '_').'.'.$extension; |
||
| 90 | } |
||
| 91 | |||
| 92 | if($resize_width || $resize_height) { |
||
| 93 | $this->resizeImage($file, $file_path.'/'.$filename, $resize_width, $resize_height); |
||
| 94 | return $file_path.'/'.$filename; |
||
| 95 | }else{ |
||
| 96 | if (Storage::put($file_path.'/'.$filename, $file, 'public')) { |
||
| 97 | return $file_path.'/'.$filename; |
||
| 98 | } else { |
||
| 99 | throw new \Exception("Something went wrong, file can't upload!"); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | }else{ |
||
| 103 | throw new \Exception("The file format is not allowed!"); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $base64_value |
||
| 109 | * @param bool $encrypt |
||
| 110 | * @param null $resize_width |
||
| 111 | * @param null $resize_height |
||
| 112 | * @throws \Exception |
||
| 113 | */ |
||
| 114 | public function uploadBase64($filename, $base64_value, $encrypt = true, $resize_width = null, $resize_height = null) |
||
| 115 | { |
||
| 116 | $fileData = base64_decode($base64_value); |
||
| 117 | $mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
||
| 118 | if($mime_type) { |
||
| 119 | if($mime_type = explode('/', $mime_type)) { |
||
| 120 | $ext = $mime_type[1]; |
||
| 121 | if($filename && $ext) { |
||
| 122 | return $this->uploadFileProcess($filename, $ext, $encrypt, $resize_width, $resize_height); |
||
| 123 | } |
||
| 124 | }else { |
||
| 125 | throw new \Exception("Mime type not found"); |
||
| 126 | } |
||
| 127 | }else{ |
||
| 128 | throw new \Exception("Mime type not found"); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $name |
||
| 134 | * @param bool $encrypt |
||
| 135 | * @param int $resize_width |
||
| 136 | * @param null $resize_height |
||
| 137 | * @return string |
||
| 138 | * @throws \Exception |
||
| 139 | */ |
||
| 140 | public function uploadFile($name, $encrypt = true, $resize_width = null, $resize_height = null) |
||
| 141 | { |
||
| 142 | if (request()->hasFile($name)) { |
||
| 143 | $file = request()->file($name); |
||
| 144 | $filename = $file->getClientOriginalName(); |
||
| 145 | $ext = strtolower($file->getClientOriginalExtension()); |
||
| 146 | |||
| 147 | if($filename && $ext) { |
||
| 148 | return $this->uploadFileProcess($filename, $ext, $encrypt, $resize_width, $resize_height); |
||
| 149 | } |
||
| 150 | |||
| 151 | } else { |
||
| 152 | throw new \Exception("There is no file send to server!"); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $file |
||
| 158 | * @param $fullFilePath |
||
| 159 | * @param null $resize_width |
||
| 160 | * @param null $resize_height |
||
| 161 | * @param int $qty |
||
| 162 | * @param int $thumbQty |
||
| 163 | * @throws \Exception |
||
| 164 | */ |
||
| 165 | public function resizeImage($file, $fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
||
| 166 | { |
||
| 167 | $images_ext = cbConfig("UPLOAD_IMAGE_EXTENSION_ALLOWED"); |
||
| 168 | |||
| 169 | $filename = basename($fullFilePath); |
||
| 170 | $file_path = trim(str_replace($filename, '', $fullFilePath), '/'); |
||
| 171 | $ext = pathinfo($fullFilePath, PATHINFO_EXTENSION); |
||
| 172 | |||
| 173 | if (in_array(strtolower($ext), $images_ext)) { |
||
| 174 | |||
| 175 | // Upload file |
||
| 176 | $img = Image::make($file); |
||
| 177 | $img->encode($ext, $qty); |
||
| 178 | |||
| 179 | if ($resize_width && $resize_height) { |
||
| 180 | $img->fit($resize_width, $resize_height); |
||
| 181 | |||
| 182 | } elseif ($resize_width && ! $resize_height) { |
||
| 183 | |||
| 184 | $img->resize($resize_width, null, function ($constraint) { |
||
| 185 | $constraint->aspectRatio(); |
||
| 186 | }); |
||
| 187 | |||
| 188 | } elseif (! $resize_width && $resize_height) { |
||
| 189 | |||
| 190 | $img->resize(null, $resize_height, function ($constraint) { |
||
| 191 | $constraint->aspectRatio(); |
||
| 192 | }); |
||
| 193 | |||
| 194 | } else { |
||
| 195 | |||
| 196 | if ($img->width() > cbConfig("DEFAULT_IMAGE_MAX_WIDTH_RES")) { |
||
| 197 | $img->resize(cbConfig("DEFAULT_IMAGE_MAX_WIDTH_RES"), null, function ($constraint) { |
||
| 198 | $constraint->aspectRatio(); |
||
| 199 | }); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | Storage::put($fullFilePath, $img, 'public'); |
||
| 204 | }else{ |
||
| 205 | throw new \Exception("The file format is not allowed!"); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | public function update($table, $id, $data) |
||
| 210 | { |
||
| 211 | DB::table($table) |
||
| 212 | ->where($this->pk($table), $id) |
||
| 213 | ->update($data); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function updateCompact($table, $id, $params) { |
||
| 217 | $data = []; |
||
| 218 | foreach ($params as $param) { |
||
| 219 | $data[$param] = request($param); |
||
| 220 | } |
||
| 221 | $this->update($table, $id, $data); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function find($table, $id) |
||
| 225 | { |
||
| 226 | if (is_array($id)) { |
||
| 227 | $first = DB::table($table); |
||
| 228 | foreach ($id as $k => $v) { |
||
| 229 | $first->where($k, $v); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $first->first(); |
||
| 233 | } else { |
||
| 234 | $pk = $this->pk($table); |
||
| 235 | |||
| 236 | return DB::table($table)->where($pk, $id)->first(); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | public function listAllTable() |
||
| 243 | } |
||
| 244 | |||
| 245 | public function listAllColumns($table) |
||
| 246 | { |
||
| 247 | return Schema::getColumnListing($table); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function findAll($table, $condition_array = []) |
||
| 251 | { |
||
| 252 | return DB::table($table)->where($condition_array)->get(); |
||
| 253 | } |
||
| 254 | |||
| 255 | public function redirectBack($message, $type = 'warning') |
||
| 256 | { |
||
| 257 | if (request()->ajax()) { |
||
| 258 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
||
| 259 | } else { |
||
| 260 | return redirect()->back()->withInput() |
||
| 261 | ->with(['message'=> $message, 'message_type'=> $type]); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | public function redirect($to, $message, $type = 'warning') |
||
| 266 | { |
||
| 267 | if (Request::ajax()) { |
||
| 268 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]); |
||
| 269 | } else { |
||
| 270 | return redirect($to)->with(['message' => $message, 'message_type' => $type]); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | |||
| 275 | public function getCurrentMethod() |
||
| 276 | { |
||
| 277 | $action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
||
| 278 | $atloc = strpos($action, '@') + 1; |
||
| 279 | $method = substr($action, $atloc); |
||
| 280 | |||
| 281 | return $method; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function stringBetween($string, $start, $end) |
||
| 285 | { |
||
| 286 | $string = ' '.$string; |
||
| 287 | $ini = strpos($string, $start); |
||
| 288 | if ($ini == 0) { |
||
| 289 | return ''; |
||
| 290 | } |
||
| 291 | $ini += strlen($start); |
||
| 292 | $len = strpos($string, $end, $ini) - $ini; |
||
| 293 | |||
| 294 | return substr($string, $ini, $len); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param array $rules |
||
| 299 | * @param array $messages |
||
| 300 | * @throws CBValidationException |
||
| 301 | */ |
||
| 302 | public function validation($rules = [], $messages = []) |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | public function pk($table) |
||
| 324 | } |
||
| 325 | |||
| 326 | public function findPrimaryKey($table) |
||
| 327 | { |
||
| 328 | $pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
||
| 329 | if(!$pk) { |
||
| 330 | return null; |
||
| 331 | } |
||
| 332 | return $pk->getColumns()[0]; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
||
| 336 | { |
||
| 337 | $params = Request::all(); |
||
| 338 | $mainpath = trim(self::mainpath(), '/'); |
||
| 339 | $key = sanitizeXSS($key); |
||
| 340 | $type = sanitizeXSS($type); |
||
| 341 | $value = sanitizeXSS($value); |
||
| 342 | |||
| 343 | if ($params['filter_column'] && $singleSorting) { |
||
| 344 | foreach ($params['filter_column'] as $k => $filter) { |
||
| 345 | foreach ($filter as $t => $val) { |
||
| 346 | if ($t == 'sorting') { |
||
| 347 | unset($params['filter_column'][$k]['sorting']); |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | $params['filter_column'][$key][$type] = $value; |
||
| 354 | |||
| 355 | if (isset($params)) { |
||
| 356 | return $mainpath.'?'.http_build_query($params); |
||
| 357 | } else { |
||
| 358 | return $mainpath.'?filter_column['.$key.']['.$type.']='.$value; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | public function getUrlParameters($exception = null) |
||
| 364 | { |
||
| 365 | $get = request()->all(); |
||
| 366 | $inputhtml = ''; |
||
| 367 | |||
| 368 | if ($get) { |
||
| 369 | if (is_array($exception)) { |
||
| 370 | foreach ($exception as $e) { |
||
| 371 | unset($get[$e]); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | $string_parameters = http_build_query($get); |
||
| 375 | $string_parameters_array = explode('&', $string_parameters); |
||
| 376 | foreach ($string_parameters_array as $s) { |
||
| 377 | $part = explode('=', $s); |
||
| 378 | if(isset($part[0]) && isset($part[1])) { |
||
| 379 | $name = htmlspecialchars(urldecode($part[0])); |
||
| 380 | $name = strip_tags($name); |
||
| 381 | $value = htmlspecialchars(urldecode($part[1])); |
||
| 382 | $value = strip_tags($value); |
||
| 383 | if ($name) { |
||
| 384 | $inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | return $inputhtml; |
||
| 391 | } |
||
| 392 | |||
| 393 | |||
| 394 | public function routeGet($prefix, $controller) { |
||
| 395 | $alias = str_replace("@"," ", $controller); |
||
| 396 | $alias = ucwords($alias); |
||
| 397 | $alias = str_replace(" ","",$alias); |
||
| 398 | Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
||
| 399 | } |
||
| 400 | |||
| 401 | public function routePost($prefix, $controller) { |
||
| 406 | } |
||
| 407 | |||
| 408 | public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
||
| 414 | } |
||
| 415 | |||
| 416 | /* |
||
| 417 | | -------------------------------------------------------------------------------------------------------------- |
||
| 418 | | Alternate route for Laravel Route::controller |
||
| 419 | | -------------------------------------------------------------------------------------------------------------- |
||
| 420 | | $prefix = path of route |
||
| 421 | | $controller = controller name |
||
| 422 | | |
||
| 423 | */ |
||
| 424 | public function routeController($prefix, $controller) |
||
| 462 | |||
| 463 | } |
||
| 464 | } |
||
| 465 | } |
||
| 466 |