| Total Complexity | 73 | 
| Total Lines | 313 | 
| Duplicated Lines | 0 % | 
| Changes | 16 | ||
| Bugs | 3 | Features | 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;  | 
            ||
| 15 | class CBController extends Controller  | 
            ||
| 16 | { | 
            ||
| 17 | use ColumnsRegister, Join, ControllerSetting;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 18 | |||
| 19 | private $assignmentData;  | 
            ||
| 20 | |||
| 21 | public function __construct()  | 
            ||
| 22 |     { | 
            ||
| 23 | columnSingleton()->newColumns();  | 
            ||
| 24 | $this->defaultData();  | 
            ||
| 25 | $this->cbInit();  | 
            ||
| 26 | }  | 
            ||
| 27 | |||
| 28 | public function __call($method, $parameters)  | 
            ||
| 39 | }  | 
            ||
| 40 | }  | 
            ||
| 41 | |||
| 42 | private function repository($callback = null)  | 
            ||
| 43 |     { | 
            ||
| 44 | $joins = columnSingleton()->getJoin();  | 
            ||
| 45 | $columns = columnSingleton()->getColumns();  | 
            ||
| 46 | |||
| 47 | $query = DB::table($this->data['table']);  | 
            ||
| 48 | |||
| 49 | $query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key');  | 
            ||
| 50 | |||
| 51 | $softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true;  | 
            ||
| 52 |         if($softDelete === true && Schema::hasColumn($this->data['table'],'deleted_at')) { | 
            ||
| 53 | $query->whereNull($this->data['table'].'.deleted_at');  | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 |         if(isset($joins)) { | 
            ||
| 57 | foreach($joins as $join)  | 
            ||
| 58 |             { | 
            ||
| 59 | $query->join($join['target_table'],  | 
            ||
| 60 | $join['target_table_primary'],  | 
            ||
| 61 | $join['operator'],  | 
            ||
| 62 | $join['source_table_foreign'],  | 
            ||
| 63 | $join['type']);  | 
            ||
| 64 | }  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 |         foreach($columns as $column) { | 
            ||
| 68 | /** @var ColumnModel $column */  | 
            ||
| 69 |             if($column->getType() != "custom") { | 
            ||
| 70 |                 if(strpos($column->getField(),".") === false) { | 
            ||
| 71 |                     if(Schema::hasColumn($this->data['table'], $column->getField())) { | 
            ||
| 72 | $query->addSelect($this->data['table'].'.'.$column->getField());  | 
            ||
| 73 | }  | 
            ||
| 74 |                 }else{ | 
            ||
| 75 | $query->addSelect($column->getField());  | 
            ||
| 76 | }  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | $query = getTypeHook($column->getType())->query($query, $column);  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 |         if(request()->has('q')) | 
            ||
| 83 |         { | 
            ||
| 84 |             if(isset($this->data['hook_search_query'])) { | 
            ||
| 85 | $query = call_user_func($this->data['hook_search_query'], $query);  | 
            ||
| 86 |             }else{ | 
            ||
| 87 |                 $query->where(function ($where) use ($columns) { | 
            ||
| 88 | /**  | 
            ||
| 89 | * @var $where Builder  | 
            ||
| 90 | */  | 
            ||
| 91 | foreach($columns as $column)  | 
            ||
| 92 |                     { | 
            ||
| 93 |                         if(strpos($column->getField(),".") === false) { | 
            ||
| 94 | $field = $this->data['table'].'.'.$column->getField();  | 
            ||
| 95 |                         }else{ | 
            ||
| 96 | $field = $column->getField();  | 
            ||
| 97 | }  | 
            ||
| 98 |                         $where->orWhere($field, 'like', '%'.request('q').'%'); | 
            ||
| 99 | }  | 
            ||
| 100 | });  | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | |||
| 105 | // Callback From this Method  | 
            ||
| 106 |         if(isset($callback) && is_callable($callback)) { | 
            ||
| 107 | $query = call_user_func($callback, $query);  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 |         if(isset($this->data['hook_index_query']) && is_callable($this->data['hook_index_query'])) { | 
            ||
| 111 | $query = call_user_func($this->data['hook_index_query'], $query);  | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 | |||
| 115 | if(request()->has(['order_by','order_sort']))  | 
            ||
| 116 |         { | 
            ||
| 117 |             if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) { | 
            ||
| 118 |                 $query->orderBy(request('order_by'), request('order_sort')); | 
            ||
| 119 | }  | 
            ||
| 120 |         }else{ | 
            ||
| 121 | $query->orderBy($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), "desc");  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | return $query;  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | public function getIndex()  | 
            ||
| 128 |     { | 
            ||
| 129 |         if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); | 
            ||
| 130 | |||
| 131 | $query = $this->repository();  | 
            ||
| 132 |         $result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); | 
            ||
| 133 | $data['result'] = $result;  | 
            ||
| 134 | |||
| 135 |         return view("crudbooster::module.index.index", array_merge($data, $this->data)); | 
            ||
| 136 | }  | 
            ||
| 137 | |||
| 138 |     public function getFilterBy($field, $value, $parentPath) { | 
            ||
| 139 |         if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); | 
            ||
| 140 | |||
| 141 | if(!verifyReferalUrl()) return cb()->redirect(cb()->getAdminUrl($parentPath),"The url you are trying visit is incorrect");  | 
            ||
| 142 | |||
| 143 | $query = $this->repository();  | 
            ||
| 144 | |||
| 145 | $query->where($field, $value);  | 
            ||
| 146 | |||
| 147 |         $result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); | 
            ||
| 148 | $data['result'] = $result;  | 
            ||
| 149 | |||
| 150 |         $additionalView = getReferalUrl("additional"); | 
            ||
| 151 |         if($additionalView) { | 
            ||
| 152 | $data['additionalView'] = $additionalView;  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 |         return view("crudbooster::module.index.index", array_merge($data, $this->data)); | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * @throws CBValidationException  | 
            ||
| 161 | */  | 
            ||
| 162 | private function validation()  | 
            ||
| 163 |     { | 
            ||
| 164 |         if(isset($this->data['validation'])) { | 
            ||
| 165 | $validator = Validator::make(request()->all(), @$this->data['validation'], @$this->data['validation_messages']);  | 
            ||
| 166 |             if ($validator->fails()) { | 
            ||
| 167 | $message = $validator->messages();  | 
            ||
| 168 | $message_all = $message->all();  | 
            ||
| 169 |                 throw new CBValidationException(implode(', ',$message_all)); | 
            ||
| 170 | }  | 
            ||
| 171 | }  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | public function getAdd()  | 
            ||
| 182 | }  | 
            ||
| 183 | |||
| 184 | public function postAddSave()  | 
            ||
| 185 |     { | 
            ||
| 186 |         if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); | 
            ||
| 187 | |||
| 188 |         try { | 
            ||
| 189 | $this->validation();  | 
            ||
| 190 | columnSingleton()->valueAssignment();  | 
            ||
| 191 | $data = columnSingleton()->getAssignmentData();  | 
            ||
| 192 | |||
| 193 | //Clear data from Primary Key  | 
            ||
| 194 | unset($data[ cb()->pk($this->data['table']) ]);  | 
            ||
| 195 | |||
| 196 |             if(Schema::hasColumn($this->data['table'], 'created_at')) { | 
            ||
| 197 |                 $data['created_at'] = date('Y-m-d H:i:s'); | 
            ||
| 198 | }  | 
            ||
| 199 | |||
| 200 |             if(Schema::hasColumn($this->data['table'], 'updated_at')) { | 
            ||
| 201 |                 $data['updated_at'] = date('Y-m-d H:i:s'); | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 |             if(isset($this->data['hook_before_insert']) && is_callable($this->data['hook_before_insert'])) { | 
            ||
| 205 | $data = call_user_func($this->data['hook_before_insert'], $data);  | 
            ||
| 206 | }  | 
            ||
| 207 | |||
| 208 | $id = DB::table($this->data['table'])->insertGetId($data);  | 
            ||
| 209 | |||
| 210 |             if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { | 
            ||
| 211 | call_user_func($this->data['hook_after_insert'], $id);  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 |         } catch (CBValidationException $e) { | 
            ||
| 215 | Log::debug($e);  | 
            ||
| 216 | return cb()->redirectBack($e->getMessage(),'info');  | 
            ||
| 217 |         } catch (\Exception $e) { | 
            ||
| 218 | Log::error($e);  | 
            ||
| 219 |             return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 |         if (Str::contains(request("submit"),cbLang("more"))) { | 
            ||
| 223 |             return cb()->redirect(module()->addURL(), cbLang("the_data_has_been_added"), 'success'); | 
            ||
| 224 |         } else { | 
            ||
| 225 |             if(verifyReferalUrl()) { | 
            ||
| 226 |                 return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_added"), 'success'); | 
            ||
| 227 |             } else { | 
            ||
| 228 |                 return cb()->redirect(module()->url(), cbLang("the_data_has_been_added"), 'success'); | 
            ||
| 229 | }  | 
            ||
| 230 | }  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | public function getEdit($id)  | 
            ||
| 234 |     { | 
            ||
| 235 |         if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); | 
            ||
| 236 | |||
| 237 | $data = [];  | 
            ||
| 238 | $data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first();  | 
            ||
| 239 |         $data['page_title'] = $this->data['page_title'].' : '.cbLang('edit'); | 
            ||
| 240 | $data['action_url'] = module()->editSaveURL($id);  | 
            ||
| 241 |         return view('crudbooster::module.form.form', array_merge($data, $this->data)); | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | public function postEditSave($id)  | 
            ||
| 288 | }  | 
            ||
| 289 | |||
| 290 | }  | 
            ||
| 291 | }  | 
            ||
| 292 | |||
| 293 | public function getDelete($id)  | 
            ||
| 318 | }  | 
            ||
| 319 | |||
| 320 | public function getDetail($id)  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | }  | 
            ||
| 331 |