| Total Complexity | 48 |
| Total Lines | 272 |
| 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() |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getAllColumn($table) { |
||
| 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 | // Skip Column |
||
| 86 | if($column == 'deleted_at') continue; |
||
| 87 | |||
| 88 | // Check if any relation table candidate |
||
| 89 | $optionTable = ""; |
||
| 90 | if(Str::substr(strtolower($column),-3,3) == "_id") { |
||
| 91 | $relationTable = Str::substr($column,0,-3); |
||
| 92 | if(Schema::hasTable($relationTable)) { |
||
| 93 | $optionTable = $relationTable; |
||
| 94 | } |
||
| 95 | }elseif (Str::substr(strtolower($column),0,3) == "id_") { |
||
| 96 | $relationTable = Str::substr($column,3); |
||
| 97 | if(Schema::hasTable($relationTable)) { |
||
| 98 | $optionTable = $relationTable; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $label = trim(Str::title(str_replace(["id_","_id","_"]," ",$column))); |
||
| 103 | $label = Str::singular($label); |
||
| 104 | $type = "text"; |
||
| 105 | |||
| 106 | if(Str::contains(strtolower($label),["photo","image","picture","gambar"])) { |
||
| 107 | $type = "image"; |
||
| 108 | }elseif (Str::contains(strtolower($label),["email","mail"])) { |
||
| 109 | $type = "email"; |
||
| 110 | }elseif (Str::contains(strtolower($label),["description","content","detail"])) { |
||
| 111 | $type = "wysiwyg"; |
||
| 112 | }elseif (Str::contains(strtolower($label),["price","money","grand_total","tax"])) { |
||
| 113 | $type = "money"; |
||
| 114 | }elseif (Str::contains(strtolower($label),["quantity","qty","total","phone","telp"])) { |
||
| 115 | $type = "number"; |
||
| 116 | }elseif (Str::contains(strtolower($label),["date"])) { |
||
| 117 | $type = "date"; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (Str::substr(strtolower($column),-3,3) == "_id") { |
||
| 121 | $type = "select_table"; |
||
| 122 | } elseif (Str::substr($column, -3, 3) == "_at") { |
||
| 123 | $type = "datetime"; |
||
| 124 | } elseif (Str::substr($column,0, 3) == "id_") { |
||
| 125 | $type = "select_table"; |
||
| 126 | } |
||
| 127 | |||
| 128 | $columnAdd = "on"; |
||
| 129 | $columnEdit = "on"; |
||
| 130 | $columnMandatory = "on"; |
||
| 131 | if(in_array($column,['created_at','updated_at'])) { |
||
| 132 | $columnAdd = ""; |
||
| 133 | $columnEdit = ""; |
||
| 134 | $columnMandatory = ""; |
||
| 135 | } |
||
| 136 | |||
| 137 | $result[] = [ |
||
| 138 | 'column_label'=>$label, |
||
| 139 | 'column_field'=> $column, |
||
| 140 | 'column_type'=>$type, |
||
| 141 | 'column_file_encrypt'=>"on", |
||
| 142 | 'column_image_width'=>'', |
||
| 143 | 'column_image_height'=>'', |
||
| 144 | 'column_option_table'=>$optionTable, |
||
| 145 | 'column_date_format'=>'', |
||
| 146 | 'column_text_display_limit'=>150, |
||
| 147 | 'column_text_max'=>255, |
||
| 148 | 'column_text_min'=>0, |
||
| 149 | 'column_money_prefix'=>'', |
||
| 150 | 'column_money_precision'=>'', |
||
| 151 | 'column_money_thousand_separator'=>'', |
||
| 152 | 'column_money_decimal_separator'=>'', |
||
| 153 | 'column_option_value'=> "", |
||
| 154 | 'column_option_display'=> "", |
||
| 155 | 'column_option_sql_condition'=> "", |
||
| 156 | 'column_options'=> [], |
||
| 157 | 'column_sql_query'=> "", |
||
| 158 | 'column_help'=> "", |
||
| 159 | 'column_mandatory'=> $columnMandatory, |
||
| 160 | 'column_browse'=> "on", |
||
| 161 | 'column_detail'=> "on", |
||
| 162 | 'column_edit'=> $columnEdit, |
||
| 163 | 'column_add'=> $columnAdd, |
||
| 164 | 'listTableColumns'=> [] |
||
| 165 | ]; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | return response()->json($result); |
||
| 169 | } |
||
| 170 | |||
| 171 | private function findComposer() |
||
| 172 | { |
||
| 173 | if (file_exists(getcwd().'/composer.phar')) { |
||
| 174 | return '"'.PHP_BINARY.'" '.getcwd().'/composer.phar'; |
||
| 175 | } |
||
| 176 | |||
| 177 | return 'composer'; |
||
| 178 | } |
||
| 179 | |||
| 180 | private function composingAutoLoad() |
||
| 181 | { |
||
| 182 | $composer = $this->findComposer(); |
||
| 183 | $process = new Process($composer.' dump-autoload'); |
||
|
|
|||
| 184 | $process->setWorkingDirectory(base_path())->run(); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function postCreateMigration() |
||
| 188 | { |
||
| 189 | try { |
||
| 190 | cb()->validation(['table_name','structures']); |
||
| 191 | |||
| 192 | $tableName = request("table_name"); |
||
| 193 | |||
| 194 | if(!Schema::hasTable($tableName)) { |
||
| 195 | $filenameMigration = date("Y_m_d_His")."_".$tableName.".php"; |
||
| 196 | $createTemplate = file_get_contents(base_path("vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs/create.stub")); |
||
| 197 | $className = Str::studly($tableName); |
||
| 198 | $createTemplate = str_replace("DummyClass", $className, $createTemplate); |
||
| 199 | $createTemplate = str_replace("DummyTable", $tableName, $createTemplate); |
||
| 200 | $createTemplate = str_replace("\$table->increments('id');","",$createTemplate); |
||
| 201 | |||
| 202 | $structureItems = ""; |
||
| 203 | |||
| 204 | if(request("timestamp")) { |
||
| 205 | $structureItems .= "\$table->timestamps();\n\t\t\t"; |
||
| 206 | } |
||
| 207 | |||
| 208 | if(request("soft_deletes")) { |
||
| 209 | $structureItems .= "\$table->softDeletes();\n\t\t\t"; |
||
| 210 | } |
||
| 211 | |||
| 212 | foreach(request("structures") as $item) { |
||
| 213 | |||
| 214 | $nullable = ""; |
||
| 215 | |||
| 216 | if(!in_array($item['type_data'],["bigIncrements","increments","mediumIncrements","smallIncrements"])) { |
||
| 217 | $nullable = "->nullable()"; |
||
| 218 | } |
||
| 219 | |||
| 220 | if($item['length']) { |
||
| 221 | $structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."', ".$item['length'].")$nullable;\n\t\t\t"; |
||
| 222 | }else{ |
||
| 223 | $structureItems .= "\$table->".$item['type_data']."('".$item['field_name']."')$nullable;\n\t\t\t"; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | $createTemplate = str_replace("\$table->timestamps();", $structureItems, $createTemplate); |
||
| 227 | |||
| 228 | // Put File onto the migration folders |
||
| 229 | file_put_contents(database_path("migrations/".$filenameMigration), $createTemplate); |
||
| 230 | |||
| 231 | // Composing |
||
| 232 | $this->composingAutoLoad(); |
||
| 233 | |||
| 234 | // Migrate |
||
| 235 | Artisan::call("migrate"); |
||
| 236 | } else { |
||
| 237 | throw new \Exception("The table $tableName has already exists!"); |
||
| 238 | } |
||
| 239 | |||
| 240 | } catch (CBValidationException $e) { |
||
| 241 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 242 | } catch (\Exception $e) { |
||
| 243 | return response()->json(['status'=>false,'message'=>$e->getMessage()]); |
||
| 244 | } |
||
| 245 | |||
| 246 | return response()->json(['status'=>true,'message'=>'Migration successfully!']); |
||
| 247 | } |
||
| 248 | |||
| 249 | public function postCheckExistModule() |
||
| 263 | } |
||
| 264 | |||
| 265 | public function postAddSave() { |
||
| 277 | } |
||
| 278 | |||
| 279 | public function getDelete($id) { |
||
| 280 | $module = cb()->find("cb_modules",$id); |
||
| 281 | @unlink(app_path("Http/Controllers/".$module->controller.".php")); |
||
| 282 | DB::table("cb_modules")->where("id", $id)->delete(); |
||
| 283 | DB::table("cb_menus")->where("cb_modules_id", $id)->delete(); |
||
| 284 | return cb()->redirectBack("The module has been deleted!","success"); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function getDeleteSoft($id) { |
||
| 292 | } |
||
| 293 | |||
| 294 | } |