| Total Complexity | 51 |
| Total Lines | 350 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ApiCustomController 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 ApiCustomController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace crocodicstudio\crudbooster\controllers; |
||
| 10 | class ApiCustomController extends CBController |
||
| 11 | { |
||
| 12 | public function cbInit() |
||
| 13 | { |
||
| 14 | $this->table = 'cms_apicustom'; |
||
| 15 | $this->primary_key = 'id'; |
||
| 16 | $this->title_field = "nama"; |
||
| 17 | $this->button_show = false; |
||
| 18 | $this->button_new = false; |
||
| 19 | $this->button_delete = false; |
||
| 20 | $this->button_add = false; |
||
| 21 | $this->button_import = false; |
||
| 22 | $this->button_export = false; |
||
| 23 | } |
||
| 24 | |||
| 25 | function getIndex() |
||
| 26 | { |
||
| 27 | $this->cbLoader(); |
||
| 28 | |||
| 29 | if (! CRUDBooster::isSuperadmin()) { |
||
| 30 | CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'API Index', 'module' => 'API'])); |
||
| 31 | CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access')); |
||
| 32 | } |
||
| 33 | |||
| 34 | $data = []; |
||
| 35 | |||
| 36 | $data['page_title'] = 'API Generator'; |
||
| 37 | $data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
||
| 38 | $data['apis'] = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
||
| 39 | |||
| 40 | return view('crudbooster::api_documentation', $data); |
||
| 41 | } |
||
| 42 | |||
| 43 | function apiDocumentation() |
||
| 44 | { |
||
| 45 | $this->cbLoader(); |
||
| 46 | $data = []; |
||
| 47 | |||
| 48 | $data['apis'] = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
||
| 49 | |||
| 50 | return view('crudbooster::api_documentation_public', $data); |
||
| 51 | } |
||
| 52 | |||
| 53 | function getDownloadPostman() |
||
| 54 | { |
||
| 55 | $this->cbLoader(); |
||
| 56 | $data = []; |
||
| 57 | $data['variables'] = []; |
||
| 58 | $data['info'] = [ |
||
| 59 | 'name' => CRUDBooster::getSetting('appname').' - API', |
||
| 60 | '_postman_id' => "1765dd11-73d1-2978-ae11-36921dc6263d", |
||
| 61 | 'description' => '', |
||
| 62 | 'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
||
| 63 | ]; |
||
| 64 | $items = []; |
||
| 65 | $apis = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
||
| 66 | |||
| 67 | foreach ($apis as $a) { |
||
| 68 | $parameters = unserialize($a->parameters); |
||
| 69 | $formdata = []; |
||
| 70 | $httpbuilder = []; |
||
| 71 | if ($parameters) { |
||
| 72 | foreach ($parameters as $p) { |
||
| 73 | $enabled = ($p['used'] == 0) ? false : true; |
||
| 74 | $name = $p['name']; |
||
| 75 | $httpbuilder[$name] = ''; |
||
| 76 | if ($enabled) { |
||
| 77 | $formdata[] = ['key' => $name, 'value' => '', 'type' => 'text', 'enabled' => $enabled]; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (strtolower($a->method_type) == 'get') { |
||
| 83 | if ($httpbuilder) { |
||
| 84 | $httpbuilder = "?".http_build_query($httpbuilder); |
||
| 85 | } else { |
||
| 86 | $httpbuilder = ''; |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | $httpbuilder = ''; |
||
| 90 | } |
||
| 91 | |||
| 92 | $items[] = [ |
||
| 93 | 'name' => $a->nama, |
||
| 94 | 'request' => [ |
||
| 95 | 'url' => url('api/'.$a->permalink).$httpbuilder, |
||
| 96 | 'method' => $a->method_type ?: 'GET', |
||
| 97 | 'header' => [], |
||
| 98 | 'body' => [ |
||
| 99 | 'mode' => 'formdata', |
||
| 100 | 'formdata' => $formdata, |
||
| 101 | ], |
||
| 102 | 'description' => $a->keterangan, |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | $data['item'] = $items; |
||
| 107 | |||
| 108 | $json = json_encode($data); |
||
| 109 | |||
| 110 | return \Response::make($json, 200, [ |
||
| 111 | 'Content-Type' => 'application/json', |
||
| 112 | 'Content-Disposition' => 'attachment; filename='.CRUDBooster::getSetting('appname').' - API For POSTMAN.json', |
||
| 113 | ]); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function getScreetKey() |
||
| 117 | { |
||
| 118 | $this->cbLoader(); |
||
| 119 | $data['page_title'] = 'API Generator'; |
||
| 120 | $data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
||
| 121 | $data['apikeys'] = DB::table('cms_apikey')->get(); |
||
| 122 | |||
| 123 | return view('crudbooster::api_key', $data); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getGenerator() |
||
| 127 | { |
||
| 128 | $this->cbLoader(); |
||
| 129 | |||
| 130 | if (! CRUDBooster::isSuperadmin()) { |
||
| 131 | CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'API Index', 'module' => 'API'])); |
||
| 132 | CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access')); |
||
| 133 | } |
||
| 134 | |||
| 135 | $data['page_title'] = 'API Generator'; |
||
| 136 | $data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
||
| 137 | |||
| 138 | $tables = CRUDBooster::listTables(); |
||
| 139 | $tables_list = []; |
||
| 140 | foreach ($tables as $tab) { |
||
| 141 | foreach ($tab as $key => $value) { |
||
| 142 | $tables_list[] = $value; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $data['tables'] = $tables_list; |
||
| 147 | |||
| 148 | return view('crudbooster::api_generator', $data); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getEditApi($id) |
||
| 179 | } |
||
| 180 | |||
| 181 | function getGenerateScreetKey() |
||
| 182 | { |
||
| 183 | $this->cbLoader(); |
||
| 184 | //Generate a random string. |
||
| 185 | $token = openssl_random_pseudo_bytes(16); |
||
| 186 | |||
| 187 | //Convert the binary data into hexadecimal representation. |
||
| 188 | $token = bin2hex($token); |
||
| 189 | |||
| 190 | $id = DB::table('cms_apikey')->insertGetId([ |
||
| 191 | 'screetkey' => $token, |
||
| 192 | 'created_at' => date('Y-m-d H:i:s'), |
||
| 193 | 'status' => 'active', |
||
| 194 | 'hit' => 0, |
||
| 195 | ]); |
||
| 196 | |||
| 197 | $response = []; |
||
| 198 | $response['key'] = $token; |
||
| 199 | $response['id'] = $id; |
||
| 200 | |||
| 201 | return response()->json($response); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getStatusApikey() |
||
| 205 | { |
||
| 206 | CRUDBooster::valid(['id', 'status'], 'view'); |
||
| 207 | |||
| 208 | $id = Request::get('id'); |
||
| 209 | $status = (Request::get('status') == 1) ? "active" : "non active"; |
||
| 210 | |||
| 211 | DB::table('cms_apikey')->where('id', $id)->update(['status' => $status]); |
||
| 212 | |||
| 213 | return redirect()->back()->with(['message' => 'You have been update api key status !', 'message_type' => 'success']); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getDeleteApiKey() |
||
| 217 | { |
||
| 218 | |||
| 219 | $id = Request::get('id'); |
||
| 220 | if (DB::table('cms_apikey')->where('id', $id)->delete()) { |
||
| 221 | return response()->json(['status' => 1]); |
||
| 222 | } else { |
||
| 223 | return response()->json(['status' => 0]); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | function getColumnTable($table, $type = 'list') |
||
| 276 | } |
||
| 277 | |||
| 278 | function postSaveApiCustom() |
||
| 279 | { |
||
| 280 | $this->cbLoader(); |
||
| 281 | $posts = Request::all(); |
||
| 282 | |||
| 283 | $a = []; |
||
| 284 | |||
| 285 | $a['nama'] = g('nama'); |
||
| 286 | $a['tabel'] = $posts['tabel']; |
||
| 287 | $a['aksi'] = $posts['aksi']; |
||
| 288 | $a['permalink'] = g('permalink'); |
||
| 289 | $a['method_type'] = g('method_type'); |
||
| 290 | |||
| 291 | $params_name = g('params_name'); |
||
| 292 | $params_type = g('params_type'); |
||
| 293 | $params_config = g('params_config'); |
||
| 294 | $params_required = g('params_required'); |
||
| 295 | $params_used = g('params_used'); |
||
| 296 | $json = []; |
||
| 297 | |||
| 298 | for ($i = 0; $i <= count($params_name); $i++) { |
||
| 299 | if ($params_name[$i]) { |
||
| 300 | $json[] = [ |
||
| 301 | 'name' => $params_name[$i], |
||
| 302 | 'type' => $params_type[$i], |
||
| 303 | 'config' => $params_config[$i], |
||
| 304 | 'required' => $params_required[$i], |
||
| 305 | 'used' => $params_used[$i], |
||
| 306 | ]; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | $json = array_filter($json); |
||
| 311 | $a['parameters'] = serialize($json); |
||
| 312 | |||
| 313 | $a['sql_where'] = g('sql_where'); |
||
| 314 | |||
| 315 | $responses_name = g('responses_name'); |
||
| 316 | $responses_type = g('responses_type'); |
||
| 317 | $responses_subquery = g('responses_subquery'); |
||
| 318 | $responses_used = g('responses_used'); |
||
| 319 | $json = []; |
||
| 320 | for ($i = 0; $i <= count($responses_name); $i++) { |
||
| 321 | if ($responses_name[$i]) { |
||
| 322 | $json[] = [ |
||
| 323 | 'name' => $responses_name[$i], |
||
| 324 | 'type' => $responses_type[$i], |
||
| 325 | 'subquery' => $responses_subquery[$i], |
||
| 326 | 'used' => $responses_used[$i], |
||
| 327 | ]; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | $json = array_filter($json); |
||
| 332 | $a['responses'] = serialize($json); |
||
| 333 | $a['keterangan'] = g('keterangan'); |
||
| 334 | |||
| 335 | if (Request::get('id')) { |
||
| 336 | DB::table('cms_apicustom')->where('id', g('id'))->update($a); |
||
| 337 | } else { |
||
| 338 | |||
| 339 | $controllerName = ucwords(str_replace('_', ' ', $a['permalink'])); |
||
| 340 | $controllerName = str_replace(' ', '', $controllerName); |
||
| 341 | CRUDBooster::generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']); |
||
| 342 | |||
| 343 | DB::table('cms_apicustom')->insert($a); |
||
| 344 | } |
||
| 345 | |||
| 346 | return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']); |
||
| 347 | } |
||
| 348 | |||
| 349 | function getDeleteApi($id) |
||
| 360 | } |
||
| 361 | } |
||
| 362 |
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