| Conditions | 20 |
| Paths | 1440 |
| Total Lines | 83 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace crocodicstudio\crudbooster\controllers; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getAdd() |
||
| 45 | { |
||
| 46 | if(!module()->canCreate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
||
| 47 | |||
| 48 | $data = []; |
||
| 49 | $data['page_title'] = $this->data['page_title'].' : '.cbLang('add'); |
||
| 50 | $data['action_url'] = module()->addSaveURL(); |
||
| 51 | return view('crudbooster::module.form.form',array_merge($data, $this->data)); |
||
| 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) |
||
| 115 | { |
||
| 116 | if(!module()->canUpdate()) return cb()->redirect(cb()->getAdminUrl(),cbLang("you_dont_have_privilege_to_this_area")); |
||
| 117 | |||
| 118 | try { |
||
| 119 | $this->validation(); |
||
| 120 | columnSingleton()->valueAssignment(); |
||
| 121 | $data = columnSingleton()->getAssignmentData(); |
||
| 122 | |||
| 123 | // Make sure the Primary Key is not included |
||
| 124 | unset($data[ cb()->pk($this->data['table']) ]); |
||
| 125 | |||
| 223 |