| Total Complexity | 56 |
| Total Lines | 476 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LegalManager 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 LegalManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class LegalManager |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Constructor. |
||
| 12 | */ |
||
| 13 | public function __construct() |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Add a new Term and Condition. |
||
| 19 | * |
||
| 20 | * @param int $language language id |
||
| 21 | * @param string $content content |
||
| 22 | * @param int $type term and condition type (0 for HTML text or 1 for link to another page) |
||
| 23 | * @param string $changes explain changes |
||
| 24 | * @param array $extraFieldValuesToSave |
||
| 25 | * |
||
| 26 | * @return int |
||
| 27 | */ |
||
| 28 | public static function add($language, $content, $type, $changes, $extraFieldValuesToSave = []) |
||
| 29 | { |
||
| 30 | $legalTable = Database::get_main_table(TABLE_MAIN_LEGAL); |
||
| 31 | $last = self::get_last_condition($language); |
||
| 32 | |||
| 33 | if (false === $last) { |
||
| 34 | return 0; |
||
| 35 | } |
||
| 36 | |||
| 37 | $type = (int) $type; |
||
| 38 | $time = time(); |
||
| 39 | |||
| 40 | $changeList = []; |
||
| 41 | |||
| 42 | if (isset($last['id'])) { |
||
| 43 | $id = $last['id']; |
||
| 44 | |||
| 45 | // Check if extra fields changed |
||
| 46 | $extraFieldValue = new ExtraFieldValue('terms_and_condition'); |
||
| 47 | $values = $extraFieldValue->getAllValuesByItem($id); |
||
| 48 | $oldValues = array_column($values, 'value', 'variable'); |
||
| 49 | foreach ($extraFieldValuesToSave as $key => $value) { |
||
| 50 | if (is_numeric(strpos($key, 'extra_'))) { |
||
| 51 | $replace = str_replace('extra_', '', $key); |
||
| 52 | if (isset($oldValues[$replace])) { |
||
| 53 | if ($value != $oldValues[$replace]) { |
||
| 54 | $changeList[] = $replace; |
||
| 55 | } |
||
| 56 | } else { |
||
| 57 | // It means there's a new extra field that was not included before. |
||
| 58 | $changeList[] = $replace; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | if ((isset($last['content']) && $last['content'] != $content) || !empty($changeList) || empty($last)) { |
||
| 65 | $version = self::getLastVersion($language); |
||
| 66 | $version++; |
||
| 67 | $params = [ |
||
| 68 | 'language_id' => $language, |
||
| 69 | 'content' => $content, |
||
| 70 | 'changes' => $changes, |
||
| 71 | 'type' => $type, |
||
| 72 | 'version' => $version, |
||
| 73 | 'date' => $time, |
||
| 74 | ]; |
||
| 75 | |||
| 76 | $id = Database::insert($legalTable, $params); |
||
| 77 | |||
| 78 | self::updateExtraFields($id, $extraFieldValuesToSave); |
||
| 79 | |||
| 80 | return $id; |
||
|
|
|||
| 81 | } elseif ($last['type'] != $type && $language == $last['language_id']) { |
||
| 82 | // Update |
||
| 83 | $id = $last['id']; |
||
| 84 | $params = [ |
||
| 85 | 'changes' => $changes, |
||
| 86 | 'type' => $type, |
||
| 87 | 'date' => $time, |
||
| 88 | ]; |
||
| 89 | Database::update($legalTable, $params, ['id = ?' => $id]); |
||
| 90 | self::updateExtraFields($id, $extraFieldValuesToSave); |
||
| 91 | |||
| 92 | return $id; |
||
| 93 | } |
||
| 94 | |||
| 95 | return 0; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param int $itemId |
||
| 100 | * @param array $values |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public static function updateExtraFields($itemId, $values) |
||
| 105 | { |
||
| 106 | if (empty($itemId)) { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | $extraFieldValues = new ExtraFieldValue('terms_and_condition'); |
||
| 110 | $values['item_id'] = $itemId; |
||
| 111 | $extraFieldValues->saveFieldValues($values); |
||
| 112 | |||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param int $id |
||
| 118 | */ |
||
| 119 | public static function delete($id) |
||
| 121 | /* |
||
| 122 | $legalTable = Database::get_main_table(TABLE_MAIN_LEGAL); |
||
| 123 | $id = (int) $id; |
||
| 124 | $sql = "DELETE FROM $legalTable WHERE id = '".$id."'"; |
||
| 125 | */ |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the last version of a Term and condition by language. |
||
| 130 | * |
||
| 131 | * @param int $language language id |
||
| 132 | * |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public static function getLastVersion($language) |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the data of a Term and condition by language. |
||
| 153 | * |
||
| 154 | * @param int $language language id |
||
| 155 | * |
||
| 156 | * @return array all the info of a Term and condition |
||
| 157 | */ |
||
| 158 | public static function get_last_condition($language) |
||
| 159 | { |
||
| 160 | $table = Database::get_main_table(TABLE_MAIN_LEGAL); |
||
| 161 | $language = (int) $language; |
||
| 162 | $sql = "SELECT * FROM $table |
||
| 163 | WHERE language_id = $language |
||
| 164 | ORDER BY version DESC |
||
| 165 | LIMIT 1 "; |
||
| 166 | $result = Database::query($sql); |
||
| 167 | $result = Database::fetch_assoc($result); |
||
| 168 | |||
| 169 | if (isset($result['content'])) { |
||
| 170 | $result['content'] = self::replaceTags($result['content']); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $result; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if an specific version of an agreement exists. |
||
| 178 | * |
||
| 179 | * @param int $language |
||
| 180 | * @param int $version |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public static function hasVersion($language, $version) |
||
| 185 | { |
||
| 186 | $table = Database::get_main_table(TABLE_MAIN_LEGAL); |
||
| 187 | $language = (int) $language; |
||
| 188 | $version = (int) $version; |
||
| 189 | |||
| 190 | if (empty($language)) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | $sql = "SELECT version FROM $table |
||
| 195 | WHERE |
||
| 196 | language_id = $language AND |
||
| 197 | version = $version |
||
| 198 | LIMIT 1 "; |
||
| 199 | $result = Database::query($sql); |
||
| 200 | if (Database::num_rows($result) > 0) { |
||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | return false; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $content |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public static function replaceTags($content) |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Gets the last version of a Term and condition by language. |
||
| 232 | * |
||
| 233 | * @param int $language language id |
||
| 234 | * |
||
| 235 | * @return bool | int the version or false if does not exist |
||
| 236 | */ |
||
| 237 | public static function get_last_version($language) |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Show the last condition. |
||
| 258 | * |
||
| 259 | * @param array $term_preview with type and content i.e array('type'=>'1', 'content'=>'hola'); |
||
| 260 | * |
||
| 261 | * @return string html preview |
||
| 262 | */ |
||
| 263 | public static function show_last_condition($term_preview) |
||
| 264 | { |
||
| 265 | $preview = ''; |
||
| 266 | switch ($term_preview['type']) { |
||
| 267 | case 0: |
||
| 268 | if (!empty($term_preview['content'])) { |
||
| 269 | $preview = '<div class="terms-conditions"> |
||
| 270 | <div id="legal-terms" class="scrollbar-inner">'.$term_preview['content'].'</div> |
||
| 271 | </div>'; |
||
| 272 | } |
||
| 273 | $preview .= get_lang('By clicking on \'Register\' below you are agreeing to the Terms and Conditions'); |
||
| 274 | break; |
||
| 275 | // Page link |
||
| 276 | case 1: |
||
| 277 | $preview = '<fieldset> |
||
| 278 | <legend>'.get_lang('Terms and Conditions').'</legend>'; |
||
| 279 | $preview .= '<div id="legal-accept-wrapper" class="form-item"> |
||
| 280 | <label class="option" for="legal-accept"> |
||
| 281 | <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/> |
||
| 282 | '.get_lang('I have read and agree to the').' |
||
| 283 | <a href="#">'.get_lang('Terms and Conditions').'</a> |
||
| 284 | </label> |
||
| 285 | </div> |
||
| 286 | </fieldset>'; |
||
| 287 | break; |
||
| 288 | default: |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | |||
| 292 | return $preview; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the terms and condition table (only for maintenance). |
||
| 297 | * |
||
| 298 | * @param int $from |
||
| 299 | * @param int $number_of_items |
||
| 300 | * @param int $column |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public static function get_legal_data($from, $number_of_items, $column) |
||
| 305 | { |
||
| 306 | $table = Database::get_main_table(TABLE_MAIN_LEGAL); |
||
| 307 | $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE); |
||
| 308 | $from = (int) $from; |
||
| 309 | $number_of_items = (int) $number_of_items; |
||
| 310 | $column = (int) $column; |
||
| 311 | |||
| 312 | $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date) |
||
| 313 | FROM $table |
||
| 314 | INNER JOIN $lang_table l |
||
| 315 | ON (language_id = l.id) |
||
| 316 | ORDER BY language, version ASC |
||
| 317 | LIMIT $from, $number_of_items "; |
||
| 318 | |||
| 319 | $result = Database::query($sql); |
||
| 320 | $legals = []; |
||
| 321 | while ($legal = Database::fetch_array($result)) { |
||
| 322 | // max 2000 chars |
||
| 323 | $languages[] = $legal[1]; |
||
| 324 | if (strlen($legal[2]) > 2000) { |
||
| 325 | $legal[2] = substr(strip_tags($legal[2]), 0, 2000).' ... '; |
||
| 326 | } |
||
| 327 | if (0 == $legal[4]) { |
||
| 328 | $legal[4] = get_lang('HTML'); |
||
| 329 | } elseif (1 == $legal[4]) { |
||
| 330 | $legal[4] = get_lang('Page Link'); |
||
| 331 | } |
||
| 332 | $legals[] = $legal; |
||
| 333 | } |
||
| 334 | |||
| 335 | return $legals; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Gets the number of terms and conditions available. |
||
| 340 | * |
||
| 341 | * @return int |
||
| 342 | */ |
||
| 343 | public static function count() |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get type of terms and conditions. |
||
| 358 | * Type 0 is HTML Text |
||
| 359 | * Type 1 is a link to a different terms and conditions page. |
||
| 360 | * |
||
| 361 | * @param int $legal_id |
||
| 362 | * @param int $language_id |
||
| 363 | * |
||
| 364 | * @return mixed The current type of terms and conditions (int) or false on error |
||
| 365 | */ |
||
| 366 | public static function get_type_of_terms_and_conditions($legal_id, $language_id) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Send a message to a student with the legal terms link to validate |
||
| 380 | * |
||
| 381 | * @param int $userId The user to send legal terms to |
||
| 382 | * @param int $coachId The user who sends the legal terms |
||
| 383 | */ |
||
| 384 | public static function sendLegal(int $userId, int $coachId) |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param int $userId |
||
| 444 | */ |
||
| 445 | public static function deleteLegal($userId) |
||
| 446 | { |
||
| 447 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 448 | $value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'legal_accept'); |
||
| 449 | $result = $extraFieldValue->delete($value['id']); |
||
| 450 | if ($result) { |
||
| 451 | Display::addFlash(Display::return_message(get_lang('Deleted'))); |
||
| 452 | } |
||
| 453 | |||
| 454 | $value = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 455 | $userId, |
||
| 456 | 'termactivated' |
||
| 457 | ); |
||
| 458 | if ($value) { |
||
| 459 | $extraFieldValue->delete($value['id']); |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public static function getTreatmentTypeList() |
||
| 484 | ]; |
||
| 485 | } |
||
| 487 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.