| Total Complexity | 43 |
| Total Lines | 252 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Settings_WebserviceUsers_ManageConsents_Service 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 Settings_WebserviceUsers_ManageConsents_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Settings_WebserviceUsers_ManageConsents_Service extends Settings_WebserviceUsers_Record_Model |
||
| 14 | { |
||
| 15 | /** {@inheritdoc} */ |
||
| 16 | public $baseTable = 'w_#__manage_consents_user'; |
||
| 17 | |||
| 18 | /** {@inheritdoc} */ |
||
| 19 | public $baseIndex = 'id'; |
||
| 20 | |||
| 21 | /** {@inheritdoc} */ |
||
| 22 | public $editFields = [ |
||
| 23 | 'server_id' => 'FL_SERVER', |
||
| 24 | 'status' => 'FL_STATUS', |
||
| 25 | 'type' => 'FL_TYPE', |
||
| 26 | 'language' => 'FL_LANGUAGE', |
||
| 27 | 'user_id' => 'FL_USER' |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** {@inheritdoc} */ |
||
| 31 | public $listFields = [ |
||
| 32 | 'server_id' => 'FL_SERVER', |
||
| 33 | 'status' => 'FL_STATUS', |
||
| 34 | 'user_id' => 'FL_USER', |
||
| 35 | 'type' => 'FL_TYPE', |
||
| 36 | 'login_time' => 'FL_LOGIN_TIME', |
||
| 37 | 'language' => 'FL_LANGUAGE' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Function determines fields available in edition view. |
||
| 42 | * |
||
| 43 | * @param mixed $name |
||
| 44 | * |
||
| 45 | * @return string[] |
||
| 46 | */ |
||
| 47 | public function getFieldInstanceByName($name) |
||
| 88 | } |
||
| 89 | |||
| 90 | /** {@inheritdoc} */ |
||
| 91 | public function save() |
||
| 92 | { |
||
| 93 | $db = App\Db::getInstance('webservice'); |
||
| 94 | $table = $this->baseTable; |
||
| 95 | $index = $this->baseIndex; |
||
| 96 | $data = $this->getDataForSave(); |
||
| 97 | $success = true; |
||
| 98 | if (empty($this->getId())) { |
||
| 99 | $success = $db->createCommand()->insert($table, $data)->execute(); |
||
| 100 | if ($success) { |
||
| 101 | $this->set('id', $db->getLastInsertID("{$table}_{$index}_seq")); |
||
| 102 | } |
||
| 103 | } elseif ($data) { |
||
| 104 | $success = $db->createCommand()->update($table, $data, [$index => $this->getId()])->execute(); |
||
| 105 | } |
||
| 106 | return (bool) $success; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Sets data from request. |
||
| 111 | * |
||
| 112 | * @param App\Request $request |
||
| 113 | */ |
||
| 114 | public function setDataFromRequest(App\Request $request) |
||
| 115 | { |
||
| 116 | foreach (array_keys($this->getEditFields()) as $field) { |
||
| 117 | if ($request->has($field)) { |
||
| 118 | switch ($field) { |
||
| 119 | case 'server_id': |
||
| 120 | case 'status': |
||
| 121 | case 'type': |
||
| 122 | case 'user_id': |
||
| 123 | $value = $request->getInteger($field); |
||
| 124 | break; |
||
| 125 | case 'language': |
||
| 126 | $value = $request->getByType($field, 'Text'); |
||
| 127 | break; |
||
| 128 | default: |
||
| 129 | throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||{$field}", 406); |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | $this->set($field, $this->getValueToSave($field, $value)); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** {@inheritdoc} */ |
||
| 138 | public function getFieldsForSave() |
||
| 143 | } |
||
| 144 | |||
| 145 | /** {@inheritdoc} */ |
||
| 146 | public function getDataForSave() |
||
| 147 | { |
||
| 148 | if ($this->isNew()) { |
||
| 149 | $this->set('token', \App\Fields\Token::generateToken()); |
||
| 150 | } |
||
| 151 | return array_intersect_key($this->getData(), $this->getFieldsForSave()); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Function formats data for saving. |
||
| 156 | * |
||
| 157 | * @param string $key |
||
| 158 | * @param mixed $value |
||
| 159 | * |
||
| 160 | * @return int|string |
||
| 161 | */ |
||
| 162 | public function getValueToSave($key, $value) |
||
| 163 | { |
||
| 164 | switch ($key) { |
||
| 165 | case 'server_id': |
||
| 166 | case 'status': |
||
| 167 | case 'type': |
||
| 168 | case 'user_id': |
||
| 169 | $value = (int) $value; |
||
| 170 | break; |
||
| 171 | default: |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | return $value; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Function to get the Display Value, for the current field type with given DB Insert Value. |
||
| 179 | * |
||
| 180 | * @param string $name |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getDisplayValue($name) |
||
| 185 | { |
||
| 186 | switch ($name) { |
||
| 187 | case 'server_id': |
||
| 188 | $servers = Settings_WebserviceApps_Record_Model::getInstanceById($this->get($name)); |
||
| 189 | $value = $servers ? $servers->getName() : '<span class="redColor">ERROR</span>'; |
||
| 190 | break; |
||
| 191 | case 'status': |
||
| 192 | $value = \App\Language::translate((empty($this->get($name)) ? 'FL_INACTIVE' : 'FL_ACTIVE')); |
||
| 193 | break; |
||
| 194 | case 'user_id': |
||
| 195 | $value = \App\Fields\Owner::getLabel($this->get($name)); |
||
| 196 | break; |
||
| 197 | case 'language': |
||
| 198 | $value = $this->get($name) ? \App\Language::getLanguageLabel($this->get($name)) : ''; |
||
| 199 | break; |
||
| 200 | case 'type': |
||
| 201 | $label = \App\Language::translate($this->getTypeValues($this->get($name)), $this->getModule()->getName(true)); |
||
| 202 | $value = \App\TextParser::textTruncate($label); |
||
| 203 | break; |
||
| 204 | default: |
||
| 205 | $value = $this->get($name); |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | return $value; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Function to get the list view actions for the record. |
||
| 213 | * |
||
| 214 | * @return Vtiger_Link_Model[] - Associate array of Vtiger_Link_Model instances |
||
| 215 | */ |
||
| 216 | public function getRecordLinks() |
||
| 217 | { |
||
| 218 | $links = []; |
||
| 219 | $recordLinks = [ |
||
| 220 | [ |
||
| 221 | 'linktype' => 'LISTVIEWRECORD', |
||
| 222 | 'linklabel' => 'FL_TOKEN', |
||
| 223 | 'linkicon' => 'fas fa-copy', |
||
| 224 | 'linkclass' => 'btn btn-sm btn-primary clipboard', |
||
| 225 | 'linkdata' => ['copy-attribute' => 'clipboard-text', 'clipboard-text' => \App\Purifier::encodeHtml($this->get('token'))] |
||
| 226 | ], |
||
| 227 | [ |
||
| 228 | 'linktype' => 'LISTVIEWRECORD', |
||
| 229 | 'linklabel' => 'LBL_EDIT_RECORD', |
||
| 230 | 'linkurl' => $this->getModule()->getEditViewUrl() . '&record=' . $this->getId(), |
||
| 231 | 'linkicon' => 'yfi yfi-full-editing-view', |
||
| 232 | 'linkclass' => 'btn btn-sm btn-primary', |
||
| 233 | 'modalView' => true, |
||
| 234 | ], |
||
| 235 | [ |
||
| 236 | 'linktype' => 'LISTVIEWRECORD', |
||
| 237 | 'linklabel' => 'LBL_DELETE_RECORD', |
||
| 238 | 'linkurl' => 'javascript:Settings_WebserviceUsers_List_Js.deleteById(' . $this->getId() . ');', |
||
| 239 | 'linkicon' => 'fas fa-trash-alt', |
||
| 240 | 'linkclass' => 'btn btn-sm btn-danger', |
||
| 241 | ], |
||
| 242 | ]; |
||
| 243 | foreach ($recordLinks as $recordLink) { |
||
| 244 | $links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink); |
||
| 245 | } |
||
| 246 | return $links; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Type field values. |
||
| 251 | * |
||
| 252 | * @param type $value |
||
| 253 | * |
||
| 254 | * @return string|string[] |
||
| 255 | */ |
||
| 256 | public function getTypeValues($value = false) |
||
| 265 | } |
||
| 266 | } |
||
| 267 |