| Total Complexity | 64 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CBController 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 CBController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace crocodicstudio\crudbooster\controllers; |
||
| 14 | class CBController extends Controller |
||
| 15 | { |
||
| 16 | use ColumnsRegister, Join, ControllerSetting; |
||
| 17 | |||
| 18 | private $assignmentData; |
||
|
|
|||
| 19 | |||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | columnSingleton()->newColumns(); |
||
| 23 | $this->cbInit(); |
||
| 24 | $this->data['columns'] = columnSingleton()->getColumns(); |
||
| 25 | view()->share($this->data); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function __call($method, $parameters) |
||
| 29 | { |
||
| 30 | if($method == "getData") { |
||
| 31 | $key = $parameters[0]; |
||
| 32 | if(isset($this->data[$key])) { |
||
| 33 | return $this->data[$key]; |
||
| 34 | }else{ |
||
| 35 | return null; |
||
| 36 | } |
||
| 37 | }else{ |
||
| 38 | return null; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | private function repository() |
||
| 43 | { |
||
| 44 | $joins = columnSingleton()->getJoin(); |
||
| 45 | $columns = columnSingleton()->getColumns(); |
||
| 46 | |||
| 47 | $query = DB::table($this->data['table']); |
||
| 48 | |||
| 49 | |||
| 50 | $query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key'); |
||
| 51 | |||
| 52 | if(isset($this->data['hook_query_index']) && is_callable($this->data['hook_query_index'])) |
||
| 53 | { |
||
| 54 | $query = call_user_func($this->data['hook_query_index'], $query); |
||
| 55 | } |
||
| 56 | |||
| 57 | $softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
||
| 58 | if($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { |
||
| 59 | $query->whereNull($this->data['table'].'.deleted_at'); |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | if(isset($joins)) { |
||
| 64 | foreach($joins as $join) |
||
| 65 | { |
||
| 66 | $query->join($join['target_table'], |
||
| 67 | $join['target_table_primary'], |
||
| 68 | $join['operator'], |
||
| 69 | $join['source_table_foreign'], |
||
| 70 | $join['type']); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach($columns as $column) { |
||
| 75 | /** @var ColumnModel $column */ |
||
| 76 | if(strpos($column->getField(),".") === false) { |
||
| 77 | $query->addSelect($this->data['table'].'.'.$column->getField()); |
||
| 78 | }else{ |
||
| 79 | $query->addSelect($column->getField()); |
||
| 80 | } |
||
| 81 | |||
| 82 | $query = getTypeHook($column->getType())->query($query, $column); |
||
| 83 | } |
||
| 84 | |||
| 85 | if(request()->has('q')) |
||
| 86 | { |
||
| 87 | if(isset($this->data['hook_search_query'])) { |
||
| 88 | $query = call_user_func($this->data['hook_search_query'], $query); |
||
| 89 | }else{ |
||
| 90 | $query->where(function ($where) use ($columns) { |
||
| 91 | /** |
||
| 92 | * @var $where Builder |
||
| 93 | */ |
||
| 94 | foreach($columns as $column) |
||
| 95 | { |
||
| 96 | $where->orWhere($column['name'], 'like', '%'.request('q').'%'); |
||
| 97 | } |
||
| 98 | }); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | if(isset($this->data['hook_query_index']) && is_callable($this->data['hook_query_index'])) { |
||
| 103 | $query = call_user_func($this->data['hook_query_index'], $query); |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 107 | if(request()->has(['order_by','order_sort'])) |
||
| 108 | { |
||
| 109 | if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) { |
||
| 110 | $query->orderBy(request('order_by'), request('order_sort')); |
||
| 111 | } |
||
| 112 | }else{ |
||
| 113 | if(isset($this->data['order_by'])) { |
||
| 114 | $query->orderBy($this->data['order_by'][0], $this->data['order_by'][1]); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $query; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getIndex() |
||
| 122 | { |
||
| 123 | if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),"You do not have access to this area"); |
||
| 124 | |||
| 125 | $query = $this->repository(); |
||
| 126 | $result = $query->paginate(20); |
||
| 127 | $data['result'] = $result; |
||
| 128 | return view("crudbooster::module.index.index", $data); |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * @throws CBValidationException |
||
| 134 | */ |
||
| 135 | private function validation() |
||
| 136 | { |
||
| 137 | if(isset($this->data['validation'])) { |
||
| 138 | $validator = Validator::make(request()->all(), @$this->data['validation'], @$this->data['validation_messages']); |
||
| 139 | if ($validator->fails()) { |
||
| 140 | $message = $validator->messages(); |
||
| 141 | $message_all = $message->all(); |
||
| 142 | throw new CBValidationException(implode(', ',$message_all)); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getAdd() |
||
| 148 | { |
||
| 149 | if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have access to this area"); |
||
| 150 | |||
| 151 | $data = []; |
||
| 152 | $data['page_title'] = $this->data['page_title'].' : Add'; |
||
| 153 | $data['action_url'] = module()->addSaveURL(); |
||
| 154 | return view('crudbooster::module.form.form',$data); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function postAddSave() |
||
| 158 | { |
||
| 159 | if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have access to this area"); |
||
| 160 | |||
| 161 | try { |
||
| 162 | $this->validation(); |
||
| 163 | columnSingleton()->valueAssignment(); |
||
| 164 | $data = columnSingleton()->getAssignmentData(); |
||
| 165 | |||
| 166 | if(Schema::hasColumn($this->data['table'], 'created_at')) { |
||
| 167 | $data['created_at'] = date('Y-m-d H:i:s'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $id = DB::table($this->data['table'])->insertGetId($data); |
||
| 171 | |||
| 172 | if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
||
| 173 | call_user_func($this->data['hook_after_insert'], $id); |
||
| 174 | } |
||
| 175 | |||
| 176 | } catch (CBValidationException $e) { |
||
| 177 | Log::debug($e); |
||
| 178 | return cb()->redirectBack($e->getMessage(),'info'); |
||
| 179 | } catch (\Exception $e) { |
||
| 180 | Log::error($e); |
||
| 181 | return cb()->redirectBack($e->getMessage(),'warning'); |
||
| 182 | } |
||
| 183 | |||
| 184 | if (request('submit') == trans('crudbooster.button_save_more')) { |
||
| 185 | return cb()->redirect(module()->addURL(), trans("crudbooster.alert_add_data_success"), 'success'); |
||
| 186 | } else { |
||
| 187 | return cb()->redirect(module()->url(), trans("crudbooster.alert_add_data_success"), 'success'); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | public function getEdit($id) |
||
| 192 | { |
||
| 193 | if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),"You do not have access to this area"); |
||
| 194 | |||
| 195 | $data = []; |
||
| 196 | $data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
||
| 197 | $data['page_title'] = $this->data['page_title'].' : Edit'; |
||
| 198 | $data['action_url'] = module()->editSaveURL($id); |
||
| 199 | return view('crudbooster::module.form.form', $data); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function postEditSave($id) |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | public function getDelete($id) |
||
| 267 | } |
||
| 268 | |||
| 269 | public function getDetail($id) |
||
| 277 | } |
||
| 278 | |||
| 279 | public function postUploadFile() |
||
| 303 | ]); |
||
| 304 | } |
||
| 305 | } |
||
| 306 |