Total Complexity | 45 |
Total Lines | 201 |
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; |
||
19 | class CBController extends Controller |
||
20 | { |
||
21 | use ColumnsRegister, Join, ControllerSetting, Validation, Query, SubModuleController, ColumnIntervention; |
||
|
|||
22 | |||
23 | private $assignmentData; |
||
24 | |||
25 | public function __construct() |
||
26 | { |
||
27 | columnSingleton()->newColumns(); |
||
28 | $this->defaultData(); |
||
29 | $this->cbInit(); |
||
30 | $this->columnIntervention(); |
||
31 | } |
||
32 | |||
33 | public function getIndex() |
||
34 | { |
||
35 | if(!module()->canBrowse()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
||
36 | |||
37 | $query = $this->repository(); |
||
38 | $result = $query->paginate( request("limit")?:cbConfig("LIMIT_TABLE_DATA") ); |
||
39 | $data['result'] = $result; |
||
40 | |||
41 | return view("crudbooster::module.index.index", array_merge($data, $this->data)); |
||
42 | } |
||
43 | |||
44 | public function getAdd() |
||
52 | } |
||
53 | |||
54 | public function postAddSave() |
||
55 | { |
||
56 | if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
||
57 | |||
58 | try { |
||
59 | $this->validation(); |
||
60 | columnSingleton()->valueAssignment(); |
||
61 | $data = columnSingleton()->getAssignmentData(); |
||
62 | |||
63 | //Clear data from Primary Key |
||
64 | unset($data[ cb()->pk($this->data['table']) ]); |
||
65 | |||
66 | if(Schema::hasColumn($this->data['table'], 'created_at')) { |
||
67 | $data['created_at'] = date('Y-m-d H:i:s'); |
||
68 | } |
||
69 | |||
70 | if(Schema::hasColumn($this->data['table'], 'updated_at')) { |
||
71 | $data['updated_at'] = date('Y-m-d H:i:s'); |
||
72 | } |
||
73 | |||
74 | if(isset($this->data['hook_before_insert']) && is_callable($this->data['hook_before_insert'])) { |
||
75 | $data = call_user_func($this->data['hook_before_insert'], $data); |
||
76 | } |
||
77 | |||
78 | $id = DB::table($this->data['table'])->insertGetId($data); |
||
79 | |||
80 | if(isset($this->data['hook_after_insert']) && is_callable($this->data['hook_after_insert'])) { |
||
81 | call_user_func($this->data['hook_after_insert'], $id); |
||
82 | } |
||
83 | |||
84 | } catch (CBValidationException $e) { |
||
85 | Log::debug($e); |
||
86 | return cb()->redirectBack($e->getMessage(),'info'); |
||
87 | } catch (\Exception $e) { |
||
88 | Log::error($e); |
||
89 | return cb()->redirectBack(cbLang("something_went_wrong"),'warning'); |
||
90 | } |
||
91 | |||
92 | if (Str::contains(request("submit"),cbLang("more"))) { |
||
93 | return cb()->redirectBack(cbLang("the_data_has_been_added"), 'success'); |
||
94 | } else { |
||
95 | if(verifyReferalUrl()) { |
||
96 | return cb()->redirect(getReferalUrl("url"), cbLang("the_data_has_been_added"), 'success'); |
||
97 | } else { |
||
98 | return cb()->redirect(module()->url(), cbLang("the_data_has_been_added"), 'success'); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public function getEdit($id) |
||
104 | { |
||
105 | if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
||
106 | |||
107 | $data = []; |
||
108 | $data['row'] = $this->repository()->where($this->data['table'].'.'.getPrimaryKey($this->data['table']), $id)->first(); |
||
109 | $data['page_title'] = $this->data['page_title'].' : '.cbLang('edit'); |
||
110 | $data['action_url'] = module()->editSaveURL($id); |
||
111 | return view('crudbooster::module.form.form', array_merge($data, $this->data)); |
||
112 | } |
||
113 | |||
114 | public function postEditSave($id) |
||
158 | } |
||
159 | |||
160 | } |
||
161 | } |
||
162 | |||
163 | public function getDelete($id) |
||
188 | } |
||
189 | |||
190 | public function getDetail($id) |
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * @param string $method |
||
204 | * @param array $parameters |
||
205 | * @return mixed|null |
||
206 | * |
||
207 | * This method is to get the data columns and retrieve the value from this class |
||
208 | */ |
||
209 | public function __call($method, $parameters) |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 |