| Total Complexity | 81 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 17 | class CB |
||
| 18 | { |
||
| 19 | |||
| 20 | public function getRoleByName($roleName) { |
||
| 21 | return $this->find("cb_roles",['name'=>$roleName]); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function fcm() { |
||
| 25 | return new FCM(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function sidebar() { |
||
| 29 | return new SidebarMenus(); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function session() { |
||
| 33 | return new UserSession(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getDeveloperUrl($path = null) { |
||
| 37 | $path = ($path)?"/".trim($path,"/"):null; |
||
| 38 | return url(cbConfig("DEV_PATH")).$path; |
||
|
|
|||
| 39 | } |
||
| 40 | |||
| 41 | public function getProfileUrl() { |
||
| 42 | return $this->getAdminUrl()."/profile"; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getLogoutUrl() { |
||
| 46 | return $this->getAdminUrl()."/logout"; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getLoginUrl() { |
||
| 50 | return $this->getAdminUrl("login"); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function getAdminUrl($path = null) { |
||
| 54 | $path = ($path)?"/".trim($path,"/"):null; |
||
| 55 | return url(cbConfig("ADMIN_PATH")).$path; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getAppName() { |
||
| 59 | return env("APP_NAME","CRUDBOOSTER"); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function uploadBase64($value) |
||
| 63 | { |
||
| 64 | $fileData = base64_decode($value); |
||
| 65 | $mime_type = finfo_buffer(finfo_open(), $fileData, FILEINFO_MIME_TYPE); |
||
| 66 | if($mime_type) { |
||
| 67 | if($mime_type = explode('/', $mime_type)) { |
||
| 68 | $ext = $mime_type[1]; |
||
| 69 | if($ext) { |
||
| 70 | $filePath = 'uploads/'.date('Y-m'); |
||
| 71 | Storage::makeDirectory($filePath); |
||
| 72 | $filename = sha1(strRandom(5)).'.'.$ext; |
||
| 73 | if (Storage::put($filePath.'/'.$filename, $fileData)) { |
||
| 74 | self::resizeImage($filePath.'/'.$filename); |
||
| 75 | return $filePath.'/'.$filename; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | return null; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function uploadFile($name, $encrypt = true, $resize_width = 1024, $resize_height = null) |
||
| 84 | { |
||
| 85 | if (request()->hasFile($name)) { |
||
| 86 | |||
| 87 | $file = request()->file($name); |
||
| 88 | $ext = $file->getClientOriginalExtension(); |
||
| 89 | if(in_array($ext,cbConfig("UPLOAD_FILE_EXTENSION_ALLOWED"))) { |
||
| 90 | $filename = slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)); |
||
| 91 | $file_path = 'uploads/'.date('Y-m'); |
||
| 92 | |||
| 93 | //Create Directory Monthly |
||
| 94 | Storage::makeDirectory($file_path); |
||
| 95 | |||
| 96 | if ($encrypt == true) { |
||
| 97 | $filename = sha1(strRandom(5)).'.'.$ext; |
||
| 98 | } else { |
||
| 99 | $filename = slug($filename, '_').'.'.$ext; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (Storage::putFileAs($file_path, $file, $filename)) { |
||
| 103 | if($resize_width || $resize_height) { |
||
| 104 | $this->resizeImage($file_path.'/'.$filename, $resize_width, $resize_height); |
||
| 105 | } |
||
| 106 | return $file_path.'/'.$filename; |
||
| 107 | } else { |
||
| 108 | throw new \Exception("Something went wrong, file can't upload!"); |
||
| 109 | } |
||
| 110 | }else{ |
||
| 111 | throw new \Exception("The file format is not allowed!"); |
||
| 112 | } |
||
| 113 | |||
| 114 | } else { |
||
| 115 | throw new \Exception("There is no file send to server!"); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public function resizeImage($fullFilePath, $resize_width = null, $resize_height = null, $qty = 100, $thumbQty = 75) |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function update($table, $id, $data) |
||
| 162 | } |
||
| 163 | |||
| 164 | public function updateCompact($table, $id, $params) { |
||
| 165 | $data = []; |
||
| 166 | foreach ($params as $param) { |
||
| 167 | $data[$param] = request($param); |
||
| 168 | } |
||
| 169 | $this->update($table, $id, $data); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function find($table, $id) |
||
| 173 | { |
||
| 174 | if (is_array($id)) { |
||
| 175 | $first = DB::table($table); |
||
| 176 | foreach ($id as $k => $v) { |
||
| 177 | $first->where($k, $v); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $first->first(); |
||
| 181 | } else { |
||
| 182 | $pk = $this->pk($table); |
||
| 183 | |||
| 184 | return DB::table($table)->where($pk, $id)->first(); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | public function findAll($table, $condition_array = []) |
||
| 189 | { |
||
| 190 | return DB::table($table)->where($condition_array)->get(); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function redirectBack($message, $type = 'warning') |
||
| 194 | { |
||
| 195 | if (request()->ajax()) { |
||
| 196 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $_SERVER['HTTP_REFERER']]); |
||
| 197 | } else { |
||
| 198 | return redirect()->back()->withInput() |
||
| 199 | ->with(['message'=> $message, 'message_type'=> $type]); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | public function redirect($to, $message, $type = 'warning') |
||
| 204 | { |
||
| 205 | if (Request::ajax()) { |
||
| 206 | return response()->json(['message' => $message, 'message_type' => $type, 'redirect_url' => $to]); |
||
| 207 | } else { |
||
| 208 | return redirect($to)->with(['message' => $message, 'message_type' => $type]); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | public function getCurrentMethod() |
||
| 214 | { |
||
| 215 | $action = str_replace("App\Http\Controllers", "", Route::currentRouteAction()); |
||
| 216 | $atloc = strpos($action, '@') + 1; |
||
| 217 | $method = substr($action, $atloc); |
||
| 218 | |||
| 219 | return $method; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function stringBetween($string, $start, $end) |
||
| 223 | { |
||
| 224 | $string = ' '.$string; |
||
| 225 | $ini = strpos($string, $start); |
||
| 226 | if ($ini == 0) { |
||
| 227 | return ''; |
||
| 228 | } |
||
| 229 | $ini += strlen($start); |
||
| 230 | $len = strpos($string, $end, $ini) - $ini; |
||
| 231 | |||
| 232 | return substr($string, $ini, $len); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param array $rules |
||
| 237 | * @param array $messages |
||
| 238 | * @throws CBValidationException |
||
| 239 | */ |
||
| 240 | public function validation($rules = [], $messages = []) |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | public function pk($table) |
||
| 262 | } |
||
| 263 | |||
| 264 | public function findPrimaryKey($table) |
||
| 265 | { |
||
| 266 | $pk = DB::getDoctrineSchemaManager()->listTableDetails($table)->getPrimaryKey(); |
||
| 271 | } |
||
| 272 | |||
| 273 | public function urlFilterColumn($key, $type, $value = '', $singleSorting = true) |
||
| 274 | { |
||
| 275 | $params = Request::all(); |
||
| 276 | $mainpath = trim(self::mainpath(), '/'); |
||
| 277 | $key = sanitizeXSS($key); |
||
| 278 | $type = sanitizeXSS($type); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | |||
| 301 | public function getUrlParameters($exception = null) |
||
| 302 | { |
||
| 303 | $get = request()->all(); |
||
| 304 | $inputhtml = ''; |
||
| 305 | |||
| 306 | if ($get) { |
||
| 307 | if (is_array($exception)) { |
||
| 308 | foreach ($exception as $e) { |
||
| 309 | unset($get[$e]); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | $string_parameters = http_build_query($get); |
||
| 313 | $string_parameters_array = explode('&', $string_parameters); |
||
| 314 | foreach ($string_parameters_array as $s) { |
||
| 315 | $part = explode('=', $s); |
||
| 316 | if(isset($part[0]) && isset($part[1])) { |
||
| 317 | $name = htmlspecialchars(urldecode($part[0])); |
||
| 318 | $name = strip_tags($name); |
||
| 319 | $value = htmlspecialchars(urldecode($part[1])); |
||
| 320 | $value = strip_tags($value); |
||
| 321 | if ($name) { |
||
| 322 | $inputhtml .= "<input type='hidden' name='$name' value='$value'/>\n"; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return $inputhtml; |
||
| 329 | } |
||
| 330 | |||
| 331 | |||
| 332 | public function routeGet($prefix, $controller) { |
||
| 333 | $alias = str_replace("@"," ", $controller); |
||
| 334 | $alias = ucwords($alias); |
||
| 335 | $alias = str_replace(" ","",$alias); |
||
| 336 | Route::get($prefix, ['uses' => $controller, 'as' => $alias]); |
||
| 337 | } |
||
| 338 | |||
| 339 | public function routePost($prefix, $controller) { |
||
| 344 | } |
||
| 345 | |||
| 346 | public function routeGroupBackend(callable $callback, $namespace = 'crocodicstudio\crudbooster\controllers') { |
||
| 352 | } |
||
| 353 | |||
| 354 | /* |
||
| 355 | | -------------------------------------------------------------------------------------------------------------- |
||
| 356 | | Alternate route for Laravel Route::controller |
||
| 357 | | -------------------------------------------------------------------------------------------------------------- |
||
| 358 | | $prefix = path of route |
||
| 359 | | $controller = controller name |
||
| 360 | | $namespace = namespace of controller (optional) |
||
| 361 | | |
||
| 362 | */ |
||
| 363 | public function routeController($prefix, $controller, $namespace = null) |
||
| 400 |