| Total Complexity | 83 | 
| Total Lines | 525 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 1 | Features | 0 | 
Complex classes like IdP 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 IdP, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 49 | class IdP extends EntityWithDBProperties | ||
| 50 | { | ||
| 51 | |||
| 52 | const EXTERNAL_DB_SYNCSTATE_NOT_SYNCED = 0; | ||
| 53 | const EXTERNAL_DB_SYNCSTATE_SYNCED = 1; | ||
| 54 | const EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING = 2; | ||
| 55 | const TYPE_IDP = 'IdP'; | ||
| 56 | const TYPE_SP = 'SP'; | ||
| 57 | const TYPE_IDPSP = 'IdPSP'; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * | ||
| 61 | * @var integer synchronisation state with external database, if any | ||
| 62 | */ | ||
| 63 | private $externalDbSyncstate; | ||
| 64 | |||
| 65 | /** | ||
| 66 | * The shortname of this IdP's federation | ||
| 67 | * @var string | ||
| 68 | */ | ||
| 69 | public $federation; | ||
| 70 | |||
| 71 | /** | ||
| 72 | * The type of participant in DB enum notation | ||
| 73 | * @var string | ||
| 74 | */ | ||
| 75 | public $type; | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Constructs an IdP object based on its details in the database. | ||
| 79 | * Cannot be used to define a new IdP in the database! This happens via Federation::newIdP() | ||
| 80 | * | ||
| 81 | * @param int $instId the database row_id identifier | ||
| 82 | * @throws Exception | ||
| 83 | */ | ||
| 84 | public function __construct(int $instId) | ||
| 85 |     { | ||
| 86 | $this->databaseType = "INST"; | ||
| 87 | parent::__construct(); // now databaseHandle and logging is available | ||
| 88 | $this->entityOptionTable = "institution_option"; | ||
| 89 | $this->entityIdColumn = "institution_id"; | ||
| 90 | |||
| 91 | $this->identifier = $instId; | ||
| 92 | |||
| 93 |         $idp = $this->databaseHandle->exec("SELECT inst_id, country,external_db_syncstate, type FROM institution WHERE inst_id = $this->identifier"); | ||
| 94 | // SELECT -> returns resource, not boolean | ||
| 95 |         if (!$instQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idp)) { | ||
| 96 |             throw new Exception("IdP $this->identifier not found in database!"); | ||
| 97 | } | ||
| 98 | |||
| 99 | $this->federation = $instQuery->country; | ||
| 100 | $this->externalDbSyncstate = $instQuery->external_db_syncstate; | ||
| 101 | |||
| 102 | // fetch attributes from DB; populates $this->attributes array | ||
| 103 |         $this->attributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row_id  | ||
| 104 | FROM $this->entityOptionTable | ||
| 105 | WHERE $this->entityIdColumn = ? | ||
| 106 | ORDER BY option_name", "IdP"); | ||
| 107 | |||
| 108 | $this->attributes[] = ["name" => "internal:country", | ||
| 109 | "lang" => NULL, | ||
| 110 | "value" => $this->federation, | ||
| 111 | "level" => Options::LEVEL_IDP, | ||
| 112 | "row_id" => 0, | ||
| 113 | "flag" => NULL]; | ||
| 114 | |||
| 115 |         $this->name = $this->languageInstance->getLocalisedValue($this->getAttributes('general:instname')); | ||
| 116 | $eligibility = $this->eligibility($instQuery->type); | ||
| 117 |         if (in_array(IdP::ELIGIBILITY_IDP, $eligibility) && in_array(IdP::ELIGIBILITY_SP, $eligibility)) { | ||
| 118 | $eligType = IdP::TYPE_IDPSP . ""; | ||
| 119 | $this->type = $eligType; | ||
| 120 |         } elseif (in_array(IdP::ELIGIBILITY_IDP, $eligibility)) { | ||
| 121 | $eligType = IdP::TYPE_IDP . ""; | ||
| 122 |         } else { | ||
| 123 | $eligType = IdP::TYPE_SP . ""; | ||
| 124 | } | ||
| 125 | $this->type = $eligType; | ||
| 126 | $this->loggerInstance->debug(4, "--- END Constructing new IdP object $instId ... ---\n"); | ||
| 127 | } | ||
| 128 | |||
| 129 | /** | ||
| 130 | * This function retrieves all registered profiles for this IdP from the database | ||
| 131 | * | ||
| 132 | * @param bool $activeOnly if and set to non-zero will cause listing of only those institutions which have some valid profiles defined. | ||
| 133 | * @return \core\AbstractProfile[] list of Profiles of this IdP | ||
| 134 | */ | ||
| 135 | public function listProfiles(bool $activeOnly = FALSE) | ||
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * This function retrieves all SP deployments for this organisation from the database | ||
| 153 | * | ||
| 154 | * @param bool $activeOnly if and set to non-zero will cause listing of only those institutions which have some valid profiles defined. | ||
| 155 | * @return \core\AbstractDeployment[] list of deployments of this IdP | ||
| 156 | */ | ||
| 157 | public function listDeployments(bool $activeOnly = FALSE) | ||
| 169 | } | ||
| 170 | |||
| 171 | const PROFILES_INCOMPLETE = 0; | ||
| 172 | const PROFILES_CONFIGURED = 1; | ||
| 173 | const PROFILES_SHOWTIME = 2; | ||
| 174 | const PROFILES_REDIRECTED = 3; | ||
| 175 | |||
| 176 | const PROFILES_INDEX = [ | ||
| 177 | self::PROFILES_INCOMPLETE => 'PROFILES_INCOMPLETE', | ||
| 178 | self::PROFILES_CONFIGURED => 'PROFILES_CONFIGURED', | ||
| 179 | self::PROFILES_SHOWTIME => 'PROFILES_SHOWTIME', | ||
| 180 | self::PROFILES_REDIRECTED => 'PROFILES_REDIRECTED', | ||
| 181 | ]; | ||
| 182 | |||
| 183 | /** | ||
| 184 | * looks through all the profiles of the inst and determines the highest prod-ready level among the profiles | ||
| 185 | * @return int highest level of completeness of all the profiles of the inst or PROFILES_REDIRECTED if all profiles are redirected | ||
| 186 | */ | ||
| 187 | |||
| 188 | public function maxProfileStatus() | ||
| 189 |     { | ||
| 190 | $redirectProfileIds = []; | ||
| 191 |         $allProfileLevels = $this->databaseHandle->exec("SELECT profile_id, sufficient_config + showtime AS maxlevel FROM profile WHERE inst_id = $this->identifier ORDER BY maxlevel DESC"); | ||
| 192 | // SELECT yields a resource, not a boolean | ||
| 193 |         if ($allProfileLevels->num_rows == 0 ) { | ||
| 194 | return self::PROFILES_INCOMPLETE; | ||
| 195 | } | ||
| 196 | $allProfilesArray = $allProfileLevels->fetch_all(MYSQLI_ASSOC); | ||
| 197 | $max_level = $allProfilesArray[0]['maxlevel']; | ||
| 198 |         $redirectProfiles = $this->databaseHandle->exec("SELECT profile.profile_id as profile_id FROM profile JOIN profile_option ON profile.profile_id=profile_option.profile_id WHERE inst_id = $this->identifier AND profile.showtime=1 AND option_name='device-specific:redirect' AND device_id IS NULL"); | ||
| 199 |         while ($res = $redirectProfiles->fetch_object()) { | ||
| 200 | $redirectProfileIds[] = $res->profile_id; | ||
| 201 | } | ||
| 202 |         foreach ($allProfilesArray as $profile) { | ||
| 203 |             if (!in_array($profile['profile_id'], $redirectProfileIds)) { | ||
| 204 | return($max_level); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | return self::PROFILES_REDIRECTED; | ||
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 211 | * looks through all the profiles of the inst and determines the highest | ||
| 212 | * participation/conformance level for OpenRoaming | ||
| 213 | * | ||
| 214 | * @return int highest level of completeness of all the profiles of the inst | ||
| 215 | */ | ||
| 216 | public function maxOpenRoamingStatus() | ||
| 217 |     { | ||
| 218 |         $allProfiles = $this->databaseHandle->exec("SELECT MIN(openroaming) AS maxlevel FROM profile WHERE inst_id = $this->identifier"); | ||
| 219 | // SELECT yields a resource, not a boolean | ||
| 220 |         while ($res = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { | ||
| 221 | return (is_numeric($res->maxlevel) ? (int)$res->maxlevel : AbstractProfile::OVERALL_OPENROAMING_LEVEL_NO ); // insts without profiles should get a "NO" | ||
| 222 | } | ||
| 223 | return AbstractProfile::OVERALL_OPENROAMING_LEVEL_NO; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | /** This function retrieves an array of authorised users which can | ||
| 228 | * manipulate this institution. | ||
| 229 | * | ||
| 230 | * @return array owners of the institution; numbered array with members ID, MAIL and LEVEL | ||
| 231 | */ | ||
| 232 | public function listOwners() | ||
| 241 | } | ||
| 242 | |||
| 243 | /** | ||
| 244 | * Primary owners are allowed to invite other (secondary) admins to the institution | ||
| 245 | * | ||
| 246 | * @param string $user ID of a logged-in user | ||
| 247 | * @return boolean TRUE if this user is an admin with FED-level blessing | ||
| 248 | */ | ||
| 249 | public function isPrimaryOwner($user) | ||
| 257 | } | ||
| 258 | |||
| 259 | /** | ||
| 260 | * This function gets the profile count for a given IdP. | ||
| 261 | * | ||
| 262 | * The count could be retrieved from the listProfiles method | ||
| 263 | * but this is less expensive. | ||
| 264 | * | ||
| 265 | * @return int profile count | ||
| 266 | */ | ||
| 267 | public function profileCount() | ||
| 277 | } | ||
| 278 | |||
| 279 | /** | ||
| 280 | * This function gets the deployment count for a given IdP. | ||
| 281 | * | ||
| 282 | * @return int deployment count | ||
| 283 | */ | ||
| 284 | public function deploymentCount() | ||
| 294 | } | ||
| 295 | |||
| 296 | const ELIGIBILITY_IDP = "IdP"; | ||
| 297 | const ELIGIBILITY_SP = "SP"; | ||
| 298 | |||
| 299 | /** | ||
| 300 | * checks whether the participant is an IdP, an SP, or both. | ||
| 301 | * | ||
| 302 | * @return array list of eligibilities | ||
| 303 | */ | ||
| 304 | public function eligibility($type) | ||
| 305 |     { | ||
| 306 |         switch ($type) { | ||
| 307 | case "IdP": | ||
| 308 | return [IdP::ELIGIBILITY_IDP]; | ||
| 309 | case "SP": | ||
| 310 | return [IdP::ELIGIBILITY_SP]; | ||
| 311 | default: | ||
| 312 | return [IdP::ELIGIBILITY_IDP, IdP::ELIGIBILITY_SP]; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | /** | ||
| 317 | * This function sets the timestamp of last modification of the child profiles to the current timestamp. | ||
| 318 | * | ||
| 319 | * This is needed for installer caching: all installers which are on disk | ||
| 320 | * must be re-created if an attribute changes. This timestamp here | ||
| 321 | * is used to determine if the installer on disk is still new enough. | ||
| 322 | * | ||
| 323 | * @return void | ||
| 324 | */ | ||
| 325 | public function updateFreshness() | ||
| 326 |     { | ||
| 327 | // freshness is always defined for *Profiles* | ||
| 328 | // IdP needs to update timestamp of all its profiles if an IdP-wide attribute changed | ||
| 329 |         $this->databaseHandle->exec("UPDATE profile SET last_change = CURRENT_TIMESTAMP WHERE inst_id = '$this->identifier'"); | ||
| 330 | } | ||
| 331 | |||
| 332 | /** | ||
| 333 | * Adds a new profile to this IdP. | ||
| 334 | * | ||
| 335 | * Only creates the DB entry for the Profile. If you want to add attributes later, see Profile::addAttribute(). | ||
| 336 | * | ||
| 337 | * @param string $type exactly "RADIUS" or "SILVERBULLET", all other values throw an Exception | ||
| 338 | * @return AbstractProfile|NULL new Profile object if successful, or NULL if an error occurred | ||
| 339 | * @throws Exception | ||
| 340 | */ | ||
| 341 | public function newProfile(string $type) | ||
| 342 |     { | ||
| 343 |         $this->databaseHandle->exec("INSERT INTO profile (inst_id) VALUES($this->identifier)"); | ||
| 344 | $identifier = $this->databaseHandle->lastID(); | ||
| 345 |         if ($identifier > 0) { | ||
| 346 |             switch ($type) { | ||
| 347 | case AbstractProfile::PROFILETYPE_RADIUS: | ||
| 348 | return new ProfileRADIUS($identifier, $this); | ||
| 349 | case AbstractProfile::PROFILETYPE_SILVERBULLET: | ||
| 350 | $theProfile = new ProfileSilverbullet($identifier, $this); | ||
| 351 | $theProfile->addSupportedEapMethod(new \core\common\EAP(\core\common\EAP::EAPTYPE_SILVERBULLET), 1); | ||
| 352 | $theProfile->setRealm($this->identifier . "-" . $theProfile->identifier . "." . strtolower($this->federation) . strtolower(\config\ConfAssistant::SILVERBULLET['realm_suffix'])); | ||
| 353 | return $theProfile; | ||
| 354 | default: | ||
| 355 |                     throw new Exception("This type of profile is unknown and can not be added."); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | return NULL; | ||
| 359 | } | ||
| 360 | |||
| 361 | /** | ||
| 362 | * Adds a new hotspot deployment to this IdP. | ||
| 363 | * | ||
| 364 | * Only creates the DB entry for the deployment. If you want to add attributes later, see Profile::addAttribute(). | ||
| 365 | * | ||
| 366 | * @param string $type exactly "RADIUS-SP" or "MANAGED-SP", all other values throw an Exception | ||
| 367 | * @param string $consortium name of the consortium to attach this *Managed* SP to | ||
| 368 | * @return DeploymentManaged the newly created deployment | ||
| 369 | * @throws Exception | ||
| 370 | */ | ||
| 371 | public function newDeployment(string $type, string $consortium = "eduroam") | ||
| 372 |     { | ||
| 373 |         switch ($type) { | ||
| 374 | case AbstractDeployment::DEPLOYMENTTYPE_CLASSIC: | ||
| 375 | // classic deployment exist in the eduroam DB. We don't do anything here. | ||
| 376 |                 throw new Exception("This type of deployment is handled externally and requesting it here makes no sense."); | ||
| 377 | case AbstractDeployment::DEPLOYMENTTYPE_MANAGED: | ||
| 378 |                 $this->databaseHandle->exec("INSERT INTO deployment (inst_id) VALUES($this->identifier)"); | ||
| 379 | $identifier = $this->databaseHandle->lastID(); | ||
| 380 | return new DeploymentManaged($this, $identifier, $consortium); | ||
| 381 | default: | ||
| 382 |                 throw new Exception("This type of deployment is unknown and can not be added."); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | /** | ||
| 387 | * deletes the IdP and all its profiles | ||
| 388 | * | ||
| 389 | * @return void | ||
| 390 | * @throws Exception | ||
| 391 | */ | ||
| 392 | public function destroy() | ||
| 428 | } | ||
| 429 | |||
| 430 | /** | ||
| 431 | * Performs a lookup in an external database to determine matching entities to this IdP. | ||
| 432 | * | ||
| 433 | * The business logic of this function is roaming consortium specific; if no match algorithm is known for the consortium, FALSE is returned. | ||
| 434 | * | ||
| 435 | * @param string $type which type of entity are you looking for? | ||
| 436 | * @return array list of entities in external database that correspond to this IdP | ||
| 437 | */ | ||
| 438 | public function getExternalDBSyncCandidates($type) | ||
| 472 | } | ||
| 473 | |||
| 474 | /** | ||
| 475 | * returns the state of sync with the external DB. | ||
| 476 | * | ||
| 477 | * @return int | ||
| 478 | */ | ||
| 479 | public function getExternalDBSyncState() | ||
| 480 |     { | ||
| 481 |         if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED | ||
| 482 | return $this->externalDbSyncstate; | ||
| 483 | } | ||
| 484 | return self::EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING; | ||
| 485 | } | ||
| 486 | |||
| 487 | /** | ||
| 488 | * Retrieves the external DB identifier of this institution. Returns FALSE if no ID is known. | ||
| 489 | * | ||
| 490 | * @return string|boolean the external identifier; or FALSE if no external ID is known | ||
| 491 | */ | ||
| 492 | public function getExternalDBId() | ||
| 493 |     { | ||
| 494 |         if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED | ||
| 495 |             $idQuery = $this->databaseHandle->exec("SELECT external_db_id FROM institution WHERE inst_id = $this->identifier AND external_db_syncstate = " . self::EXTERNAL_DB_SYNCSTATE_SYNCED); | ||
| 496 | // SELECT -> it's a resource, not a boolean | ||
| 497 |             if (mysqli_num_rows(/** @scrutinizer ignore-type */ $idQuery) == 0) { | ||
| 498 | return FALSE; | ||
| 499 | } | ||
| 500 | $externalIdQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idQuery); | ||
| 501 | return $externalIdQuery->external_db_id; | ||
| 502 | } | ||
| 503 | return FALSE; | ||
| 504 | } | ||
| 505 | |||
| 506 | /** | ||
| 507 | * Associates the external DB id with a CAT id | ||
| 508 | * | ||
| 509 | * @param string $identifier the external DB id, which can be alphanumeric | ||
| 510 | * @return void | ||
| 511 | */ | ||
| 512 | public function setExternalDBId(string $identifier) | ||
| 513 |     { | ||
| 514 |         if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED | ||
| 515 | $syncState = self::EXTERNAL_DB_SYNCSTATE_SYNCED; | ||
| 516 |             $alreadyUsed = $this->databaseHandle->exec("SELECT DISTINCT external_db_id FROM institution WHERE external_db_id = ? AND external_db_syncstate = ?", "si", $identifier, $syncState); | ||
| 517 | // SELECT -> resource, not boolean | ||
| 518 |             if (mysqli_num_rows(/** @scrutinizer ignore-type */ $alreadyUsed) == 0) { | ||
| 519 |                 $this->databaseHandle->exec("UPDATE institution SET external_db_id = ?, external_db_syncstate = ? WHERE inst_id = ?", "sii", $identifier, $syncState, $this->identifier); | ||
| 520 | } | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | /** | ||
| 525 | * removes the link between a CAT institution and the external DB | ||
| 526 | * | ||
| 527 | * @return void | ||
| 528 | */ | ||
| 529 | public function removeExternalDBId() | ||
| 530 |     { | ||
| 531 |         if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED | ||
| 532 |             if ($this->getExternalDBId() !== FALSE) { | ||
| 533 | $syncState = self::EXTERNAL_DB_SYNCSTATE_NOT_SYNCED; | ||
| 534 |                 $this->databaseHandle->exec("UPDATE institution SET external_db_id = NULL, external_db_syncstate = ? WHERE inst_id = ?", "ii", $syncState, $this->identifier); | ||
| 535 | } | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | public const INSTNAME_CHANGED = 1; | ||
| 540 | |||
| 541 | /** | ||
| 542 | * | ||
| 543 | * @param IdP $old the IdP instance with the old state | ||
| 544 | * @param IdP $new the IdP instance with the new state | ||
| 545 | * @return array list of changed things, and details about the change | ||
| 546 | */ | ||
| 547 | public static function significantChanges($old, $new) | ||
| 574 | } | ||
| 575 | } | ||
| 576 | 
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths