| Total Complexity | 52 |
| Total Lines | 357 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Federation 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 Federation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Federation extends EntityWithDBProperties { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * the handle to the FRONTEND database (only needed for some stats access) |
||
| 48 | * |
||
| 49 | * @var DBConnection |
||
| 50 | */ |
||
| 51 | private $frontendHandle; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * the top-level domain of the Federation |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $tld; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * retrieve the statistics from the database in an internal array representation |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | private function downloadStatsCore() { |
||
| 66 | $grossAdmin = 0; |
||
| 67 | $grossUser = 0; |
||
| 68 | $grossSilverbullet = 0; |
||
| 69 | $dataArray = []; |
||
| 70 | // first, find out which profiles belong to this federation |
||
| 71 | $cohesionQuery = "SELECT downloads.device_id as dev_id, sum(downloads.downloads_user) as dl_user, sum(downloads.downloads_silverbullet) as dl_sb, sum(downloads.downloads_admin) as dl_admin FROM profile, institution, downloads WHERE profile.inst_id = institution.inst_id AND institution.country = ? AND profile.profile_id = downloads.profile_id group by device_id"; |
||
| 72 | $profilesList = $this->databaseHandle->exec($cohesionQuery, "s", $this->tld); |
||
| 73 | $deviceArray = \devices\Devices::listDevices(); |
||
| 74 | // SELECT -> resource, no boolean |
||
| 75 | while ($queryResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $profilesList)) { |
||
| 76 | $dataArray[$deviceArray[$queryResult->dev_id]['display']] = ["ADMIN" => $queryResult->dl_admin, "SILVERBULLET" => $queryResult->dl_sb, "USER" => $queryResult->dl_user]; |
||
| 77 | $grossAdmin = $grossAdmin + $queryResult->dl_admin; |
||
| 78 | $grossSilverbullet = $grossSilverbullet + $queryResult->dl_sb; |
||
| 79 | $grossUser = $grossUser + $queryResult->dl_user; |
||
| 80 | } |
||
| 81 | $dataArray["TOTAL"] = ["ADMIN" => $grossAdmin, "SILVERBULLET" => $grossSilverbullet, "USER" => $grossUser]; |
||
| 82 | return $dataArray; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * NOOP on Federations, but have to override the abstract parent method |
||
| 87 | */ |
||
| 88 | public function updateFreshness() { |
||
| 89 | // Federation is always fresh |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * gets the download statistics for the federation |
||
| 94 | * @param string $format either as an html *table* or *XML* or *JSON* |
||
| 95 | * @return string|array |
||
| 96 | */ |
||
| 97 | public function downloadStats($format) { |
||
| 98 | $data = $this->downloadStatsCore(); |
||
| 99 | $retstring = ""; |
||
| 100 | |||
| 101 | switch ($format) { |
||
| 102 | case "table": |
||
| 103 | foreach ($data as $device => $numbers) { |
||
| 104 | if ($device == "TOTAL") { |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | $retstring .= "<tr><td>$device</td><td>" . $numbers['ADMIN'] . "</td><td>" . $numbers['SILVERBULLET'] . "</td><td>" . $numbers['USER'] . "</td></tr>"; |
||
| 108 | } |
||
| 109 | $retstring .= "<tr><td><strong>TOTAL</strong></td><td><strong>" . $data['TOTAL']['ADMIN'] . "</strong></td><td><strong>" . $data['TOTAL']['SILVERBULLET'] . "</strong></td><td><strong>" . $data['TOTAL']['USER'] . "</strong></td></tr>"; |
||
| 110 | break; |
||
| 111 | case "XML": |
||
| 112 | // the calls to date() operate on current date, so there is no chance for a FALSE to be returned. Silencing scrutinizer. |
||
| 113 | $retstring .= "<federation id='$this->tld' ts='" . /** @scrutinizer ignore-type */ date("Y-m-d") . "T" . /** @scrutinizer ignore-type */ date("H:i:s") . "'>\n"; |
||
| 114 | foreach ($data as $device => $numbers) { |
||
| 115 | if ($device == "TOTAL") { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | $retstring .= " <device name='" . $device . "'>\n <downloads group='admin'>" . $numbers['ADMIN'] . "</downloads>\n <downloads group='managed_idp'>" . $numbers['SILVERBULLET'] . "</downloads>\n <downloads group='user'>" . $numbers['USER'] . "</downloads>\n </device>"; |
||
| 119 | } |
||
| 120 | $retstring .= "<total>\n <downloads group='admin'>" . $data['TOTAL']['ADMIN'] . "</downloads>\n <downloads group='managed_idp'>" . $data['TOTAL']['SILVERBULLET'] . "</downloads>\n <downloads group='user'>" . $data['TOTAL']['USER'] . "</downloads>\n</total>\n"; |
||
| 121 | $retstring .= "</federation>"; |
||
| 122 | break; |
||
| 123 | case "array": |
||
| 124 | return $data; |
||
| 125 | default: |
||
| 126 | throw new Exception("Statistics can be requested only in 'table' or 'XML' format!"); |
||
| 127 | } |
||
| 128 | |||
| 129 | return $retstring; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * |
||
| 134 | * Constructs a Federation object. |
||
| 135 | * |
||
| 136 | * @param string $fedname - textual representation of the Federation object |
||
| 137 | * Example: "lu" (for Luxembourg) |
||
| 138 | */ |
||
| 139 | public function __construct($fedname) { |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Creates a new IdP inside the federation. |
||
| 176 | * |
||
| 177 | * @param string $ownerId Persistent identifier of the user for whom this IdP is created (first administrator) |
||
| 178 | * @param string $level Privilege level of the first administrator (was he blessed by a federation admin or a peer?) |
||
| 179 | * @param string $mail e-mail address with which the user was invited to administer (useful for later user identification if the user chooses a "funny" real name) |
||
| 180 | * @return int identifier of the new IdP |
||
| 181 | */ |
||
| 182 | public function newIdP($ownerId, $level, $mail = NULL) { |
||
| 183 | $this->databaseHandle->exec("INSERT INTO institution (country) VALUES('$this->tld')"); |
||
| 184 | $identifier = $this->databaseHandle->lastID(); |
||
| 185 | |||
| 186 | if ($identifier == 0 || !$this->loggerInstance->writeAudit($ownerId, "NEW", "IdP $identifier")) { |
||
| 187 | $text = "<p>Could not create a new " . CONFIG_CONFASSISTANT['CONSORTIUM']['nomenclature_inst'] . "!</p>"; |
||
| 188 | echo $text; |
||
| 189 | throw new Exception($text); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ($ownerId != "PENDING") { |
||
| 193 | if ($mail === NULL) { |
||
| 194 | throw new Exception("New IdPs in a federation need a mail address UNLESS created by API without OwnerId"); |
||
| 195 | } |
||
| 196 | $this->databaseHandle->exec("INSERT INTO ownership (user_id,institution_id, blesslevel, orig_mail) VALUES(?,?,?,?)", "siss", $ownerId, $identifier, $level, $mail); |
||
| 197 | } |
||
| 198 | return $identifier; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Lists all Identity Providers in this federation |
||
| 203 | * |
||
| 204 | * @param int $activeOnly if set to non-zero will list only those institutions which have some valid profiles defined. |
||
| 205 | * @return array (Array of IdP instances) |
||
| 206 | * |
||
| 207 | */ |
||
| 208 | public function listIdentityProviders($activeOnly = 0) { |
||
| 209 | // default query is: |
||
| 210 | $allIDPs = $this->databaseHandle->exec("SELECT inst_id FROM institution |
||
| 211 | WHERE country = '$this->tld' ORDER BY inst_id"); |
||
| 212 | // the one for activeOnly is much more complex: |
||
| 213 | if ($activeOnly) { |
||
| 214 | $allIDPs = $this->databaseHandle->exec("SELECT distinct institution.inst_id AS inst_id |
||
| 215 | FROM institution |
||
| 216 | JOIN profile ON institution.inst_id = profile.inst_id |
||
| 217 | WHERE institution.country = '$this->tld' |
||
| 218 | AND profile.showtime = 1 |
||
| 219 | ORDER BY inst_id"); |
||
| 220 | } |
||
| 221 | |||
| 222 | $returnarray = []; |
||
| 223 | // SELECT -> resource, not boolean |
||
| 224 | while ($idpQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allIDPs)) { |
||
| 225 | $idp = new IdP($idpQuery->inst_id); |
||
| 226 | $name = $idp->name; |
||
| 227 | $idpInfo = ['entityID' => $idp->identifier, |
||
| 228 | 'title' => $name, |
||
| 229 | 'country' => strtoupper($idp->federation), |
||
| 230 | 'instance' => $idp]; |
||
| 231 | $returnarray[$idp->identifier] = $idpInfo; |
||
| 232 | } |
||
| 233 | return $returnarray; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * returns an array with information about the authorised administrators of the federation |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function listFederationAdmins() { |
||
| 242 | $returnarray = []; |
||
| 243 | $query = "SELECT user_id FROM user_options WHERE option_name = 'user:fedadmin' AND option_value = ?"; |
||
| 244 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
| 245 | $query = "SELECT eptid as user_id FROM view_admin WHERE role = 'fedadmin' AND realm = ?"; |
||
| 246 | } |
||
| 247 | $userHandle = DBConnection::handle("USER"); // we need something from the USER database for a change |
||
| 248 | $upperFed = strtoupper($this->tld); |
||
| 249 | // SELECT -> resource, not boolean |
||
| 250 | $admins = $userHandle->exec($query, "s", $upperFed); |
||
| 251 | |||
| 252 | while ($fedAdminQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $admins)) { |
||
| 253 | $returnarray[] = $fedAdminQuery->user_id; |
||
| 254 | } |
||
| 255 | return $returnarray; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * cross-checks in the EXTERNAL customer DB which institutions exist there for the federations |
||
| 260 | * |
||
| 261 | * @param bool $unmappedOnly if set to TRUE, only returns those which do not have a known mapping to our internally known institutions |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function listExternalEntities($unmappedOnly) { |
||
| 333 | } |
||
| 334 | |||
| 335 | const UNKNOWN_IDP = -1; |
||
| 336 | const AMBIGUOUS_IDP = -2; |
||
| 337 | |||
| 338 | /** |
||
| 339 | * for a MySQL list of institutions, find an institution or find out that |
||
| 340 | * there is no single best match |
||
| 341 | * |
||
| 342 | * @param \mysqli_result $dbResult |
||
| 343 | * @param string $country used to return the country of the inst, if can be found out |
||
| 344 | * @return int the identifier of the inst, or one of the special return values if unsuccessful |
||
| 345 | */ |
||
| 346 | private static function findCandidates(\mysqli_result $dbResult, &$country) { |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * If we are running diagnostics, our input from the user is the realm. We |
||
| 366 | * need to find out which IdP this realm belongs to. |
||
| 367 | * @param string $realm the realm to search for |
||
| 368 | * @return array an array with two entries, CAT ID and DB ID, with either the respective ID of the IdP in the system, or UNKNOWN_IDP or AMBIGUOUS_IDP |
||
| 369 | */ |
||
| 370 | public static function determineIdPIdByRealm($realm) { |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * helper function to sort institutions by their name |
||
| 395 | * @param array $a an array with institution a's information |
||
| 396 | * @param array $b an array with institution b's information |
||
| 397 | * @return int the comparison result |
||
| 398 | */ |
||
| 399 | private function usortInstitution($a, $b) { |
||
| 401 | } |
||
| 402 | |||
| 403 | } |
||
| 404 |