| Total Complexity | 53 |
| Total Lines | 625 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WSUser 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 WSUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class WSUser extends WS |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Enables or disables a user |
||
| 16 | * |
||
| 17 | * @param string User id field name |
||
| 18 | * @param string User id value |
||
| 19 | * @param int Set to 1 to enable and to 0 to disable |
||
| 20 | * @return int |
||
| 21 | */ |
||
| 22 | protected function changeUserActiveState( |
||
| 23 | $user_id_field_name, |
||
| 24 | $user_id_value, |
||
| 25 | $state |
||
| 26 | ) { |
||
| 27 | $user_id = $this->getUserId($user_id_field_name, $user_id_value); |
||
| 28 | if ($user_id instanceof WSError) { |
||
| 29 | return $user_id; |
||
|
|
|||
| 30 | } else { |
||
| 31 | if ($state == 0) { |
||
| 32 | UserManager::disable($user_id); |
||
| 33 | } else { |
||
| 34 | if ($state == 1) { |
||
| 35 | UserManager::enable($user_id); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Enables or disables multiple users |
||
| 43 | * |
||
| 44 | * @param array Users |
||
| 45 | * @param int Set to 1 to enable and to 0 to disable |
||
| 46 | * @return array Array of results |
||
| 47 | */ |
||
| 48 | protected function changeUsersActiveState($users, $state) |
||
| 49 | { |
||
| 50 | $results = []; |
||
| 51 | foreach ($users as $user) { |
||
| 52 | $result_tmp = []; |
||
| 53 | $result_op = $this->changeUserActiveState( |
||
| 54 | $user['user_id_field_name'], |
||
| 55 | $user['user_id_value'], |
||
| 56 | $state |
||
| 57 | ); |
||
| 58 | $result_tmp['user_id_value'] = $user['user_id_value']; |
||
| 59 | if ($result_op instanceof WSError) { |
||
| 60 | // Return the error in the results |
||
| 61 | $result_tmp['result'] = $result_op->toArray(); |
||
| 62 | } else { |
||
| 63 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 64 | } |
||
| 65 | $results[] = $result_tmp; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $results; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Disables a user |
||
| 73 | * |
||
| 74 | * @param string API secret key |
||
| 75 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 76 | * @param string User id value |
||
| 77 | */ |
||
| 78 | public function DisableUser( |
||
| 79 | $secret_key, |
||
| 80 | $user_id_field_name, |
||
| 81 | $user_id_value |
||
| 82 | ) { |
||
| 83 | $verifKey = $this->verifyKey($secret_key); |
||
| 84 | if ($verifKey instanceof WSError) { |
||
| 85 | // Let the implementation handle it |
||
| 86 | $this->handleError($verifKey); |
||
| 87 | } else { |
||
| 88 | $result = $this->changeUserActiveState( |
||
| 89 | $user_id_field_name, |
||
| 90 | $user_id_value, |
||
| 91 | 0 |
||
| 92 | ); |
||
| 93 | if ($result instanceof WSError) { |
||
| 94 | $this->handleError($result); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Disables multiple users |
||
| 101 | * |
||
| 102 | * @param string API secret key |
||
| 103 | * @param array Array of users with elements of the form |
||
| 104 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 105 | * @return array Array with elements like |
||
| 106 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 107 | * Note that if the result array contains a code different |
||
| 108 | * than 0, an error occured |
||
| 109 | */ |
||
| 110 | public function DisableUsers($secret_key, $users) |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Enables a user |
||
| 123 | * |
||
| 124 | * @param string API secret key |
||
| 125 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 126 | * @param string User id value |
||
| 127 | */ |
||
| 128 | public function EnableUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 129 | { |
||
| 130 | $verifKey = $this->verifyKey($secret_key); |
||
| 131 | if ($verifKey instanceof WSError) { |
||
| 132 | $this->handleError($verifKey); |
||
| 133 | } else { |
||
| 134 | $result = $this->changeUserActiveState( |
||
| 135 | $user_id_field_name, |
||
| 136 | $user_id_value, |
||
| 137 | 1 |
||
| 138 | ); |
||
| 139 | if ($result instanceof WSError) { |
||
| 140 | $this->handleError($result); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Enables multiple users |
||
| 147 | * |
||
| 148 | * @param string API secret key |
||
| 149 | * @param array Array of users with elements of the form |
||
| 150 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 151 | * @return array Array with elements like |
||
| 152 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 153 | * Note that if the result array contains a code different |
||
| 154 | * than 0, an error occured |
||
| 155 | */ |
||
| 156 | public function EnableUsers($secret_key, $users) |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Deletes a user (helper method) |
||
| 169 | * |
||
| 170 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 171 | * @param string User id value |
||
| 172 | * @return mixed True if user was successfully deleted, WSError otherwise |
||
| 173 | */ |
||
| 174 | protected function deleteUserHelper($user_id_field_name, $user_id_value) |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Deletes a user |
||
| 193 | * |
||
| 194 | * @param string API secret key |
||
| 195 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 196 | * @param string User id value |
||
| 197 | */ |
||
| 198 | public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 199 | { |
||
| 200 | $verifKey = $this->verifyKey($secret_key); |
||
| 201 | if ($verifKey instanceof WSError) { |
||
| 202 | $this->handleError($verifKey); |
||
| 203 | } else { |
||
| 204 | $result = $this->deleteUserHelper( |
||
| 205 | $user_id_field_name, |
||
| 206 | $user_id_value |
||
| 207 | ); |
||
| 208 | if ($result instanceof WSError) { |
||
| 209 | $this->handleError($result); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Deletes multiple users |
||
| 216 | * |
||
| 217 | * @param string API secret key |
||
| 218 | * @param array Array of users with elements of the form |
||
| 219 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 220 | * @return array Array with elements like |
||
| 221 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 222 | * Note that if the result array contains a code different |
||
| 223 | * than 0, an error occured |
||
| 224 | */ |
||
| 225 | public function DeleteUsers($secret_key, $users) |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Creates a user (helper method) |
||
| 254 | * |
||
| 255 | * @param string User first name |
||
| 256 | * @param string User last name |
||
| 257 | * @param int User status |
||
| 258 | * @param string Login name |
||
| 259 | * @param string Password (encrypted or not) |
||
| 260 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, |
||
| 261 | * set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 262 | * to include the salt in the extra fields if you are encrypting the password |
||
| 263 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 264 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 265 | * @param int Visibility. |
||
| 266 | * @param string User email. |
||
| 267 | * @param string Language. |
||
| 268 | * @param string Phone. |
||
| 269 | * @param string Expiration date |
||
| 270 | * @param array Extra fields. An array with elements of the form |
||
| 271 | * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). |
||
| 272 | * @return mixed New user id generated by the system, WSError otherwise |
||
| 273 | */ |
||
| 274 | protected function createUserHelper( |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Creates a user |
||
| 328 | * |
||
| 329 | * @param string API secret key |
||
| 330 | * @param string User first name |
||
| 331 | * @param string User last name |
||
| 332 | * @param int User status |
||
| 333 | * @param string Login name |
||
| 334 | * @param string Password (encrypted or not) |
||
| 335 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, |
||
| 336 | * set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 337 | * to include the salt in the extra fields if you are encrypting the password |
||
| 338 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 339 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 340 | * @param int Visibility. Set by default to 1 |
||
| 341 | * @param string User email. Set by default to an empty string |
||
| 342 | * @param string Language. Set by default to english |
||
| 343 | * @param string Phone. Set by default to an empty string |
||
| 344 | * @param string Expiration date. Set to null by default |
||
| 345 | * @param array Extra fields. An array with elements of the form |
||
| 346 | * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default |
||
| 347 | * @return int New user id generated by the system |
||
| 348 | */ |
||
| 349 | public function CreateUser( |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Creates multiple users |
||
| 397 | * |
||
| 398 | * @param string API secret key |
||
| 399 | * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method |
||
| 400 | * @return array Array with elements of the form |
||
| 401 | * array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 402 | */ |
||
| 403 | public function CreateUsers($secret_key, $users) |
||
| 404 | { |
||
| 405 | $verifKey = $this->verifyKey($secret_key); |
||
| 406 | if ($verifKey instanceof WSError) { |
||
| 407 | $this->handleError($verifKey); |
||
| 408 | } else { |
||
| 409 | $results = []; |
||
| 410 | foreach ($users as $user) { |
||
| 411 | $result_tmp = []; |
||
| 412 | // re-initialize variables just in case |
||
| 413 | $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null; |
||
| 414 | extract($user); |
||
| 415 | $result = $this->createUserHelper( |
||
| 416 | $firstname, |
||
| 417 | $lastname, |
||
| 418 | $status, |
||
| 419 | $login, |
||
| 420 | $password, |
||
| 421 | $encrypt_method, |
||
| 422 | $user_id_field_name, |
||
| 423 | $user_id_value, |
||
| 424 | $visibility, |
||
| 425 | $email, |
||
| 426 | $language, |
||
| 427 | $phone, |
||
| 428 | $expiration_date, |
||
| 429 | $extras |
||
| 430 | ); |
||
| 431 | if ($result instanceof WSError) { |
||
| 432 | $result_tmp['result'] = $result->toArray(); |
||
| 433 | $result_tmp['user_id_value'] = $user_id_value; |
||
| 434 | $result_tmp['user_id_generated'] = 0; |
||
| 435 | } else { |
||
| 436 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 437 | $result_tmp['user_id_value'] = $user_id_value; |
||
| 438 | $result_tmp['user_id_generated'] = $result; |
||
| 439 | } |
||
| 440 | $results[] = $result_tmp; |
||
| 441 | } |
||
| 442 | |||
| 443 | return $results; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Edits user info (helper method) |
||
| 449 | * |
||
| 450 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 451 | * @param string User id value |
||
| 452 | * @param string First name |
||
| 453 | * @param string Last name |
||
| 454 | * @param int User status |
||
| 455 | * @param string Login name |
||
| 456 | * @param string Password. Leave blank if you don't want to update it |
||
| 457 | * @param string Encrypt method |
||
| 458 | * @param string User email |
||
| 459 | * @param string Language. Set by default to english |
||
| 460 | * @param string Phone. Set by default to an empty string |
||
| 461 | * @param string Expiration date. Set to null by default |
||
| 462 | * @param array Extra fields. An array with elements of the form |
||
| 463 | * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). |
||
| 464 | * Leave empty if you don't want to update |
||
| 465 | * @return mixed True if user was successfully updated, WSError otherwise |
||
| 466 | */ |
||
| 467 | protected function editUserHelper( |
||
| 517 | } |
||
| 518 | } |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Edits user info |
||
| 523 | * |
||
| 524 | * @param string API secret key |
||
| 525 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 526 | * @param string User id value |
||
| 527 | * @param string First name |
||
| 528 | * @param string Last name |
||
| 529 | * @param int User status |
||
| 530 | * @param string Login name |
||
| 531 | * @param string Password. Leave blank if you don't want to update it |
||
| 532 | * @param string Encrypt method |
||
| 533 | * @param string User email |
||
| 534 | * @param string Language. Set by default to english |
||
| 535 | * @param string Phone. Set by default to an empty string |
||
| 536 | * @param string Expiration date. Set to null by default |
||
| 537 | * @param array Extra fields. An array with elements of the form |
||
| 538 | * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update |
||
| 539 | */ |
||
| 540 | public function EditUser( |
||
| 541 | $secret_key, |
||
| 542 | $user_id_field_name, |
||
| 543 | $user_id_value, |
||
| 544 | $firstname, |
||
| 545 | $lastname, |
||
| 546 | $status, |
||
| 547 | $loginname, |
||
| 548 | $password, |
||
| 549 | $encrypt_method, |
||
| 550 | $email, |
||
| 551 | $language, |
||
| 552 | $phone, |
||
| 553 | $expiration_date, |
||
| 554 | $extras |
||
| 555 | ) { |
||
| 556 | // First, verify the secret key |
||
| 557 | $verifKey = $this->verifyKey($secret_key); |
||
| 558 | if ($verifKey instanceof WSError) { |
||
| 559 | $this->handleError($verifKey); |
||
| 560 | } else { |
||
| 561 | $extras_associative = []; |
||
| 562 | if (!empty($extras)) { |
||
| 563 | foreach ($extras as $extra) { |
||
| 564 | $extras_associative[$extra['field_name']] = $extra['field_value']; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | $result = $this->editUserHelper( |
||
| 569 | $user_id_field_name, |
||
| 570 | $user_id_value, |
||
| 571 | $firstname, |
||
| 572 | $lastname, |
||
| 573 | $status, |
||
| 574 | $loginname, |
||
| 575 | $password, |
||
| 576 | $encrypt_method, |
||
| 577 | $email, |
||
| 578 | $language, |
||
| 579 | $phone, |
||
| 580 | $expiration_date, |
||
| 581 | $extras_associative |
||
| 582 | ); |
||
| 583 | if ($result instanceof WSError) { |
||
| 584 | $this->handleError($result); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Edits multiple users |
||
| 591 | * |
||
| 592 | * @param string API secret key |
||
| 593 | * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method |
||
| 594 | * @return array Array with elements like |
||
| 595 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 596 | * Note that if the result array contains a code different |
||
| 597 | * than 0, an error occured |
||
| 598 | */ |
||
| 599 | public function EditUsers($secret_key, $users) |
||
| 637 | } |
||
| 638 | } |
||
| 639 | } |
||
| 640 |