| Total Complexity | 49 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeveloperModulesController 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 DeveloperModulesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class DeveloperModulesController extends Controller |
||
| 21 | { |
||
| 22 | |||
| 23 | private $view = "crudbooster::dev_layouts.modules.modules"; |
||
| 24 | |||
| 25 | public function __construct() |
||
| 26 | { |
||
| 27 | view()->share(['page_title'=>'Module Manager']); |
||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | public function getIndex() { |
||
| 32 | $data = []; |
||
| 33 | $data['result'] = DB::table("cb_modules")->get(); |
||
| 34 | return view($this->view.'.index',$data); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function getAdd() { |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getTables() |
||
| 45 | { |
||
| 46 | return response()->json(cb()->listAllTable()); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getAllColumn($table) { |
||
| 50 | $data = cb()->listAllColumns($table); |
||
| 51 | $pk = cb()->findPrimaryKey($table); |
||
| 52 | $result = []; |
||
| 53 | foreach($data as $item) { |
||
| 54 | |||
| 55 | if(Str::contains(strtolower($item),["title","name","label"])) { |
||
| 56 | $display = true; |
||
| 57 | }else{ |
||
| 58 | $display = false; |
||
| 59 | } |
||
| 60 | |||
| 61 | $result[] = [ |
||
| 62 | "column"=>$item, |
||
| 63 | "primary_key"=>($pk==$item)?true:false, |
||
| 64 | "display"=>$display |
||
| 65 | ]; |
||
| 66 | } |
||
| 67 | return response()->json($result); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getColumns($table) |
||
| 71 | { |
||
| 72 | if(request("modules_id")) { |
||
| 73 | $module = cb()->find("cb_modules",request("modules_id")); |
||
| 74 | if($module->last_column_build) { |
||
| 75 | return response()->json(json_decode($module->last_column_build)); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $columns = cb()->listAllColumns($table); |
||
| 80 | $pk = cb()->findPrimaryKey($table); |
||
| 81 | $result = []; |
||
| 82 | foreach($columns as $column) { |
||
| 83 | if($column != $pk) { |
||
| 84 | |||
| 85 | // Check if any relation table candidate |
||
| 86 | $optionTable = ""; |
||
| 87 | if(Str::substr(strtolower($column),-3,3) == "_id") { |
||
| 88 | $relationTable = Str::substr($column,0,-3); |
||
| 89 | if(Schema::hasTable($relationTable)) { |
||
| 90 | $optionTable = $relationTable; |
||
| 91 | } |
||
| 92 | }elseif (Str::substr(strtolower($column),0,3) == "id_") { |
||
| 93 | $relationTable = Str::substr($column,3); |
||
| 94 | if(Schema::hasTable($relationTable)) { |
||
| 95 | $optionTable = $relationTable; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | $label = trim(Str::title(str_replace(["id_","_id","_"]," ",$column))); |
||
| 100 | $label = Str::singular($label); |
||
| 101 | $type = "text"; |
||
| 102 | |||
| 103 | if(Str::contains(strtolower($label),["photo","image","picture","gambar"])) { |
||
| 104 | $type = "image"; |
||
| 105 | }elseif (Str::contains(strtolower($label),["email","mail"])) { |
||
| 106 | $type = "email"; |
||
| 107 | }elseif (Str::contains(strtolower($label),["description","content","detail"])) { |
||
| 108 | $type = "wysiwyg"; |
||
| 109 | }elseif (Str::contains(strtolower($label),["price","money","grand_total","tax"])) { |
||
| 110 | $type = "money"; |
||
| 111 | }elseif (Str::contains(strtolower($label),["quantity","qty","total","phone","telp"])) { |
||
| 112 | $type = "number"; |
||
| 113 | }elseif (Str::contains(strtolower($label),["date"])) { |
||
| 114 | $type = "date"; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (Str::substr(strtolower($column),-3,3) == "_id") { |
||
| 118 | $type = "select_table"; |
||
| 119 | } elseif (Str::substr($column, -3, 3) == "_at") { |
||
| 120 | $type = "datetime"; |
||
| 121 | } elseif (Str::substr($column,0, 3) == "id_") { |
||
| 122 | $type = "select_table"; |
||
| 123 | } |
||
| 124 | |||
| 125 | $result[] = [ |
||
| 126 | 'column_label'=>$label, |
||
| 127 | 'column_field'=> $column, |
||
| 128 | 'column_type'=>$type, |
||
| 129 | 'column_option_table'=>$optionTable, |
||
| 130 | 'column_option_value'=> "", |
||
| 131 | 'column_option_display'=> "", |
||
| 132 | 'column_option_sql_condition'=> "", |
||
| 133 | 'column_options'=> [], |
||
| 134 | 'column_sql_query'=> "", |
||
| 135 | 'column_help'=> "", |
||
| 136 | 'column_mandatory'=> "on", |
||
| 137 | 'column_browse'=> "on", |
||
| 138 | 'column_detail'=> "on", |
||
| 139 | 'column_edit'=> "on", |
||
| 140 | 'column_add'=> "on", |
||
| 141 | 'listTableColumns'=> [] |
||
| 142 | ]; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | return response()->json($result); |
||
| 146 | } |
||
| 147 | |||
| 148 | private function findComposer() |
||
| 149 | { |
||
| 150 | if (file_exists(getcwd().'/composer.phar')) { |
||
| 151 | return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar'; |
||
| 152 | } |
||
| 153 | |||
| 154 | return 'composer'; |
||
| 155 | } |
||
| 156 | |||
| 157 | private function composingAutoLoad() |
||
| 158 | { |
||
| 159 | $composer = $this->findComposer(); |
||
| 160 | $process = new Process($composer.' dump-autoload'); |
||
|
|
|||
| 161 | $process->setWorkingDirectory(base_path())->run(); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function postCreateMigration() |
||
| 165 | { |
||
| 166 | try { |
||
| 167 | cb()->validation(['table_name','structures']); |
||
| 168 | |||
| 169 | $tableName = request("table_name"); |
||
| 170 | |||
| 171 | if(!Schema::hasTable($tableName)) { |
||
| 172 | $filenameMigration = date("Y_m_d_His")."_".$tableName.".php"; |
||
| 173 | $createTemplate = file_get_contents(base_path("vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs/create.stub")); |
||
| 174 | $className = Str::studly($tableName); |
||
| 175 | $createTemplate = str_replace("DummyClass", $className, $createTemplate); |
||
| 176 | $createTemplate = str_replace("DummyTable", $tableName, $createTemplate); |
||
| 177 | $createTemplate = str_replace("\$table->increments('id');","",$createTemplate); |
||
| 178 | |||
| 179 | $structureItems = ""; |
||
| 180 | |||
| 181 | if(request("timestamp")) { |
||
| 182 | $structureItems .= "\$table->timestamps();\n\t\t\t"; |
||
| 183 | } |
||
| 184 | |||
| 185 | if(request("soft_deletes")) { |
||
| 186 | $structureItems .= "\$table->softDeletes();\n\t\t\t"; |
||
| 187 | } |
||
| 188 | |||
| 189 | foreach(request("structures") as $item) { |
||
| 190 | |||
| 191 | $nullable = ""; |
||
| 192 | |||
| 193 | if(!in_array($item['type_data'],["bigIncrements","increments","mediumIncrements","smallIncrements"])) { |
||
| 194 | $nullable = "->nullable()"; |
||
| 195 | } |
||
| 196 | |||
| 197 | if($item['length']) { |
||
| 198 | $structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."', ".$item['length'].")$nullable;\n\t\t\t"; |
||
| 199 | }else{ |
||
| 200 | $structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."')$nullable;\n\t\t\t"; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | $createTemplate = str_replace("\$table->timestamps();", $structureItems, $createTemplate); |
||
| 204 | |||
| 205 | // Put File onto the migration folders |
||
| 206 | file_put_contents(database_path("migrations/".$filenameMigration), $createTemplate); |
||
| 207 | |||
| 208 | // Composing |
||
| 209 | $this->composingAutoLoad(); |
||
| 210 | |||
| 211 | // Migrate |
||
| 212 | Artisan::call("migrate"); |
||
| 213 | } else { |
||
| 214 | throw new \Exception("The table $tableName has already exists!"); |
||
| 215 | } |
||
| 216 | |||
| 217 | } catch (CBValidationException $e) { |
||
| 218 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 219 | } catch (\Exception $e) { |
||
| 220 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 221 | } |
||
| 222 | |||
| 223 | return response()->json(['status'=>true,'message'=>'Migration successfully!']); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function postCheckExistModule() |
||
| 240 | } |
||
| 241 | |||
| 242 | public function postAddSave() { |
||
| 243 | |||
| 244 | try { |
||
| 245 | cb()->validation(['name', 'table','icon','columns']); |
||
| 246 | |||
| 247 | $module = (new ModuleGenerator(request('table'), request('name'), request('icon'), request("columns"), request("rebuild")))->make(); |
||
| 248 | |||
| 249 | } catch (CBValidationException $e) { |
||
| 250 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 251 | } |
||
| 252 | |||
| 253 | return response()->json(['status'=>true, 'message'=>'Please remember that you can still modify the structure by edit the controller file '.$module['controller'].' :)']); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function getEdit($id) { |
||
| 257 | $data = []; |
||
| 258 | $data['row'] = cb()->find("cb_modules", $id); |
||
| 259 | return view($this->view.".edit", $data); |
||
| 260 | } |
||
| 261 | |||
| 262 | public function postEditSave($id) { |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function getDelete($id) { |
||
| 276 | $module = cb()->find("cb_modules",$id); |
||
| 277 | @unlink(app_path("Http/Controllers/".$module->controller.".php")); |
||
| 278 | DB::table("cb_modules")->where("id", $id)->delete(); |
||
| 279 | DB::table("cb_menus")->where("cb_modules_id", $id)->delete(); |
||
| 280 | return cb()->redirectBack("The module has been deleted!","success"); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getDeleteSoft($id) { |
||
| 288 | } |
||
| 289 | |||
| 290 | } |