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 OAuth2 .war. |
||
| 65 | */ |
||
| 66 | const DEFAULTDBSERVICEURL = 'http://localhost:8080/oauth2/dbService'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string OAUTH1DBSERVICEURL The URL for the OAuth 1.0a dbService. |
||
| 70 | */ |
||
| 71 | const OAUTH1DBSERVICEURL = 'http://localhost:8080/oauth/dbService'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string OAUTH2DBSERVICEURL The URL for the OAuth 2 dbService. |
||
| 75 | */ |
||
| 76 | const OAUTH2DBSERVICEURL = 'http://localhost:8080/oauth2/dbService'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array $STATUS The various STATUS_* constants, originally from |
||
| 80 | * Store.pm. The keys of the array are strings corresponding to the |
||
| 81 | * constant names. The values of the array are the integer (hex) |
||
| 82 | * values. For example, DBService::$STATUS['STATUS_OK'] = 0; |
||
| 83 | * Use 'array_search($this->status,DBService::$STATUS)' to look |
||
| 84 | * up the STATUS_* name given the status integer value. |
||
| 85 | */ |
||
| 86 | public static $STATUS = array( |
||
| 87 | 'STATUS_OK' => 0x0, |
||
| 88 | 'STATUS_ACTION_NOT_FOUND' => 0x1, |
||
| 89 | 'STATUS_NEW_USER' => 0x2, |
||
| 90 | 'STATUS_USER_UPDATED' => 0x4, |
||
| 91 | 'STATUS_USER_NOT_FOUND' => 0x6, |
||
| 92 | 'STATUS_USER_EXISTS' => 0x8, |
||
| 93 | 'STATUS_USER_EXISTS_ERROR' => 0xFFFA1, |
||
| 94 | 'STATUS_USER_NOT_FOUND_ERROR' => 0xFFFA3, |
||
| 95 | 'STATUS_TRANSACTION_NOT_FOUND' => 0xFFFA5, |
||
| 96 | 'STATUS_IDP_SAVE_FAILED' => 0xFFFA7, |
||
| 97 | 'STATUS_DUPLICATE_PARAMETER_FOUND' => 0xFFFF1, |
||
| 98 | 'STATUS_INTERNAL_ERROR' => 0xFFFF3, |
||
| 99 | 'STATUS_SAVE_IDP_FAILED' => 0xFFFF5, |
||
| 100 | 'STATUS_MALFORMED_INPUT_ERROR' => 0xFFFF7, |
||
| 101 | 'STATUS_MISSING_PARAMETER_ERROR' => 0xFFFF9, |
||
| 102 | 'STATUS_NO_REMOTE_USER' => 0xFFFFB, |
||
| 103 | 'STATUS_NO_IDENTITY_PROVIDER' => 0xFFFFD, |
||
| 104 | 'STATUS_CLIENT_NOT_FOUND' => 0xFFFFF, |
||
| 105 | 'STATUS_TRANSACTION_NOT_FOUND' => 0x10001, |
||
| 106 | 'STATUS_EPTID_MISMATCH' => 0x100001, |
||
| 107 | ); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int $status The returned status code from dbService calls |
||
| 111 | */ |
||
| 112 | public $status; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string $user_uid The CILogon UID |
||
| 116 | */ |
||
| 117 | public $user_uid; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var string $remote_user The HTTP session REMOTE_USER |
||
| 121 | */ |
||
| 122 | public $remote_user; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string $idp The Identity Provider's entityId |
||
| 126 | */ |
||
| 127 | public $idp; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string $idp_display_name The Identity Provider's name |
||
| 131 | */ |
||
| 132 | public $idp_display_name; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string $first_name User's given name |
||
| 136 | */ |
||
| 137 | public $first_name; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var string $last_name User's family name |
||
| 141 | */ |
||
| 142 | public $last_name; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var string $display_name User's full name |
||
| 146 | */ |
||
| 147 | public $display_name; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var string $email User's email address |
||
| 151 | */ |
||
| 152 | public $email; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var string $distinguished_name X.509 DN + email address |
||
| 156 | */ |
||
| 157 | public $distinguished_name; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var string $eppn eduPersonPrincipalName |
||
| 161 | */ |
||
| 162 | public $eppn; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var string $eptid eduPersonTargetedID |
||
| 166 | */ |
||
| 167 | public $eptid; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var string $open_id Old Google OpenID 2.0 identifier |
||
| 171 | */ |
||
| 172 | public $open_id; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var string $oidc OpenID Connect identifier |
||
| 176 | */ |
||
| 177 | public $oidc; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var string $affiliation eduPersonScopedAffiliation |
||
| 181 | */ |
||
| 182 | public $affiliation; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var string $ou Organizational Unit |
||
| 186 | */ |
||
| 187 | public $ou; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var string $member_of isMemberOf group information |
||
| 191 | */ |
||
| 192 | public $member_of; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var string $acr Authentication Context Class Ref |
||
| 196 | */ |
||
| 197 | public $acr; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var string $entitlement eduPersonEntitlement |
||
| 201 | */ |
||
| 202 | public $entitlement; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var string $serial_string CILogon serial string (e.g., A34201) |
||
| 206 | */ |
||
| 207 | public $serial_string; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @var string $create_time Time user entry was created |
||
| 211 | */ |
||
| 212 | public $create_time; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var string $oauth_token OAuth 2.0 token |
||
| 216 | */ |
||
| 217 | public $oauth_token; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @var string $cilogon_callback OAuth 1.0a callback URL |
||
| 221 | */ |
||
| 222 | public $cilogon_callback; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var string $cilogon_success OAuth 1.0a success URL |
||
| 226 | */ |
||
| 227 | public $cilogon_success; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @var string $cilogon_failure OAuth 1.0a failure URL |
||
| 231 | */ |
||
| 232 | public $cilogon_failure; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @var string $cilogon_portal_name OAuth client name |
||
| 236 | */ |
||
| 237 | public $cilogon_portal_name; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @var string $two_factor Two factor string used by TwoFactor.php |
||
| 241 | */ |
||
| 242 | public $two_factor; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @var array $idp_uids IdPs stored in the 'values' of the array |
||
| 246 | */ |
||
| 247 | public $idp_uids; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @var string $client_name OAuth 2.0 client name |
||
| 251 | */ |
||
| 252 | public $client_name; |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @var string $client_id OAuth 2.0 client identifier |
||
| 256 | */ |
||
| 257 | public $client_id; |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @var string $client_home_uri OAuth 2.0 client home URL |
||
| 261 | */ |
||
| 262 | public $client_home_uri; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @var array $client_callback_uris An array of OAuth 2.0 callback URLs |
||
| 266 | */ |
||
| 267 | public $client_callback_uris; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @var string $dbservice URL The URL to use for the dbService |
||
| 271 | */ |
||
| 272 | private $dbserviceurl; |
||
| 273 | |||
| 274 | /** |
||
| 275 | * __construct |
||
| 276 | * |
||
| 277 | * Default constructor. All of the various class members are |
||
| 278 | * initialized to 'null' or empty arrays. |
||
| 279 | * |
||
| 280 | * @param string $serviceurl (Optional) The URL of the database service |
||
| 281 | * servlet |
||
| 282 | */ |
||
| 283 | public function __construct($serviceurl = self::DEFAULTDBSERVICEURL) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * getDBServiceURL |
||
| 291 | * |
||
| 292 | * Returns the full URL of the database servlet used by the call() |
||
| 293 | * function. |
||
| 294 | * |
||
| 295 | * @return string The URL of the database service servlet |
||
| 296 | */ |
||
| 297 | public function getDBServiceURL() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * setDBServiceURL |
||
| 304 | * |
||
| 305 | * Set the private variable $dbserviceurl to the full URL of the |
||
| 306 | * database servlet, which is used by the call() function. |
||
| 307 | * |
||
| 308 | * @param string $serviceurl The URL of the database service servlet. |
||
| 309 | */ |
||
| 310 | public function setDBServiceURL($serviceurl) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * clear |
||
| 317 | * |
||
| 318 | * Set all of the class members to 'null' or empty arrays. |
||
| 319 | */ |
||
| 320 | public function clear() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * clearUser |
||
| 330 | * |
||
| 331 | * Set all of the class member variables associated with getUser() |
||
| 332 | * to 'null'. |
||
| 333 | */ |
||
| 334 | public function clearUser() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * clearPortal |
||
| 362 | * |
||
| 363 | * Set all of the class member variables associated with |
||
| 364 | * getPortalParameters() to 'null'. |
||
| 365 | */ |
||
| 366 | public function clearPortal() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * clearIdps |
||
| 378 | * |
||
| 379 | * Set the class member variable $idp_uids to an empty array. |
||
| 380 | */ |
||
| 381 | public function clearIdps() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * clearClient |
||
| 389 | * |
||
| 390 | * Set all of the class member variables associated with |
||
| 391 | * getClient() to 'null'. |
||
| 392 | */ |
||
| 393 | public function clearClient() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * getUser |
||
| 404 | * |
||
| 405 | * This method calls the 'getUser' action of the servlet and sets |
||
| 406 | * the class member variables associated with user info |
||
| 407 | * appropriately. If the servlet returns correctly (i.e. an HTTP |
||
| 408 | * status code of 200), this method returns true. |
||
| 409 | * |
||
| 410 | * @param mixed $args Variable number of parameters: 1, or more. |
||
| 411 | * For 1 parameter : $uid (database user identifier) |
||
| 412 | * For more than 1 parameter, parameters can include: |
||
| 413 | * $remote_user, $idp, $idp_display_name, |
||
| 414 | * $first_name, $last_name, $display_name, $email, |
||
| 415 | * $eppn, $eptid, $openid, $oidc, $affiliation, |
||
| 416 | * $ou, $member_of, $acr, $entitlement |
||
| 417 | * |
||
| 418 | * @return bool True if the servlet returned correctly. Else false. |
||
| 419 | */ |
||
| 420 | public function getUser(...$args) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * getLastArchivedUser |
||
| 471 | * |
||
| 472 | * This method calls the 'getLastArchivedUser' action of the |
||
| 473 | * servlet and sets the class member variables associated with user |
||
| 474 | * info appropriately. If the servlet returns correctly (i.e. an |
||
| 475 | * HTTP status code of 200), this method returns true. |
||
| 476 | * |
||
| 477 | * @param string $uid The database user identifier |
||
| 478 | * @return bool True if the servlet returned correctly. Else false. |
||
| 479 | */ |
||
| 480 | public function getLastArchivedUser($uid) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * removeUser |
||
| 490 | * |
||
| 491 | * This method calls the 'removeUser' action of the servlet and |
||
| 492 | * sets the class member variable $status appropriately. If the |
||
| 493 | * servlet returns correctly (i.e. an HTTP status code of 200), |
||
| 494 | * this method returns true. |
||
| 495 | * |
||
| 496 | * @param string $uid The database user identifier |
||
| 497 | * @return bool True if the servlet returned correctly. Else false. |
||
| 498 | */ |
||
| 499 | public function removeUser($uid) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * getTwoFactorInfo |
||
| 509 | * |
||
| 510 | * This method calls the 'getTwoFactorInfo' action of the servlet |
||
| 511 | * and sets the class member variables associated with the user's |
||
| 512 | * two-factor info appropriately. If the servlet returns correctly |
||
| 513 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 514 | * Note that this method isn't strictly necessary since the |
||
| 515 | * two_factor info data is returned when getUser is called. |
||
| 516 | * |
||
| 517 | * @param string $uid The database user identifier |
||
| 518 | * @return bool True if the servlet returned correctly. Else false. |
||
| 519 | */ |
||
| 520 | public function getTwoFactorInfo($uid) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * setTwoFactorInfo |
||
| 530 | * |
||
| 531 | * This method calls the 'setTwoFactorInfo' action of the servlet |
||
| 532 | * and sets the class member variable associated with the user's |
||
| 533 | * two-factor info appropriately. If the servlet returns correctly |
||
| 534 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 535 | * |
||
| 536 | * @param string $uid The database user identifier |
||
| 537 | * @param string $two_factor (Optional) The two-factor info string. |
||
| 538 | * Defaults to empty string. |
||
| 539 | * @return bool True if the servlet returned correctly. Else false. |
||
| 540 | */ |
||
| 541 | public function setTwoFactorInfo($uid, $two_factor = '') |
||
| 548 | |||
| 549 | /** |
||
| 550 | * getPortalParameters |
||
| 551 | * |
||
| 552 | * This method calls the 'getPortalParameter' action of the servlet |
||
| 553 | * and sets the class member variables associated with the portal |
||
| 554 | * parameters appropriately. If the servlet returns correctly (i.e. |
||
| 555 | * an HTTP status code of 200), this method returns true. |
||
| 556 | * |
||
| 557 | * @param string $oauth_token The database OAuth identifier token |
||
| 558 | * @return bool True if the servlet returned correctly. Else false. |
||
| 559 | */ |
||
| 560 | public function getPortalParameters($oauth_token) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * getIdps |
||
| 570 | * |
||
| 571 | * This method calls the 'getAllIdps' action of the servlet and |
||
| 572 | * sets the class member array $idp_uris to contain all of the |
||
| 573 | * Idps in the database, stored in the 'values' of the array. If |
||
| 574 | * the servlet returns correctly (i.e. an HTTP status code of 200), |
||
| 575 | * this method returns true. |
||
| 576 | * |
||
| 577 | * @return bool True if the servlet returned correctly. Else false. |
||
| 578 | */ |
||
| 579 | public function getIdps() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * setIdps |
||
| 588 | * |
||
| 589 | * This method calls the 'setAllIdps' action of the servlet using |
||
| 590 | * the class memeber array $idp_uris as the source for the Idps to |
||
| 591 | * be stored to the database. Note that if this array is empty, |
||
| 592 | * an error code will be returned in the status since at least one |
||
| 593 | * IdP should be saved to the database. If you want to pass an |
||
| 594 | * array of Idps to be saved, see the setIdpsFromKeys($array) and |
||
| 595 | * setIdpsFromValues($array) methods. If the servlet returns |
||
| 596 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 597 | * returns true. |
||
| 598 | * |
||
| 599 | * @return bool True if the servlet returned correctly. Else false. |
||
| 600 | */ |
||
| 601 | public function setIdps() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * setIdpsFromKeys |
||
| 629 | * |
||
| 630 | * This is a convenience method which calls setIdps using a |
||
| 631 | * passed-in array of IdPs stored as the keys of the array. It |
||
| 632 | * first sets the class member array $idp_uids appropriately and |
||
| 633 | * then calls the setIdps() method. If the servlet returns |
||
| 634 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 635 | * returns true. See also setIdpsFromValues(). |
||
| 636 | * |
||
| 637 | * @param array $idps An array of IdPs to be saved, stored in the |
||
| 638 | * 'keys' of the array. |
||
| 639 | * @return bool True if the servlet returned correctly. Else false. |
||
| 640 | */ |
||
| 641 | public function setIdpsFromKeys($idps) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * setIdpsFromValues |
||
| 652 | * |
||
| 653 | * This is a convenience method which calls setIdps using a |
||
| 654 | * passed-in array of IdPs stored as the values of the array. It |
||
| 655 | * first sets the class member array $idp_uids appropriately and |
||
| 656 | * then calls the setIdps() method. If the servlet returns |
||
| 657 | * correctly (i.e. an HTTP status code of 200), this method |
||
| 658 | * returns true. See also setIdpsFromKeys(). |
||
| 659 | * |
||
| 660 | * @param array $idps An array of IdPs to be saved, stored in the |
||
| 661 | * 'values' of the array. |
||
| 662 | * @return bool True if the servlet returned correctly. Else false. |
||
| 663 | */ |
||
| 664 | public function setIdpsFromValues($idps) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * getClient |
||
| 675 | * |
||
| 676 | * This method calls the 'getClient' action of the Oauth 2.0 |
||
| 677 | * servlet and sets the class member variables associated with |
||
| 678 | * client info appropriately. If the servlet returns correctly |
||
| 679 | * (i.e. an HTTP status code of 200), this method returns true. |
||
| 680 | * |
||
| 681 | * @param string $cid The Oauth 2.0 Client ID (client_id). |
||
| 682 | * @return bool True if the servlet returned correctly. Else false. |
||
| 683 | */ |
||
| 684 | public function getClient($cid) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * setTransactionState |
||
| 694 | * |
||
| 695 | * This method calls the 'setTransactionState' action of the Oauth |
||
| 696 | * 2.0 servlet to associate the Oauth 2.0 'code' with the database |
||
| 697 | * user UID. This is necessary for the Oauth 2.0 server to be able |
||
| 698 | * to return information about the user (name, email address) as |
||
| 699 | * well as return a certificate for the user. If the servlet |
||
| 700 | * returns correctly (i.e. an HTTP status code of 200), this method |
||
| 701 | * returns true. Check the 'status' return value to verify that |
||
| 702 | * the transaction state was set successfully. |
||
| 703 | * |
||
| 704 | * @param string $code The 'code' as returned by the OAuth 2.0 server. |
||
| 705 | * @param string $uid The database user UID. |
||
| 706 | * @param int The Unix timestamp of the user authentication. |
||
| 707 | * @param string $loa (Optional) The Level of Assurance: '' = basic, |
||
| 708 | * 'openid' = OpenID Connect (e.g., Google), |
||
| 709 | * 'http://incommonfederation.org/assurance/silver' = silver |
||
| 710 | * @param string $myproxyinfo (Optional) the 'info:...' string to be |
||
| 711 | * passed to MyProxy. |
||
| 712 | * @return bool True if the servlet returned correctly. Else false. |
||
| 713 | */ |
||
| 714 | public function setTransactionState( |
||
| 732 | |||
| 733 | /** |
||
| 734 | * call |
||
| 735 | * |
||
| 736 | * This method does the brunt of the work for calling the |
||
| 737 | * dbService servlet. The single parameter is a string of |
||
| 738 | * 'key1=value1&key2=value2&...' containing all of the parameters |
||
| 739 | * for the dbService. If the servlet returns an HTTP status code |
||
| 740 | * of 200, then this method will return true. It parses the return |
||
| 741 | * output for various 'key=value' lines and stores then in the |
||
| 742 | * appropriate member variables, urldecoded of course. |
||
| 743 | * |
||
| 744 | * @param string $params A string containing 'key=value' pairs, |
||
| 745 | * separated by ampersands ('&') as appropriate for passing to a |
||
| 746 | * URL for a GET query. |
||
| 747 | * @return bool True if the servlet returned correctly. Else false. |
||
| 748 | */ |
||
| 749 | public function call($params) |
||
| 888 | |||
| 889 | /** |
||
| 890 | * dump |
||
| 891 | * |
||
| 892 | * This is a convenience method which prints out all of the |
||
| 893 | * non-null / non-empty member variables to stdout. |
||
| 894 | */ |
||
| 895 | public function dump() |
||
| 1005 | } |
||
| 1006 |