| Total Complexity | 44 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminApiGeneratorController 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 AdminApiGeneratorController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AdminApiGeneratorController extends CBController |
||
| 15 | { |
||
| 16 | public function cbInit() |
||
| 17 | { |
||
| 18 | $this->table = 'cms_apicustom'; |
||
| 19 | $this->primaryKey = "id"; |
||
| 20 | $this->title_field = "nama"; |
||
| 21 | $this->button_show = false; |
||
|
|
|||
| 22 | $this->button_new = false; |
||
| 23 | $this->deleteBtn = false; |
||
| 24 | $this->button_add = false; |
||
| 25 | $this->button_import = false; |
||
| 26 | $this->buttonExport = false; |
||
| 27 | } |
||
| 28 | |||
| 29 | function getIndex() |
||
| 30 | { |
||
| 31 | $this->cbLoader(); |
||
| 32 | |||
| 33 | $data = []; |
||
| 34 | |||
| 35 | $data['page_title'] = 'API Generator'; |
||
| 36 | $data['apis'] = $this->table()->orderby('nama', 'asc')->get(); |
||
| 37 | |||
| 38 | return view('CbApiGen::api_documentation', $data); |
||
| 39 | } |
||
| 40 | |||
| 41 | function apiDocumentation() |
||
| 42 | { |
||
| 43 | $this->cbLoader(); |
||
| 44 | $data = []; |
||
| 45 | |||
| 46 | $data['apis'] = $this->table()->orderby('nama', 'asc')->get(); |
||
| 47 | |||
| 48 | return view('CbApiGen::api_documentation_public', $data); |
||
| 49 | } |
||
| 50 | |||
| 51 | function getDownloadPostman() |
||
| 52 | { |
||
| 53 | $this->cbLoader(); |
||
| 54 | $data = []; |
||
| 55 | $data['variables'] = []; |
||
| 56 | $data['info'] = [ |
||
| 57 | 'name' => cbGetsetting('appname').' - API', |
||
| 58 | '_postman_id' => "1765dd11-73d1-2978-ae11-36921dc6263d", |
||
| 59 | 'description' => '', |
||
| 60 | 'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
||
| 61 | ]; |
||
| 62 | $items = []; |
||
| 63 | foreach ($this->table()->orderby('nama', 'asc')->get() as $api) { |
||
| 64 | $parameters = unserialize($api->parameters); |
||
| 65 | $formdata = []; |
||
| 66 | $httpbuilder = []; |
||
| 67 | if ($parameters) { |
||
| 68 | foreach ($parameters as $p) { |
||
| 69 | $enabled = ($p['used'] == 0) ? false : true; |
||
| 70 | $name = $p['name']; |
||
| 71 | $httpbuilder[$name] = ''; |
||
| 72 | if ($enabled) { |
||
| 73 | $formdata[] = ['key' => $name, 'value' => '', 'type' => 'text', 'enabled' => $enabled]; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | if (strtolower($api->method_type) == 'get' && $httpbuilder) { |
||
| 79 | $httpbuilder = "?".http_build_query($httpbuilder); |
||
| 80 | }else{ |
||
| 81 | $httpbuilder = ''; |
||
| 82 | } |
||
| 83 | |||
| 84 | $items[] = [ |
||
| 85 | 'name' => $api->nama, |
||
| 86 | 'request' => [ |
||
| 87 | 'url' => url('api/'.$api->permalink).$httpbuilder, |
||
| 88 | 'method' => $api->method_type ?: 'GET', |
||
| 89 | 'header' => [], |
||
| 90 | 'body' => [ |
||
| 91 | 'mode' => 'formdata', |
||
| 92 | 'formdata' => $formdata, |
||
| 93 | ], |
||
| 94 | 'description' => $api->keterangan, |
||
| 95 | ], |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | $data['item'] = $items; |
||
| 99 | |||
| 100 | $json = json_encode($data); |
||
| 101 | |||
| 102 | return \Response::make($json, 200, [ |
||
| 103 | 'Content-Type' => 'application/json', |
||
| 104 | 'Content-Disposition' => 'attachment; filename='.cbGetsetting('appname').' - API For POSTMAN.json', |
||
| 105 | ]); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getScreetKey() |
||
| 109 | { |
||
| 110 | $this->cbLoader(); |
||
| 111 | $data['page_title'] = 'API Generator'; |
||
| 112 | $data['apikeys'] = DB::table('cms_apikey')->get(); |
||
| 113 | |||
| 114 | return view('CbApiGen::api_key', $data); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getGenerator() |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getEditApi($id) |
||
| 128 | { |
||
| 129 | $this->cbLoader(); |
||
| 130 | |||
| 131 | $row = $this->findRow($id)->first(); |
||
| 132 | |||
| 133 | $data['row'] = $row; |
||
| 134 | $data['parameters'] = json_encode(unserialize($row->parameters)); |
||
| 135 | $data['responses'] = json_encode(unserialize($row->responses)); |
||
| 136 | $data['page_title'] = 'API Generator'; |
||
| 137 | |||
| 138 | $data['tables'] = CRUDBooster::listCbTables(); |
||
| 139 | |||
| 140 | return view('CbApiGen::api_generator', $data); |
||
| 141 | } |
||
| 142 | |||
| 143 | function getGenerateScreetKey() |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getStatusApikey() |
||
| 167 | { |
||
| 168 | CbValidator::valid(['id' => 'required', 'status' => 'required'], 'view'); |
||
| 169 | |||
| 170 | $id = request('id'); |
||
| 171 | $status = (request('status') == 1) ? "active" : "non active"; |
||
| 172 | |||
| 173 | DB::table('cms_apikey')->where('id', $id)->update(['status' => $status]); |
||
| 174 | |||
| 175 | backWithMsg('You have been update api key status !'); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getDeleteApiKey() |
||
| 186 | } |
||
| 187 | |||
| 188 | function getColumnTable($table, $type = 'list') |
||
| 189 | { |
||
| 190 | $this->cbLoader(); |
||
| 191 | $except = ['created_at', 'deleted_at', 'updated_at']; |
||
| 192 | |||
| 193 | $result = DbInspector::getTableCols($table); |
||
| 194 | $new_result = []; |
||
| 195 | foreach ($result as $ro) { |
||
| 196 | |||
| 197 | if (in_array($ro, $except)) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | $type_field = DbInspector::getFieldTypes($table, $ro); |
||
| 201 | $new_result[] = ['name' => $ro, 'type' => $this->getFieldType($ro, $type_field)]; |
||
| 202 | |||
| 203 | if (!in_array($type, ['list', 'detail']) || !starts_with($ro, 'id_') ) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | $table2 = substr($ro, 3); |
||
| 207 | foreach (DB::getSchemaBuilder()->getColumnListing($table2) as $col) { |
||
| 208 | if (FieldDetector::isExceptional($col) || starts_with($ro, 'id_')) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | $col = str_replace("_$table2", "", $col); |
||
| 212 | $new_result[] = ['name' => $table2.'_'.$col, 'type' => DbInspector::getFieldTypes($table2, $col)]; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | return response()->json($new_result); |
||
| 217 | } |
||
| 218 | |||
| 219 | function postSaveApiCustom() |
||
| 220 | { |
||
| 221 | $this->cbLoader(); |
||
| 222 | $posts = request()->all(); |
||
| 223 | |||
| 224 | $_data = []; |
||
| 225 | |||
| 226 | $_data['nama'] = g('nama'); |
||
| 227 | $_data['tabel'] = $posts['tabel']; |
||
| 228 | $_data['aksi'] = $posts['aksi']; |
||
| 229 | $_data['permalink'] = g('permalink'); |
||
| 230 | $_data['method_type'] = g('method_type'); |
||
| 231 | |||
| 232 | $json = $this->json(g('params_name'), g('params_type'), g('params_config'), g('params_required'), g('params_used')); |
||
| 233 | |||
| 234 | $_data['parameters'] = serialize(array_filter($json)); |
||
| 235 | |||
| 236 | $_data['sql_where'] = g('sql_where'); |
||
| 237 | |||
| 238 | $json = $this->json2(g('responses_name'), g('responses_type'), g('responses_subquery'), g('responses_used')); |
||
| 239 | $json = array_filter($json); |
||
| 240 | $_data['responses'] = serialize($json); |
||
| 241 | $_data['keterangan'] = g('keterangan'); |
||
| 242 | |||
| 243 | $this->saveToDB($_data); |
||
| 244 | |||
| 245 | return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']); |
||
| 246 | } |
||
| 247 | |||
| 248 | function getDeleteApi($id) |
||
| 249 | { |
||
| 250 | $this->cbLoader(); |
||
| 251 | $row = $this->findRow($id)->first(); |
||
| 252 | $this->findRow($id)->delete(); |
||
| 253 | |||
| 254 | $controllername = ucwords(str_replace('_', ' ', $row->permalink)); |
||
| 255 | $controllername = str_replace(' ', '', $controllername); |
||
| 256 | @unlink(base_path(controllers_dir()."Api".$controllername."Controller.php")); |
||
| 257 | |||
| 258 | return response()->json(['status' => 1]); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param $params_name |
||
| 263 | * @param $params_type |
||
| 264 | * @param $params_config |
||
| 265 | * @param $params_required |
||
| 266 | * @param $params_used |
||
| 267 | * @param $json |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | private function json($params_name, $params_type, $params_config, $params_required, $params_used, $json) |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $responses_name |
||
| 291 | * @param $responses_type |
||
| 292 | * @param $responses_subquery |
||
| 293 | * @param $responses_used |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | private function json2($responses_name, $responses_type, $responses_subquery, $responses_used) |
||
| 297 | { |
||
| 298 | $json = []; |
||
| 299 | for ($i = 0, $_count = count($responses_name); $i <= $_count; $i++) { |
||
| 300 | if (! $responses_name[$i]) { |
||
| 301 | continue; |
||
| 302 | } |
||
| 303 | $json[] = [ |
||
| 304 | 'name' => $responses_name[$i], |
||
| 305 | 'type' => $responses_type[$i], |
||
| 306 | 'subquery' => $responses_subquery[$i], |
||
| 307 | 'used' => $responses_used[$i], |
||
| 308 | ]; |
||
| 309 | } |
||
| 310 | |||
| 311 | return $json; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $a |
||
| 316 | */ |
||
| 317 | private function saveToDB($a) |
||
| 318 | { |
||
| 319 | if (request('id')) { |
||
| 320 | return $this->findRow(g('id'))->update($a); |
||
| 321 | } |
||
| 322 | |||
| 323 | $controllerName = ucwords(str_replace('_', ' ', $a['permalink'])); |
||
| 324 | $controllerName = str_replace(' ', '', $controllerName); |
||
| 325 | $this->generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']); |
||
| 326 | |||
| 327 | return $this->table()->insert($a); |
||
| 328 | } |
||
| 329 | |||
| 330 | private function generateAPI($controller_name, $table_name, $permalink, $method_type = 'post') |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param $ro string |
||
| 339 | * @param $default string |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | private function getFieldType($ro, $default) |
||
| 359 | } |
||
| 360 | } |
||
| 361 |