| Total Complexity | 64 |
| Total Lines | 429 |
| 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 |
||
| 55 | class Federation extends EntityWithDBProperties { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * the handle to the FRONTEND database (only needed for some stats access) |
||
| 59 | * |
||
| 60 | * @var DBConnection |
||
| 61 | */ |
||
| 62 | private $frontendHandle; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * the top-level domain of the Federation |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $tld; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * retrieve the statistics from the database in an internal array representation |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | private function downloadStatsCore() { |
||
| 77 | $grossAdmin = 0; |
||
| 78 | $grossUser = 0; |
||
| 79 | $grossSilverbullet = 0; |
||
| 80 | $dataArray = []; |
||
| 81 | // first, find out which profiles belong to this federation |
||
| 82 | $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"; |
||
| 83 | $profilesList = $this->databaseHandle->exec($cohesionQuery, "s", $this->tld); |
||
| 84 | $deviceArray = \devices\Devices::listDevices(); |
||
| 85 | // SELECT -> resource, no boolean |
||
| 86 | while ($queryResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $profilesList)) { |
||
| 87 | if (isset($deviceArray[$queryResult->dev_id])) { |
||
| 88 | $displayName = $deviceArray[$queryResult->dev_id]['display']; |
||
| 89 | } else { // this device has stats, but doesn't exist in current config. We don't even know its display name, so display its raw representation |
||
| 90 | $displayName = sprintf(_("(discontinued) %s"), $queryResult->dev_id); |
||
| 91 | } |
||
| 92 | $dataArray[$displayName] = ["ADMIN" => $queryResult->dl_admin, "SILVERBULLET" => $queryResult->dl_sb, "USER" => $queryResult->dl_user]; |
||
| 93 | $grossAdmin = $grossAdmin + $queryResult->dl_admin; |
||
| 94 | $grossSilverbullet = $grossSilverbullet + $queryResult->dl_sb; |
||
| 95 | $grossUser = $grossUser + $queryResult->dl_user; |
||
| 96 | } |
||
| 97 | $dataArray["TOTAL"] = ["ADMIN" => $grossAdmin, "SILVERBULLET" => $grossSilverbullet, "USER" => $grossUser]; |
||
| 98 | return $dataArray; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * when a Federation attribute changes, invalidate caches of all IdPs |
||
| 103 | * in that federation (e.g. change of fed logo changes the actual |
||
| 104 | * installers) |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function updateFreshness() { |
||
| 109 | $idplist = $this->listIdentityProviders(); |
||
| 110 | foreach ($idplist as $idpDetail) { |
||
| 111 | $idpDetail['instance']->updateFreshness(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * gets the download statistics for the federation |
||
| 117 | * @param string $format either as an html *table* or *XML* or *JSON* |
||
| 118 | * @return string|array |
||
| 119 | */ |
||
| 120 | public function downloadStats($format) { |
||
| 121 | $data = $this->downloadStatsCore(); |
||
| 122 | $retstring = ""; |
||
| 123 | |||
| 124 | switch ($format) { |
||
| 125 | case "table": |
||
| 126 | foreach ($data as $device => $numbers) { |
||
| 127 | if ($device == "TOTAL") { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | $retstring .= "<tr><td>$device</td><td>" . $numbers['ADMIN'] . "</td><td>" . $numbers['SILVERBULLET'] . "</td><td>" . $numbers['USER'] . "</td></tr>"; |
||
| 131 | } |
||
| 132 | $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>"; |
||
| 133 | break; |
||
| 134 | case "XML": |
||
| 135 | // the calls to date() operate on current date, so there is no chance for a FALSE to be returned. Silencing scrutinizer. |
||
| 136 | $retstring .= "<federation id='$this->tld' ts='" . /** @scrutinizer ignore-type */ date("Y-m-d") . "T" . /** @scrutinizer ignore-type */ date("H:i:s") . "'>\n"; |
||
| 137 | foreach ($data as $device => $numbers) { |
||
| 138 | if ($device == "TOTAL") { |
||
| 139 | continue; |
||
| 140 | } |
||
| 141 | $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>"; |
||
| 142 | } |
||
| 143 | $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"; |
||
| 144 | $retstring .= "</federation>"; |
||
| 145 | break; |
||
| 146 | case "array": |
||
| 147 | return $data; |
||
| 148 | default: |
||
| 149 | throw new Exception("Statistics can be requested only in 'table' or 'XML' format!"); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $retstring; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * |
||
| 157 | * Constructs a Federation object. |
||
| 158 | * |
||
| 159 | * @param string $fedname textual representation of the Federation object |
||
| 160 | * Example: "lu" (for Luxembourg) |
||
| 161 | */ |
||
| 162 | public function __construct($fedname) { |
||
| 163 | |||
| 164 | // initialise the superclass variables |
||
| 165 | |||
| 166 | $this->databaseType = "INST"; |
||
| 167 | $this->entityOptionTable = "federation_option"; |
||
| 168 | $this->entityIdColumn = "federation_id"; |
||
| 169 | |||
| 170 | $cat = new CAT(); |
||
| 171 | if (!isset($cat->knownFederations[$fedname])) { |
||
| 172 | throw new Exception("This federation is not known to the system!"); |
||
| 173 | } |
||
| 174 | $this->identifier = 0; // we do not use the numeric ID of a federation |
||
| 175 | $this->tld = $fedname; |
||
| 176 | $this->name = $cat->knownFederations[$this->tld]; |
||
| 177 | |||
| 178 | parent::__construct(); // we now have access to our database handle |
||
| 179 | |||
| 180 | $this->frontendHandle = DBConnection::handle("FRONTEND"); |
||
| 181 | |||
| 182 | // fetch attributes from DB; populates $this->attributes array |
||
| 183 | $this->attributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row |
||
| 184 | FROM $this->entityOptionTable |
||
| 185 | WHERE $this->entityIdColumn = ? |
||
| 186 | ORDER BY option_name", "FED"); |
||
| 187 | |||
| 188 | |||
| 189 | $this->attributes[] = array("name" => "internal:country", |
||
| 190 | "lang" => NULL, |
||
| 191 | "value" => $this->tld, |
||
| 192 | "level" => "FED", |
||
| 193 | "row" => 0, |
||
| 194 | "flag" => NULL); |
||
| 195 | |||
| 196 | if (CONFIG['FUNCTIONALITY_LOCATIONS']['CONFASSISTANT_RADIUS'] != 'LOCAL' && CONFIG['FUNCTIONALITY_LOCATIONS']['CONFASSISTANT_SILVERBULLET'] == 'LOCAL') { |
||
| 197 | // this instance exclusively does SB, so it is not necessary to ask |
||
| 198 | // fed ops whether they want to enable it or not. So always add it |
||
| 199 | // to the list of fed attributes |
||
| 200 | $this->attributes[] = array("name" => "fed:silverbullet", |
||
| 201 | "lang" => NULL, |
||
| 202 | "value" => "on", |
||
| 203 | "level" => "FED", |
||
| 204 | "row" => 0, |
||
| 205 | "flag" => NULL); |
||
| 206 | } |
||
| 207 | |||
| 208 | $this->idpListActive = []; |
||
| 209 | $this->idpListAll = []; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
|
|
|||
| 213 | * Creates a new IdP inside the federation. |
||
| 214 | * |
||
| 215 | * @param string $ownerId Persistent identifier of the user for whom this IdP is created (first administrator) |
||
| 216 | * @param string $level Privilege level of the first administrator (was he blessed by a federation admin or a peer?) |
||
| 217 | * @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) |
||
| 218 | * @return int identifier of the new IdP |
||
| 219 | */ |
||
| 220 | public function newIdP($ownerId, $level, $mail = NULL, $bestnameguess = NULL) { |
||
| 221 | $this->databaseHandle->exec("INSERT INTO institution (country) VALUES('$this->tld')"); |
||
| 222 | $identifier = $this->databaseHandle->lastID(); |
||
| 223 | |||
| 224 | if ($identifier == 0 || !$this->loggerInstance->writeAudit($ownerId, "NEW", "IdP $identifier")) { |
||
| 225 | $text = "<p>Could not create a new " . CONFIG_CONFASSISTANT['CONSORTIUM']['nomenclature_inst'] . "!</p>"; |
||
| 226 | echo $text; |
||
| 227 | throw new Exception($text); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ($ownerId != "PENDING") { |
||
| 231 | if ($mail === NULL) { |
||
| 232 | throw new Exception("New IdPs in a federation need a mail address UNLESS created by API without OwnerId"); |
||
| 233 | } |
||
| 234 | $this->databaseHandle->exec("INSERT INTO ownership (user_id,institution_id, blesslevel, orig_mail) VALUES(?,?,?,?)", "siss", $ownerId, $identifier, $level, $mail); |
||
| 235 | } |
||
| 236 | if ($bestnameguess == NULL) { |
||
| 237 | $bestnameguess = "(no name yet, identifier $identifier)"; |
||
| 238 | } |
||
| 239 | $admins = $this->listFederationAdmins(); |
||
| 240 | |||
| 241 | // notify the fed admins... |
||
| 242 | |||
| 243 | foreach ($admins as $id) { |
||
| 244 | $user = new User($id); |
||
| 245 | /// arguments are: 1. nomenclature for "institution" |
||
| 246 | // 2. IdP name; |
||
| 247 | /// 3. consortium name (e.g. eduroam); |
||
| 248 | /// 4. federation shortname, e.g. "LU"; |
||
| 249 | /// 5. product name (e.g. eduroam CAT); |
||
| 250 | /// 6. product long name (e.g. eduroam Configuration Assistant Tool) |
||
| 251 | $message = sprintf(_("Hi, |
||
| 252 | |||
| 253 | the invitation for the new %s %s in your %s federation %s has been used and the IdP was created in %s. |
||
| 254 | |||
| 255 | We thought you might want to know. |
||
| 256 | |||
| 257 | Best regards, |
||
| 258 | |||
| 259 | %s"), common\Entity::$nomenclature_inst, $bestnameguess, CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'], strtoupper($this->tld), CONFIG['APPEARANCE']['productname'], CONFIG['APPEARANCE']['productname_long']); |
||
| 260 | $retval = $user->sendMailToUser(sprintf(_("%s in your federation was created"), common\Entity::$nomenclature_inst), $message); |
||
| 261 | if ($retval === FALSE) { |
||
| 262 | $this->loggerInstance->debug(2, "Mail to federation admin was NOT sent!\n"); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return $identifier; |
||
| 267 | } |
||
| 268 | |||
| 269 | private $idpListAll; |
||
| 270 | private $idpListActive; |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Lists all Identity Providers in this federation |
||
| 274 | * |
||
| 275 | * @param int $activeOnly if set to non-zero will list only those institutions which have some valid profiles defined. |
||
| 276 | * @return array (Array of IdP instances) |
||
| 277 | * |
||
| 278 | */ |
||
| 279 | public function listIdentityProviders($activeOnly = 0) { |
||
| 280 | // maybe we did this exercise before? |
||
| 281 | if ($activeOnly != 0 && count($this->idpListActive) > 0) { |
||
| 282 | return $this->idpListActive; |
||
| 283 | } |
||
| 284 | if ($activeOnly == 0 && count($this->idpListAll) > 0) { |
||
| 285 | return $this->idpListAll; |
||
| 286 | } |
||
| 287 | // default query is: |
||
| 288 | $allIDPs = $this->databaseHandle->exec("SELECT inst_id FROM institution |
||
| 289 | WHERE country = '$this->tld' ORDER BY inst_id"); |
||
| 290 | // the one for activeOnly is much more complex: |
||
| 291 | if ($activeOnly) { |
||
| 292 | $allIDPs = $this->databaseHandle->exec("SELECT distinct institution.inst_id AS inst_id |
||
| 293 | FROM institution |
||
| 294 | JOIN profile ON institution.inst_id = profile.inst_id |
||
| 295 | WHERE institution.country = '$this->tld' |
||
| 296 | AND profile.showtime = 1 |
||
| 297 | ORDER BY inst_id"); |
||
| 298 | } |
||
| 299 | |||
| 300 | $returnarray = []; |
||
| 301 | // SELECT -> resource, not boolean |
||
| 302 | while ($idpQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allIDPs)) { |
||
| 303 | $idp = new IdP($idpQuery->inst_id); |
||
| 304 | $name = $idp->name; |
||
| 305 | $idpInfo = ['entityID' => $idp->identifier, |
||
| 306 | 'title' => $name, |
||
| 307 | 'country' => strtoupper($idp->federation), |
||
| 308 | 'instance' => $idp]; |
||
| 309 | $returnarray[$idp->identifier] = $idpInfo; |
||
| 310 | } |
||
| 311 | if ($activeOnly != 0) { // we're only doing this once. |
||
| 312 | $this->idpListActive = $returnarray; |
||
| 313 | } else { |
||
| 314 | $this->idpListAll = $returnarray; |
||
| 315 | } |
||
| 316 | return $returnarray; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * returns an array with information about the authorised administrators of the federation |
||
| 321 | * |
||
| 322 | * @return array list of the admins of this federation |
||
| 323 | */ |
||
| 324 | public function listFederationAdmins() { |
||
| 325 | $returnarray = []; |
||
| 326 | $query = "SELECT user_id FROM user_options WHERE option_name = 'user:fedadmin' AND option_value = ?"; |
||
| 327 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
| 328 | $query = "SELECT eptid as user_id FROM view_admin WHERE role = 'fedadmin' AND realm = ?"; |
||
| 329 | } |
||
| 330 | $userHandle = DBConnection::handle("USER"); // we need something from the USER database for a change |
||
| 331 | $upperFed = strtoupper($this->tld); |
||
| 332 | // SELECT -> resource, not boolean |
||
| 333 | $admins = $userHandle->exec($query, "s", $upperFed); |
||
| 334 | |||
| 335 | while ($fedAdminQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $admins)) { |
||
| 336 | $returnarray[] = $fedAdminQuery->user_id; |
||
| 337 | } |
||
| 338 | return $returnarray; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * cross-checks in the EXTERNAL customer DB which institutions exist there for the federations |
||
| 343 | * |
||
| 344 | * @param bool $unmappedOnly if set to TRUE, only returns those which do not have a known mapping to our internally known institutions |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function listExternalEntities($unmappedOnly) { |
||
| 416 | } |
||
| 417 | |||
| 418 | const UNKNOWN_IDP = -1; |
||
| 419 | const AMBIGUOUS_IDP = -2; |
||
| 420 | |||
| 421 | /** |
||
| 422 | * for a MySQL list of institutions, find an institution or find out that |
||
| 423 | * there is no single best match |
||
| 424 | * |
||
| 425 | * @param \mysqli_result $dbResult the query object to work with |
||
| 426 | * @param string $country used to return the country of the inst, if can be found out |
||
| 427 | * @return int the identifier of the inst, or one of the special return values if unsuccessful |
||
| 428 | */ |
||
| 429 | private static function findCandidates(\mysqli_result $dbResult, &$country) { |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * If we are running diagnostics, our input from the user is the realm. We |
||
| 449 | * need to find out which IdP this realm belongs to. |
||
| 450 | * @param string $realm the realm to search for |
||
| 451 | * @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 |
||
| 452 | */ |
||
| 453 | public static function determineIdPIdByRealm($realm) { |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * helper function to sort institutions by their name |
||
| 478 | * @param array $a an array with institution a's information |
||
| 479 | * @param array $b an array with institution b's information |
||
| 480 | * @return int the comparison result |
||
| 481 | */ |
||
| 482 | private function usortInstitution($a, $b) { |
||
| 484 | } |
||
| 485 | |||
| 486 | } |
||
| 487 |