Complex classes like DBService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DBService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class DBService |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var string DEFAULTDBSERVICEURL The main URL for the dbService. |
||
| 64 | * Corresponds to the OAuth 1.0a .war. |
||
| 65 | */ |
||
| 66 | const DEFAULTDBSERVICEURL = 'http://localhost:8080/oauth/dbService'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string DEFAULTDBSERVICEURL The new URL for the dbService, to be |
||
| 70 | * used once Jeff has verified all dbService calls work with the |
||
| 71 | * new OAuth 2.0 .war. |
||
| 72 | */ |
||
| 73 | const OAUTH2DBSERVICEURL = 'http://localhost:8080/oauth2/dbService'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array $STATUS The various STATUS_* constants, originally from |
||
| 77 | * Store.pm. The keys of the array are strings corresponding to the |
||
| 78 | * constant names. The values of the array are the integer (hex) |
||
| 79 | * values. For example, DBService::$STATUS['STATUS_OK'] = 0; |
||
| 80 | * Use 'array_search($this->status,DBService::$STATUS)' to look |
||
| 81 | * up the STATUS_* name given the status integer value. |
||
| 82 | */ |
||
| 83 | public static $STATUS = array( |
||
| 84 | 'STATUS_OK' => 0x0, |
||
| 85 | 'STATUS_ACTION_NOT_FOUND' => 0x1, |
||
| 86 | 'STATUS_NEW_USER' => 0x2, |
||
| 87 | 'STATUS_USER_UPDATED' => 0x4, |
||
| 88 | 'STATUS_USER_NOT_FOUND' => 0x6, |
||
| 89 | 'STATUS_USER_EXISTS' => 0x8, |
||
| 90 | 'STATUS_USER_EXISTS_ERROR' => 0xFFFA1, |
||
| 91 | 'STATUS_USER_NOT_FOUND_ERROR' => 0xFFFA3, |
||
| 92 | 'STATUS_TRANSACTION_NOT_FOUND' => 0xFFFA5, |
||
| 93 | 'STATUS_IDP_SAVE_FAILED' => 0xFFFA7, |
||
| 94 | 'STATUS_DUPLICATE_PARAMETER_FOUND' => 0xFFFF1, |
||
| 95 | 'STATUS_INTERNAL_ERROR' => 0xFFFF3, |
||
| 96 | 'STATUS_SAVE_IDP_FAILED' => 0xFFFF5, |
||
| 97 | 'STATUS_MALFORMED_INPUT_ERROR' => 0xFFFF7, |
||
| 98 | 'STATUS_MISSING_PARAMETER_ERROR' => 0xFFFF9, |
||
| 99 | 'STATUS_NO_REMOTE_USER' => 0xFFFFB, |
||
| 100 | 'STATUS_NO_IDENTITY_PROVIDER' => 0xFFFFD, |
||
| 101 | 'STATUS_CLIENT_NOT_FOUND' => 0xFFFFF, |
||
| 102 | 'STATUS_TRANSACTION_NOT_FOUND' => 0x10001, |
||
| 103 | 'STATUS_EPTID_MISMATCH' => 0x100001, |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int $status The returned status code from dbService calls |
||
| 108 | */ |
||
| 109 | public $status; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string $user_uid The CILogon UID |
||
| 113 | */ |
||
| 114 | public $user_uid; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string $remote_user The HTTP session REMOTE_USER |
||
| 118 | */ |
||
| 119 | public $remote_user; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string $idp The Identity Provider's entityId |
||
| 123 | */ |
||
| 124 | public $idp; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string $idp_display_name The Identity Provider's name |
||
| 128 | */ |
||
| 129 | public $idp_display_name; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string $first_name User's given name |
||
| 133 | */ |
||
| 134 | public $first_name; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string $last_name User's family name |
||
| 138 | */ |
||
| 139 | public $last_name; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string $display_name User's full name |
||
| 143 | */ |
||
| 144 | public $display_name; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string $email User's email address |
||
| 148 | */ |
||
| 149 | public $email; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var string $distinguished_name X.509 DN + email address |
||
| 153 | */ |
||
| 154 | public $distinguished_name; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var string $eppn eduPersonPrincipalName |
||
| 158 | */ |
||
| 159 | public $eppn; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var string $eptid eduPersonTargetedID |
||
| 163 | */ |
||
| 164 | public $eptid; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var string $open_id Old Google OpenID 2.0 identifier |
||
| 168 | */ |
||
| 169 | public $open_id; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var string $oidc OpenID Connect identifier |
||
| 173 | */ |
||
| 174 | public $oidc; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var string $affiliation eduPersonScopedAffiliation |
||
| 178 | */ |
||
| 179 | public $affiliation; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var string $ou Organizational Unit |
||
| 183 | */ |
||
| 184 | public $ou; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var string $serial_string CILogon serial string (e.g., A34201) |
||
| 188 | */ |
||
| 189 | public $serial_string; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @var string $create_time Time user entry was created |
||
| 193 | */ |
||
| 194 | public $create_time; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var string $oauth_token OAuth 2.0 token |
||
| 198 | */ |
||
| 199 | public $oauth_token; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @var string $cilogon_callback OAuth 1.0a callback URL |
||
| 203 | */ |
||
| 204 | public $cilogon_callback; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @var string $cilogon_success OAuth 1.0a success URL |
||
| 208 | */ |
||
| 209 | public $cilogon_success; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @var string $cilogon_failure OAuth 1.0a failure URL |
||
| 213 | */ |
||
| 214 | public $cilogon_failure; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @var string $cilogon_portal_name OAuth client name |
||
| 218 | */ |
||
| 219 | public $cilogon_portal_name; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @var string $two_factor Two factor string used by TwoFactor.php |
||
| 223 | */ |
||
| 224 | public $two_factor; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @var array $idp_uids IdPs stored in the 'values' of the array |
||
| 228 | */ |
||
| 229 | public $idp_uids; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @var string $client_name OAuth 2.0 client name |
||
| 233 | */ |
||
| 234 | public $client_name; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @var string $client_id OAuth 2.0 client identifier |
||
| 238 | */ |
||
| 239 | public $client_id; |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @var string $client_home_uri OAuth 2.0 client home URL |
||
| 243 | */ |
||
| 244 | public $client_home_uri; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @var array $client_callback_uris An array of OAuth 2.0 callback URLs |
||
| 248 | */ |
||
| 249 | public $client_callback_uris; |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @var string $dbservice URL The URL to use for the dbService |
||
| 253 | */ |
||
| 254 | private $dbserviceurl; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * __construct |
||
| 258 | * |
||
| 259 | * Default constructor. All of the various class members are |
||
| 260 | * initialized to 'null' or empty arrays. |
||
| 261 | * |
||
| 262 | * @param string $serviceurl (Optional) The URL of the database service |
||
| 263 | * servlet |
||
| 264 | */ |
||
| 265 | public function __construct($serviceurl = self::DEFAULTDBSERVICEURL) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * getDBServiceURL |
||
| 273 | * |
||
| 274 | * Returns the full URL of the database servlet used by the call() |
||
| 275 | * function. |
||
| 276 | * |
||
| 277 | * @return string The URL of the database service servlet |
||
| 278 | */ |
||
| 279 | public function getDBServiceURL() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * setDBServiceURL |
||
| 286 | * |
||
| 287 | * Set the private variable $dbserviceurl to the full URL of the |
||
| 288 | * database servlet, which is used by the call() function. |
||
| 289 | * |
||
| 290 | * @param string $serviceurl The URL of the database service servlet. |
||
| 291 | */ |
||
| 292 | public function setDBServiceURL($serviceurl) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * clear |
||
| 299 | * |
||
| 300 | * Set all of the class members to 'null' or empty arrays. |
||
| 301 | */ |
||
| 302 | public function clear() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * clearUser |
||
| 312 | * |
||
| 313 | * Set all of the class member variables associated with getUser() |
||
| 314 | * to 'null'. |
||
| 315 | */ |
||
| 316 | public function clearUser() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * clearPortal |
||
| 337 | * |
||
| 338 | * Set all of the class member variables associated with |
||
| 339 | * getPortalParameters() to 'null'. |
||
| 340 | */ |
||
| 341 | public function clearPortal() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * clearIdps |
||
| 353 | * |
||
| 354 | * Set the class member variable $idp_uids to an empty array. |
||
| 355 | */ |
||
| 356 | public function clearIdps() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * clearClient |
||
| 364 | * |
||
| 365 | * Set all of the class member variables associated with |
||
| 366 | * getClient() to 'null'. |
||
| 367 | */ |
||
| 368 | public function clearClient() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * getUser |
||
| 379 | * |
||
| 380 | * This method calls the 'getUser' action of the servlet and sets |
||
| 381 | * the class member variables associated with user info |
||
| 382 | * appropriately. If the servlet returns correctly (i.e. an HTTP |
||
| 383 | * status code of 200), this method returns true. |
||
| 384 | * |
||
| 385 | * @param mixed $args Variable number of parameters: 1, or more. |
||
| 386 | * For 1 parameter : $uid (database user identifier) |
||
| 387 | * For more than 1 parameter, parameters can include: |
||
| 388 | * $remote_user, $idp, $idp_display_name, |
||
| 389 | * $first_name, $last_name, $display_name, $email, |
||
| 390 | * $eppn, $eptid, $openid, $oidc, $affiliation, $ou |
||
| 391 | * |
||
| 392 | * @return bool True if the servlet returned correctly. Else false. |
||
| 393 | */ |
||
| 394 | public function getUser(...$args) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * getLastArchivedUser |
||
| 437 | * |
||
| 438 | * This method calls the 'getLastArchivedUser' action of the |
||
| 439 | * servlet and sets the class member variables associated with user |
||
| 440 | * info appropriately. If the servlet returns correctly (i.e. an |
||
| 441 | * HTTP status code of 200), this method returns true. |
||
| 442 | * |
||
| 443 | * @param string $uid The database user identifier |
||
| 444 | * @return bool True if the servlet returned correctly. Else false. |
||
| 445 | */ |
||
| 446 | public function getLastArchivedUser($uid) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * removeUser |
||
| 456 | * |
||
| 457 | * This method calls the 'removeUser' action of the servlet and |
||
| 458 | * sets the class member variable $status appropriately. If the |
||
| 459 | * servlet returns correctly (i.e. an HTTP status code of 200), |
||
| 460 | * this method returns true. |
||
| 461 | * |
||
| 462 | * @param string $uid The database user identifier |
||
| 463 | * @return bool True if the servlet returned correctly. Else false. |
||
| 464 | */ |
||
| 465 | public function removeUser($uid) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * getTwoFactorInfo |
||
| 475 | * |
||
| 476 | * This method calls the 'getTwoFactorInfo' action of the servlet |
||
| 477 | * and sets the class member variables associated with the user's |
||
| 478 | * two-factor info appropriately. If the servlet returns correctly |
||
| 479 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 480 | * Note that this method isn't strictly necessary since the |
||
| 481 | * two_factor info data is returned when getUser is called. |
||
| 482 | * |
||
| 483 | * @param string $uid The database user identifier |
||
| 484 | * @return bool True if the servlet returned correctly. Else false. |
||
| 485 | */ |
||
| 486 | public function getTwoFactorInfo($uid) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * setTwoFactorInfo |
||
| 496 | * |
||
| 497 | * This method calls the 'setTwoFactorInfo' action of the servlet |
||
| 498 | * and sets the class member variable associated with the user's |
||
| 499 | * two-factor info appropriately. If the servlet returns correctly |
||
| 500 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 501 | * |
||
| 502 | * @param string $uid The database user identifier |
||
| 503 | * @param string $two_factor (Optional) The two-factor info string. |
||
| 504 | * Defaults to empty string. |
||
| 505 | * @return bool True if the servlet returned correctly. Else false. |
||
| 506 | */ |
||
| 507 | public function setTwoFactorInfo($uid, $two_factor = '') |
||
| 514 | |||
| 515 | /** |
||
| 516 | * getPortalParameters |
||
| 517 | * |
||
| 518 | * This method calls the 'getPortalParameter' action of the servlet |
||
| 519 | * and sets the class member variables associated with the portal |
||
| 520 | * parameters appropriately. If the servlet returns correctly (i.e. |
||
| 521 | * an HTTP status code of 200), this method returns true. |
||
| 522 | * |
||
| 523 | * @param string $oauth_token The database OAuth identifier token |
||
| 524 | * @return bool True if the servlet returned correctly. Else false. |
||
| 525 | */ |
||
| 526 | public function getPortalParameters($oauth_token) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * getIdps |
||
| 536 | * |
||
| 537 | * This method calls the 'getAllIdps' action of the servlet and |
||
| 538 | * sets the class member array $idp_uris to contain all of the |
||
| 539 | * Idps in the database, stored in the 'values' of the array. If |
||
| 540 | * the servlet returns correctly (i.e. an HTTP status code of 200), |
||
| 541 | * this method returns true. |
||
| 542 | * |
||
| 543 | * @return bool True if the servlet returned correctly. Else false. |
||
| 544 | */ |
||
| 545 | public function getIdps() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * setIdps |
||
| 554 | * |
||
| 555 | * This method calls the 'setAllIdps' action of the servlet using |
||
| 556 | * the class memeber array $idp_uris as the source for the Idps to |
||
| 557 | * be stored to the database. Note that if this array is empty, |
||
| 558 | * an error code will be returned in the status since at least one |
||
| 559 | * IdP should be saved to the database. If you want to pass an |
||
| 560 | * array of Idps to be saved, see the setIdpsFromKeys($array) and |
||
| 561 | * setIdpsFromValues($array) methods. If the servlet returns |
||
| 562 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 563 | * returns true. |
||
| 564 | * |
||
| 565 | * @return bool True if the servlet returned correctly. Else false. |
||
| 566 | */ |
||
| 567 | public function setIdps() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * setIdpsFromKeys |
||
| 595 | * |
||
| 596 | * This is a convenience method which calls setIdps using a |
||
| 597 | * passed-in array of IdPs stored as the keys of the array. It |
||
| 598 | * first sets the class member array $idp_uids appropriately and |
||
| 599 | * then calls the setIdps() method. If the servlet returns |
||
| 600 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 601 | * returns true. See also setIdpsFromValues(). |
||
| 602 | * |
||
| 603 | * @param array $idps An array of IdPs to be saved, stored in the |
||
| 604 | * 'keys' of the array. |
||
| 605 | * @return bool True if the servlet returned correctly. Else false. |
||
| 606 | */ |
||
| 607 | public function setIdpsFromKeys($idps) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * setIdpsFromValues |
||
| 618 | * |
||
| 619 | * This is a convenience method which calls setIdps using a |
||
| 620 | * passed-in array of IdPs stored as the values of the array. It |
||
| 621 | * first sets the class member array $idp_uids appropriately and |
||
| 622 | * then calls the setIdps() method. If the servlet returns |
||
| 623 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 624 | * returns true. See also setIdpsFromKeys(). |
||
| 625 | * |
||
| 626 | * @param array $idps An array of IdPs to be saved, stored in the |
||
| 627 | * 'values' of the array. |
||
| 628 | * @return bool True if the servlet returned correctly. Else false. |
||
| 629 | */ |
||
| 630 | public function setIdpsFromValues($idps) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * getClient |
||
| 641 | * |
||
| 642 | * This method calls the 'getClient' action of the Oauth 2.0 |
||
| 643 | * servlet and sets the class member variables associated with |
||
| 644 | * client info appropriately. If the servlet returns correctly |
||
| 645 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 646 | * |
||
| 647 | * @param string $cid The Oauth 2.0 Client ID (client_id). |
||
| 648 | * @return bool True if the servlet returned correctly. Else false. |
||
| 649 | */ |
||
| 650 | public function getClient($cid) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * setTransactionState |
||
| 660 | * |
||
| 661 | * This method calls the 'setTransactionState' action of the Oauth |
||
| 662 | * 2.0 servlet to associate the Oauth 2.0 'code' with the database |
||
| 663 | * user UID. This is necessary for the Oauth 2.0 server to be able |
||
| 664 | * to return information about the user (name, email address) as |
||
| 665 | * well as return a certificate for the user. If the servlet |
||
| 666 | * returns correctly (i.e. an HTTP status code of 200), this method |
||
| 667 | * returns true. Check the 'status' return value to verify that |
||
| 668 | * the transaction state was set successfully. |
||
| 669 | * |
||
| 670 | * @param string $code The 'code' as returned by the OAuth 2.0 server. |
||
| 671 | * @param string $uid The database user UID. |
||
| 672 | * @param int The Unix timestamp of the user authentication. |
||
| 673 | * @param string $loa (Optional) The Level of Assurance: '' = basic, |
||
| 674 | * 'openid' = OpenID Connect (e.g., Google), |
||
| 675 | * 'http://incommonfederation.org/assurance/silver' = silver |
||
| 676 | * @param string $myproxyinfo (Optional) the 'info:...' string to be |
||
| 677 | * passed to MyProxy. |
||
| 678 | * @return bool True if the servlet returned correctly. Else false. |
||
| 679 | */ |
||
| 680 | public function setTransactionState( |
||
| 698 | |||
| 699 | /** |
||
| 700 | * call |
||
| 701 | * |
||
| 702 | * This method does the brunt of the work for calling the |
||
| 703 | * dbService servlet. The single parameter is a string of |
||
| 704 | * 'key1=value1&key2=value2&...' containing all of the parameters |
||
| 705 | * for the dbService. If the servlet returns an HTTP status code |
||
| 706 | * of 200, then this method will return true. It parses the return |
||
| 707 | * output for various 'key=value' lines and stores then in the |
||
| 708 | * appropriate member variables, urldecoded of course. |
||
| 709 | * |
||
| 710 | * @param string $params A string containing 'key=value' pairs, |
||
| 711 | * separated by ampersands ('&') as appropriate for passing to a |
||
| 712 | * URL for a GET query. |
||
| 713 | * @return bool True if the servlet returned correctly. Else false. |
||
| 714 | */ |
||
| 715 | public function call($params) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * dump |
||
| 835 | * |
||
| 836 | * This is a convenience method which prints out all of the |
||
| 837 | * non-null / non-empty member variables to stdout. |
||
| 838 | */ |
||
| 839 | public function dump() |
||
| 940 | } |
||
| 941 |