| Total Complexity | 53 |
| Total Lines | 634 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | * Disables a user. |
||
| 16 | * |
||
| 17 | * @param string API secret key |
||
| 18 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 19 | * @param string User id value |
||
| 20 | */ |
||
| 21 | public function DisableUser( |
||
| 22 | $secret_key, |
||
| 23 | $user_id_field_name, |
||
| 24 | $user_id_value |
||
| 25 | ) { |
||
| 26 | $verifKey = $this->verifyKey($secret_key); |
||
| 27 | if ($verifKey instanceof WSError) { |
||
| 28 | // Let the implementation handle it |
||
| 29 | $this->handleError($verifKey); |
||
| 30 | } else { |
||
| 31 | $result = $this->changeUserActiveState( |
||
| 32 | $user_id_field_name, |
||
| 33 | $user_id_value, |
||
| 34 | 0 |
||
| 35 | ); |
||
| 36 | if ($result instanceof WSError) { |
||
|
|
|||
| 37 | $this->handleError($result); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Disables multiple users. |
||
| 44 | * |
||
| 45 | * @param string API secret key |
||
| 46 | * @param array Array of users with elements of the form |
||
| 47 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 48 | * |
||
| 49 | * @return array Array with elements like |
||
| 50 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 51 | * Note that if the result array contains a code different |
||
| 52 | * than 0, an error occured |
||
| 53 | */ |
||
| 54 | public function DisableUsers($secret_key, $users) |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Enables a user. |
||
| 67 | * |
||
| 68 | * @param string API secret key |
||
| 69 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 70 | * @param string User id value |
||
| 71 | */ |
||
| 72 | public function EnableUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 73 | { |
||
| 74 | $verifKey = $this->verifyKey($secret_key); |
||
| 75 | if ($verifKey instanceof WSError) { |
||
| 76 | $this->handleError($verifKey); |
||
| 77 | } else { |
||
| 78 | $result = $this->changeUserActiveState( |
||
| 79 | $user_id_field_name, |
||
| 80 | $user_id_value, |
||
| 81 | 1 |
||
| 82 | ); |
||
| 83 | if ($result instanceof WSError) { |
||
| 84 | $this->handleError($result); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Enables multiple users. |
||
| 91 | * |
||
| 92 | * @param string API secret key |
||
| 93 | * @param array Array of users with elements of the form |
||
| 94 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 95 | * |
||
| 96 | * @return array Array with elements like |
||
| 97 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 98 | * Note that if the result array contains a code different |
||
| 99 | * than 0, an error occured |
||
| 100 | */ |
||
| 101 | public function EnableUsers($secret_key, $users) |
||
| 102 | { |
||
| 103 | $verifKey = $this->verifyKey($secret_key); |
||
| 104 | if ($verifKey instanceof WSError) { |
||
| 105 | // Let the implementation handle it |
||
| 106 | $this->handleError($verifKey); |
||
| 107 | } else { |
||
| 108 | return $this->changeUsersActiveState($users, 1); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Deletes a user. |
||
| 114 | * |
||
| 115 | * @param string API secret key |
||
| 116 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 117 | * @param string User id value |
||
| 118 | */ |
||
| 119 | public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) |
||
| 120 | { |
||
| 121 | $verifKey = $this->verifyKey($secret_key); |
||
| 122 | if ($verifKey instanceof WSError) { |
||
| 123 | $this->handleError($verifKey); |
||
| 124 | } else { |
||
| 125 | $result = $this->deleteUserHelper( |
||
| 126 | $user_id_field_name, |
||
| 127 | $user_id_value |
||
| 128 | ); |
||
| 129 | if ($result instanceof WSError) { |
||
| 130 | $this->handleError($result); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Deletes multiple users. |
||
| 137 | * |
||
| 138 | * @param string API secret key |
||
| 139 | * @param array Array of users with elements of the form |
||
| 140 | * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value') |
||
| 141 | * |
||
| 142 | * @return array Array with elements like |
||
| 143 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 144 | * Note that if the result array contains a code different |
||
| 145 | * than 0, an error occured |
||
| 146 | */ |
||
| 147 | public function DeleteUsers($secret_key, $users) |
||
| 148 | { |
||
| 149 | $verifKey = $this->verifyKey($secret_key); |
||
| 150 | if ($verifKey instanceof WSError) { |
||
| 151 | $this->handleError($verifKey); |
||
| 152 | } else { |
||
| 153 | $results = []; |
||
| 154 | foreach ($users as $user) { |
||
| 155 | $result_tmp = []; |
||
| 156 | $result_op = $this->deleteUserHelper( |
||
| 157 | $user['user_id_field_name'], |
||
| 158 | $user['user_id_value'] |
||
| 159 | ); |
||
| 160 | $result_tmp['user_id_value'] = $user['user_id_value']; |
||
| 161 | if ($result_op instanceof WSError) { |
||
| 162 | // Return the error in the results |
||
| 163 | $result_tmp['result'] = $result_op->toArray(); |
||
| 164 | } else { |
||
| 165 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 166 | } |
||
| 167 | $results[] = $result_tmp; |
||
| 168 | } |
||
| 169 | |||
| 170 | return $results; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Creates a user. |
||
| 176 | * |
||
| 177 | * @param string API secret key |
||
| 178 | * @param string User first name |
||
| 179 | * @param string User last name |
||
| 180 | * @param int User status |
||
| 181 | * @param string Login name |
||
| 182 | * @param string Password (encrypted or not) |
||
| 183 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, |
||
| 184 | * set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 185 | * to include the salt in the extra fields if you are encrypting the password |
||
| 186 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 187 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 188 | * @param int Visibility. Set by default to 1 |
||
| 189 | * @param string User email. Set by default to an empty string |
||
| 190 | * @param string Language. Set by default to english |
||
| 191 | * @param string Phone. Set by default to an empty string |
||
| 192 | * @param string Expiration date. Set to null by default |
||
| 193 | * @param array Extra fields. An array with elements of the form |
||
| 194 | * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default |
||
| 195 | * |
||
| 196 | * @return int New user id generated by the system |
||
| 197 | */ |
||
| 198 | public function CreateUser( |
||
| 199 | $secret_key, |
||
| 200 | $firstname, |
||
| 201 | $lastname, |
||
| 202 | $status, |
||
| 203 | $login, |
||
| 204 | $password, |
||
| 205 | $encrypt_method, |
||
| 206 | $user_id_field_name, |
||
| 207 | $user_id_value, |
||
| 208 | $visibility = 1, |
||
| 209 | $email = '', |
||
| 210 | $language = 'english', |
||
| 211 | $phone = '', |
||
| 212 | $expiration_date = '0000-00-00 00:00:00', |
||
| 213 | $extras = [] |
||
| 214 | ) { |
||
| 215 | // First, verify the secret key |
||
| 216 | $verifKey = $this->verifyKey($secret_key); |
||
| 217 | if ($verifKey instanceof WSError) { |
||
| 218 | $this->handleError($verifKey); |
||
| 219 | } else { |
||
| 220 | $result = $this->createUserHelper( |
||
| 221 | $firstname, |
||
| 222 | $lastname, |
||
| 223 | $status, |
||
| 224 | $login, |
||
| 225 | $password, |
||
| 226 | $encrypt_method, |
||
| 227 | $user_id_field_name, |
||
| 228 | $user_id_value, |
||
| 229 | $visibility, |
||
| 230 | $email, |
||
| 231 | $language, |
||
| 232 | $phone, |
||
| 233 | $expiration_date, |
||
| 234 | $extras |
||
| 235 | ); |
||
| 236 | if ($result instanceof WSError) { |
||
| 237 | $this->handleError($result); |
||
| 238 | } else { |
||
| 239 | return $result; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Creates multiple users. |
||
| 246 | * |
||
| 247 | * @param string API secret key |
||
| 248 | * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method |
||
| 249 | * |
||
| 250 | * @return array Array with elements of the form |
||
| 251 | * array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 252 | */ |
||
| 253 | public function CreateUsers($secret_key, $users) |
||
| 254 | { |
||
| 255 | $verifKey = $this->verifyKey($secret_key); |
||
| 256 | if ($verifKey instanceof WSError) { |
||
| 257 | $this->handleError($verifKey); |
||
| 258 | } else { |
||
| 259 | $results = []; |
||
| 260 | foreach ($users as $user) { |
||
| 261 | $result_tmp = []; |
||
| 262 | // re-initialize variables just in case |
||
| 263 | $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null; |
||
| 264 | extract($user); |
||
| 265 | $result = $this->createUserHelper( |
||
| 266 | $firstname, |
||
| 267 | $lastname, |
||
| 268 | $status, |
||
| 269 | $login, |
||
| 270 | $password, |
||
| 271 | $encrypt_method, |
||
| 272 | $user_id_field_name, |
||
| 273 | $user_id_value, |
||
| 274 | $visibility, |
||
| 275 | $email, |
||
| 276 | $language, |
||
| 277 | $phone, |
||
| 278 | $expiration_date, |
||
| 279 | $extras |
||
| 280 | ); |
||
| 281 | if ($result instanceof WSError) { |
||
| 282 | $result_tmp['result'] = $result->toArray(); |
||
| 283 | $result_tmp['user_id_value'] = $user_id_value; |
||
| 284 | $result_tmp['user_id_generated'] = 0; |
||
| 285 | } else { |
||
| 286 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 287 | $result_tmp['user_id_value'] = $user_id_value; |
||
| 288 | $result_tmp['user_id_generated'] = $result; |
||
| 289 | } |
||
| 290 | $results[] = $result_tmp; |
||
| 291 | } |
||
| 292 | |||
| 293 | return $results; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Edits user info. |
||
| 299 | * |
||
| 300 | * @param string API secret key |
||
| 301 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 302 | * @param string User id value |
||
| 303 | * @param string First name |
||
| 304 | * @param string Last name |
||
| 305 | * @param int User status |
||
| 306 | * @param string Login name |
||
| 307 | * @param string Password. Leave blank if you don't want to update it |
||
| 308 | * @param string Encrypt method |
||
| 309 | * @param string User email |
||
| 310 | * @param string Language. Set by default to english |
||
| 311 | * @param string Phone. Set by default to an empty string |
||
| 312 | * @param string Expiration date. Set to null by default |
||
| 313 | * @param array Extra fields. An array with elements of the form |
||
| 314 | * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update |
||
| 315 | */ |
||
| 316 | public function EditUser( |
||
| 317 | $secret_key, |
||
| 318 | $user_id_field_name, |
||
| 319 | $user_id_value, |
||
| 320 | $firstname, |
||
| 321 | $lastname, |
||
| 322 | $status, |
||
| 323 | $loginname, |
||
| 324 | $password, |
||
| 325 | $encrypt_method, |
||
| 326 | $email, |
||
| 327 | $language, |
||
| 328 | $phone, |
||
| 329 | $expiration_date, |
||
| 330 | $extras |
||
| 331 | ) { |
||
| 332 | // First, verify the secret key |
||
| 333 | $verifKey = $this->verifyKey($secret_key); |
||
| 334 | if ($verifKey instanceof WSError) { |
||
| 335 | $this->handleError($verifKey); |
||
| 336 | } else { |
||
| 337 | $extras_associative = []; |
||
| 338 | if (!empty($extras)) { |
||
| 339 | foreach ($extras as $extra) { |
||
| 340 | $extras_associative[$extra['field_name']] = $extra['field_value']; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | $result = $this->editUserHelper( |
||
| 345 | $user_id_field_name, |
||
| 346 | $user_id_value, |
||
| 347 | $firstname, |
||
| 348 | $lastname, |
||
| 349 | $status, |
||
| 350 | $loginname, |
||
| 351 | $password, |
||
| 352 | $encrypt_method, |
||
| 353 | $email, |
||
| 354 | $language, |
||
| 355 | $phone, |
||
| 356 | $expiration_date, |
||
| 357 | $extras_associative |
||
| 358 | ); |
||
| 359 | if ($result instanceof WSError) { |
||
| 360 | $this->handleError($result); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Edits multiple users. |
||
| 367 | * |
||
| 368 | * @param string API secret key |
||
| 369 | * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method |
||
| 370 | * |
||
| 371 | * @return array Array with elements like |
||
| 372 | * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 373 | * Note that if the result array contains a code different |
||
| 374 | * than 0, an error occured |
||
| 375 | */ |
||
| 376 | public function EditUsers($secret_key, $users) |
||
| 377 | { |
||
| 378 | $verifKey = $this->verifyKey($secret_key); |
||
| 379 | if ($verifKey instanceof WSError) { |
||
| 380 | $this->handleError($verifKey); |
||
| 381 | } else { |
||
| 382 | $results = []; |
||
| 383 | foreach ($users as $user) { |
||
| 384 | $result_tmp = []; |
||
| 385 | // re-initialize variables just in case |
||
| 386 | $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null; |
||
| 387 | extract($user); |
||
| 388 | $result_op = $this->editUserHelper( |
||
| 389 | $user_id_field_name, |
||
| 390 | $user_id_value, |
||
| 391 | $firstname, |
||
| 392 | $lastname, |
||
| 393 | $status, |
||
| 394 | $loginname, |
||
| 395 | $password, |
||
| 396 | $encrypt_method, |
||
| 397 | $email, |
||
| 398 | $language, |
||
| 399 | $phone, |
||
| 400 | $expiration_date, |
||
| 401 | $extras |
||
| 402 | ); |
||
| 403 | $result_tmp['user_id_value'] = $user['user_id_value']; |
||
| 404 | if ($result_op instanceof WSError) { |
||
| 405 | // Return the error in the results |
||
| 406 | $result_tmp['result'] = $result_op->toArray(); |
||
| 407 | } else { |
||
| 408 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 409 | } |
||
| 410 | $results[] = $result_tmp; |
||
| 411 | } |
||
| 412 | |||
| 413 | return $results; |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Enables or disables a user. |
||
| 419 | * |
||
| 420 | * @param string User id field name |
||
| 421 | * @param string User id value |
||
| 422 | * @param int Set to 1 to enable and to 0 to disable |
||
| 423 | * |
||
| 424 | * @return int |
||
| 425 | */ |
||
| 426 | protected function changeUserActiveState( |
||
| 427 | $user_id_field_name, |
||
| 428 | $user_id_value, |
||
| 429 | $state |
||
| 430 | ) { |
||
| 431 | $user_id = $this->getUserId($user_id_field_name, $user_id_value); |
||
| 432 | if ($user_id instanceof WSError) { |
||
| 433 | return $user_id; |
||
| 434 | } else { |
||
| 435 | if ($state == 0) { |
||
| 436 | UserManager::disable($user_id); |
||
| 437 | } else { |
||
| 438 | if ($state == 1) { |
||
| 439 | UserManager::enable($user_id); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Enables or disables multiple users. |
||
| 447 | * |
||
| 448 | * @param array Users |
||
| 449 | * @param int Set to 1 to enable and to 0 to disable |
||
| 450 | * |
||
| 451 | * @return array Array of results |
||
| 452 | */ |
||
| 453 | protected function changeUsersActiveState($users, $state) |
||
| 454 | { |
||
| 455 | $results = []; |
||
| 456 | foreach ($users as $user) { |
||
| 457 | $result_tmp = []; |
||
| 458 | $result_op = $this->changeUserActiveState( |
||
| 459 | $user['user_id_field_name'], |
||
| 460 | $user['user_id_value'], |
||
| 461 | $state |
||
| 462 | ); |
||
| 463 | $result_tmp['user_id_value'] = $user['user_id_value']; |
||
| 464 | if ($result_op instanceof WSError) { |
||
| 465 | // Return the error in the results |
||
| 466 | $result_tmp['result'] = $result_op->toArray(); |
||
| 467 | } else { |
||
| 468 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 469 | } |
||
| 470 | $results[] = $result_tmp; |
||
| 471 | } |
||
| 472 | |||
| 473 | return $results; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Deletes a user (helper method). |
||
| 478 | * |
||
| 479 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 480 | * @param string User id value |
||
| 481 | * |
||
| 482 | * @return mixed True if user was successfully deleted, WSError otherwise |
||
| 483 | */ |
||
| 484 | protected function deleteUserHelper($user_id_field_name, $user_id_value) |
||
| 485 | { |
||
| 486 | $user_id = $this->getUserId($user_id_field_name, $user_id_value); |
||
| 487 | if ($user_id instanceof WSError) { |
||
| 488 | return $user_id; |
||
| 489 | } else { |
||
| 490 | if (!UserManager::delete_user($user_id)) { |
||
| 491 | return new WSError( |
||
| 492 | 101, |
||
| 493 | "There was a problem while deleting this user" |
||
| 494 | ); |
||
| 495 | } else { |
||
| 496 | return true; |
||
| 497 | } |
||
| 498 | } |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Creates a user (helper method). |
||
| 503 | * |
||
| 504 | * @param string User first name |
||
| 505 | * @param string User last name |
||
| 506 | * @param int User status |
||
| 507 | * @param string Login name |
||
| 508 | * @param string Password (encrypted or not) |
||
| 509 | * @param string Encrypt method. Leave blank if you are passing the password in clear text, |
||
| 510 | * set to the encrypt method used to encrypt the password otherwise. Remember |
||
| 511 | * to include the salt in the extra fields if you are encrypting the password |
||
| 512 | * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id |
||
| 513 | * @param string User id value. Leave blank if you are using the internal user_id |
||
| 514 | * @param int visibility |
||
| 515 | * @param string user email |
||
| 516 | * @param string language |
||
| 517 | * @param string phone |
||
| 518 | * @param string Expiration date |
||
| 519 | * @param array Extra fields. An array with elements of the form |
||
| 520 | * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). |
||
| 521 | * |
||
| 522 | * @return mixed New user id generated by the system, WSError otherwise |
||
| 523 | */ |
||
| 524 | protected function createUserHelper( |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Edits user info (helper method). |
||
| 577 | * |
||
| 578 | * @param string User id field name. Use "chamilo_user_id" in order to use internal system id |
||
| 579 | * @param string User id value |
||
| 580 | * @param string First name |
||
| 581 | * @param string Last name |
||
| 582 | * @param int User status |
||
| 583 | * @param string Login name |
||
| 584 | * @param string Password. Leave blank if you don't want to update it |
||
| 585 | * @param string Encrypt method |
||
| 586 | * @param string User email |
||
| 587 | * @param string Language. Set by default to english |
||
| 588 | * @param string Phone. Set by default to an empty string |
||
| 589 | * @param string Expiration date. Set to null by default |
||
| 590 | * @param array Extra fields. An array with elements of the form |
||
| 591 | * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). |
||
| 592 | * Leave empty if you don't want to update |
||
| 593 | * |
||
| 594 | * @return mixed True if user was successfully updated, WSError otherwise |
||
| 595 | */ |
||
| 596 | protected function editUserHelper( |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 |