Complex classes like Api 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 Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Api |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Checks a key for validity and KillLog access. |
||
| 27 | * |
||
| 28 | * @static |
||
| 29 | * @param $keyID int The keyID to be checked. |
||
| 30 | * @param $vCode string The vCode to be checked |
||
| 31 | * @return string A message, Success on success, otherwise an error. |
||
| 32 | */ |
||
| 33 | public static function checkAPI($keyID, $vCode) |
||
| 34 | { |
||
| 35 | $keyID = trim($keyID); |
||
| 36 | $vCode = trim($vCode); |
||
| 37 | if ($keyID == "" || $vCode == "") |
||
| 38 | return "Error, no keyID and/or vCode"; |
||
| 39 | $keyID = (int)$keyID; |
||
| 40 | if ($keyID == 0) { |
||
| 41 | return "Invalid keyID. Did you get the keyID and vCode mixed up?"; |
||
| 42 | } |
||
| 43 | |||
| 44 | $pheal = Util::getPheal($keyID, $vCode); |
||
| 45 | try |
||
| 46 | { |
||
| 47 | $result = $pheal->accountScope->APIKeyInfo(); |
||
| 48 | } |
||
| 49 | catch (Exception $e) |
||
| 50 | { |
||
| 51 | if (strlen($keyID) > 20) |
||
| 52 | return "Error, you might have mistaken keyid for the vcode"; |
||
| 53 | return "Error: " . $e->getCode() . " Message: " . $e->getMessage(); |
||
| 54 | } |
||
| 55 | |||
| 56 | $key = $result->key; |
||
| 57 | $accessMask = $key->accessMask; |
||
| 58 | $hasBits = self::hasBits($accessMask); |
||
| 59 | |||
| 60 | if (!$hasBits) { |
||
| 61 | return "Error, key does not have access to killlog, please modify key to add killlog access"; |
||
| 62 | } |
||
| 63 | return "success"; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Adds a key to the database. |
||
| 68 | * |
||
| 69 | * @static |
||
| 70 | * @param int $keyID |
||
| 71 | * @param string $vCode |
||
| 72 | * @param null|string $label |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public static function addKey($keyID, $vCode, $label = null) |
||
| 76 | { |
||
| 77 | $userID = User::getUserID(); |
||
| 78 | if ($userID == null) $userID = 0; |
||
|
|
|||
| 79 | |||
| 80 | $exists = Db::queryRow("SELECT userID, keyID, vCode FROM zz_api WHERE keyID = :keyID AND vCode = :vCode", array(":keyID" => $keyID, ":vCode" => $vCode), 0); |
||
| 81 | if ($exists == null) { |
||
| 82 | // Insert the api key |
||
| 83 | Db::execute("replace into zz_api (userID, keyID, vCode, label) VALUES (:userID, :keyID, :vCode, :label)", array(":userID" => $userID, ":keyID" => $keyID, ":vCode" => $vCode, ":label" => $label)); |
||
| 84 | } else if ($exists["userID"] == 0) { |
||
| 85 | // Someone already gave us this key anonymously, give it to this user |
||
| 86 | Db::execute("UPDATE zz_api SET userID = :userID, label = :label WHERE keyID = :keyID", array(":userID" => $userID, ":label" => $label, ":keyID" => $keyID)); |
||
| 87 | return "keyID $keyID previously existed in our database but has now been assigned to you."; |
||
| 88 | } else { |
||
| 89 | return "keyID $keyID is already in the database..."; |
||
| 90 | } |
||
| 91 | |||
| 92 | $pheal = Util::getPheal($keyID, $vCode); |
||
| 93 | $result = $pheal->accountScope->APIKeyInfo(); |
||
| 94 | $key = $result->key; |
||
| 95 | $keyType = $key->type; |
||
| 96 | |||
| 97 | if ($keyType == "Account") $keyType = "Character"; |
||
| 98 | |||
| 99 | $ip = IP::get(); |
||
| 100 | |||
| 101 | Log::log("API: $keyID has been added. Type: $keyType ($ip)"); |
||
| 102 | return "Success, your $keyType key has been added."; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Deletes a key owned by the currently logged in user. |
||
| 107 | * |
||
| 108 | * @static |
||
| 109 | * @param $keyID int |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public static function deleteKey($keyID) |
||
| 113 | { |
||
| 114 | $userID = user::getUserID(); |
||
| 115 | Db::execute("DELETE FROM zz_api_characters WHERE keyID = :keyID", array(":keyID" => $keyID)); |
||
| 116 | Db::execute("DELETE FROM zz_api WHERE userID = :userID AND keyID = :keyID", array(":userID" => $userID, ":keyID" => $keyID)); |
||
| 117 | return "$keyID has been deleted"; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns a list of keys owned by the currently logged in user. |
||
| 122 | * |
||
| 123 | * @static |
||
| 124 | * @param $userID int |
||
| 125 | * @return array Returns |
||
| 126 | */ |
||
| 127 | public static function getKeys($userID) |
||
| 128 | { |
||
| 129 | if(!isset($userID)) |
||
| 130 | $userID = user::getUserID(); |
||
| 131 | |||
| 132 | $result = Db::query("SELECT keyID, vCode, label, lastValidation, errorCode FROM zz_api WHERE userID = :userID order by keyID", array(":userID" => $userID), 0); |
||
| 133 | return $result; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns an array of character keys. |
||
| 138 | * |
||
| 139 | * @static |
||
| 140 | * @param $userID int |
||
| 141 | * @return array Returns |
||
| 142 | */ |
||
| 143 | public static function getCharacterKeys($userID) |
||
| 144 | { |
||
| 145 | $result = Db::query("select c.* from zz_api_characters c left join zz_api a on (c.keyID = a.keyID) where a.userID = :userID", array(":userID" => $userID), 0); |
||
| 146 | return $result; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns an array of the characters assigned to this user. |
||
| 151 | * |
||
| 152 | * @static |
||
| 153 | * @param $userID int |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public static function getCharacters($userID) |
||
| 157 | { |
||
| 158 | $db = Db::query("SELECT characterID FROM zz_api_characters c left join zz_api a on (c.keyID = a.keyID) where userID = :userID", array(":userID" => $userID), 0); |
||
| 159 | $results = Info::addInfo($db); |
||
| 160 | return $results; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Tests the access mask for KillLog access |
||
| 165 | * |
||
| 166 | * @static |
||
| 167 | * @param int $accessMask |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public static function hasBits($accessMask) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * API exception handling |
||
| 177 | * |
||
| 178 | * @static |
||
| 179 | * @param integer $keyID |
||
| 180 | * @param int $charID |
||
| 181 | * @param Exception $exception |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public static function handleApiException($keyID, $charID, $exception) |
||
| 304 | |||
| 305 | public static function fetchApis() |
||
| 327 | |||
| 328 | public static function doApiSummary() |
||
| 329 | { |
||
| 330 | $lastActualKills = Db::queryField("select contents count from zz_storage where locker = 'actualKills'", "count", array(), 0); |
||
| 331 | $actualKills = Db::queryField("select count(*) count from zz_killmails where processed = 1", "count", array(), 0); |
||
| 332 | |||
| 333 | $lastTotalKills = Db::queryField("select contents count from zz_storage where locker = 'totalKills'", "count", array(), 0); |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $keyID string |
||
| 348 | * @param $charID int |
||
| 349 | * @param $killlog string |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | public static function processRawApi($keyID, $charID, $killlog) |
||
| 371 | } |
||
| 372 |