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. 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 IdP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class IdP extends EntityWithDBProperties { |
||
| 39 | |||
| 40 | const EXTERNAL_DB_SYNCSTATE_NOT_SYNCED = 0; |
||
| 41 | const EXTERNAL_DB_SYNCSTATE_SYNCED = 1; |
||
| 42 | const EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING = 2; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * |
||
| 46 | * @var int synchronisation state with external database, if any |
||
| 47 | */ |
||
| 48 | private $externalDbSyncstate; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The shortname of this IdP's federation |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $federation; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructs an IdP object based on its details in the database. |
||
| 58 | * Cannot be used to define a new IdP in the database! This happens via Federation::newIdP() |
||
| 59 | * |
||
| 60 | * @param int $instId the database row identifier |
||
| 61 | */ |
||
| 62 | public function __construct(int $instId) { |
||
| 63 | $this->databaseType = "INST"; |
||
| 64 | parent::__construct(); // now databaseHandle and logging is available |
||
| 65 | $this->entityOptionTable = "institution_option"; |
||
| 66 | $this->entityIdColumn = "institution_id"; |
||
| 67 | if (!is_numeric($instId)) { |
||
| 68 | throw new Exception("An " . CONFIG_CONFASSISTANT['CONSORTIUM']['nomenclature_inst'] . " is identified by an integer index!"); |
||
| 69 | } |
||
| 70 | $this->identifier = (int) $instId; |
||
| 71 | |||
| 72 | $idp = $this->databaseHandle->exec("SELECT inst_id, country,external_db_syncstate FROM institution WHERE inst_id = $this->identifier"); |
||
| 73 | // SELECT -> returns resource, not boolean |
||
| 74 | if (!$instQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idp)) { |
||
| 75 | throw new Exception("IdP $this->identifier not found in database!"); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->federation = $instQuery->country; |
||
| 79 | $this->externalDbSyncstate = $instQuery->external_db_syncstate; |
||
| 80 | |||
| 81 | // fetch attributes from DB; populates $this->attributes array |
||
| 82 | $this->attributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row |
||
| 83 | FROM $this->entityOptionTable |
||
| 84 | WHERE $this->entityIdColumn = ? |
||
| 85 | ORDER BY option_name", "IdP", "i", $this->identifier); |
||
| 86 | |||
| 87 | $this->attributes[] = ["name" => "internal:country", |
||
| 88 | "lang" => NULL, |
||
| 89 | "value" => $this->federation, |
||
| 90 | "level" => "IdP", |
||
| 91 | "row" => 0, |
||
| 92 | "flag" => NULL]; |
||
| 93 | |||
| 94 | $this->name = $this->languageInstance->getLocalisedValue($this->getAttributes('general:instname')); |
||
| 95 | $this->loggerInstance->debug(3, "--- END Constructing new IdP object ... ---\n"); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * This function retrieves all registered profiles for this IdP from the database |
||
| 100 | * |
||
| 101 | * @param bool $activeOnly if and set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
| 102 | * @return array list of Profiles of this IdP |
||
| 103 | */ |
||
| 104 | public function listProfiles(bool $activeOnly = FALSE) { |
||
| 105 | $query = "SELECT profile_id FROM profile WHERE inst_id = $this->identifier" . ($activeOnly ? " AND showtime = 1" : ""); |
||
| 106 | $allProfiles = $this->databaseHandle->exec($query); |
||
| 107 | $returnarray = []; |
||
| 108 | // SELECT -> resource, not boolean |
||
| 109 | while ($profileQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
| 110 | $oneProfile = ProfileFactory::instantiate($profileQuery->profile_id, $this); |
||
| 111 | $oneProfile->institution = $this->identifier; |
||
| 112 | $returnarray[] = $oneProfile; |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->loggerInstance->debug(2, "listProfiles: " . print_r($returnarray, true)); |
||
| 116 | return $returnarray; |
||
| 117 | } |
||
| 118 | |||
| 119 | const PROFILES_INCOMPLETE = 0; |
||
| 120 | const PROFILES_CONFIGURED = 1; |
||
| 121 | const PROFILES_SHOWTIME = 2; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * looks through all the profiles of the inst and determines the highest prod-ready level among the profiles |
||
| 125 | * @return int highest level of completeness of all the profiles of the inst |
||
| 126 | */ |
||
| 127 | public function maxProfileStatus() { |
||
| 128 | $allProfiles = $this->databaseHandle->exec("SELECT sufficient_config + showtime AS maxlevel FROM profile WHERE inst_id = $this->identifier ORDER BY maxlevel DESC LIMIT 1"); |
||
| 129 | // SELECT yields a resource, not a boolean |
||
| 130 | while ($res = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
| 131 | return $res->maxlevel; |
||
| 132 | } |
||
| 133 | return self::PROFILES_INCOMPLETE; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** This function retrieves an array of authorised users which can |
||
| 137 | * manipulate this institution. |
||
| 138 | * |
||
| 139 | * @return array owners of the institution; numbered array with members ID, MAIL and LEVEL |
||
| 140 | */ |
||
| 141 | public function listOwners() { |
||
| 142 | $returnarray = []; |
||
| 143 | $admins = $this->databaseHandle->exec("SELECT user_id, orig_mail, blesslevel FROM ownership WHERE institution_id = $this->identifier ORDER BY user_id"); |
||
| 144 | // SELECT -> resource, not boolean |
||
| 145 | while ($ownerQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $admins)) { |
||
| 146 | $returnarray[] = ['ID' => $ownerQuery->user_id, 'MAIL' => $ownerQuery->orig_mail, 'LEVEL' => $ownerQuery->blesslevel]; |
||
| 147 | } |
||
| 148 | return $returnarray; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Primary owners are allowed to invite other (secondary) admins to the institution |
||
| 153 | * |
||
| 154 | * @param string $user ID of a logged-in user |
||
| 155 | * @return bool TRUE if this user is an admin with FED-level blessing |
||
| 156 | */ |
||
| 157 | public function isPrimaryOwner($user) { |
||
| 158 | foreach ($this->listOwners() as $oneOwner) { |
||
| 159 | if ($oneOwner['ID'] == $user && $oneOwner['LEVEL'] == "FED") { |
||
| 160 | return TRUE; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | return FALSE; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * This function gets the profile count for a given IdP. |
||
| 168 | * |
||
| 169 | * The count could be retreived from the listProfiles method |
||
| 170 | * but this is less expensive. |
||
| 171 | * |
||
| 172 | * @return int profile count |
||
| 173 | */ |
||
| 174 | public function profileCount() { |
||
| 175 | $result = $this->databaseHandle->exec("SELECT profile_id FROM profile |
||
| 176 | WHERE inst_id = $this->identifier"); |
||
| 177 | // SELECT -> resource, not boolean |
||
| 178 | return(mysqli_num_rows(/** @scrutinizer ignore-type */ $result)); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * This function sets the timestamp of last modification of the child profiles to the current timestamp. |
||
| 183 | * |
||
| 184 | * This is needed for installer caching: all installers which are on disk |
||
| 185 | * must be re-created if an attribute changes. This timestamp here |
||
| 186 | * is used to determine if the installer on disk is still new enough. |
||
| 187 | */ |
||
| 188 | public function updateFreshness() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Adds a new profile to this IdP. |
||
| 196 | * |
||
| 197 | * Only creates the DB entry for the Profile. If you want to add attributes later, see Profile::addAttribute(). |
||
| 198 | * |
||
| 199 | * @param string $type exactly "RADIUS" or "SILVERBULLET", all other values throw an Exception |
||
| 200 | * @return object new Profile object if successful, or FALSE if an error occured |
||
| 201 | */ |
||
| 202 | public function newProfile(string $type) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * deletes the IdP and all its profiles |
||
| 224 | */ |
||
| 225 | public function destroy() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Performs a lookup in an external database to determine matching entities to this IdP. |
||
| 261 | * |
||
| 262 | * The business logic of this function is roaming consortium specific; if no match algorithm is known for the consortium, FALSE is returned. |
||
| 263 | * |
||
| 264 | * @return mixed list of entities in external database that correspond to this IdP or FALSE if no consortium-specific matching function is defined |
||
| 265 | */ |
||
| 266 | public function getExternalDBSyncCandidates() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * returns the state of sync with the external DB. |
||
| 309 | * |
||
| 310 | * @return int |
||
| 311 | */ |
||
| 312 | public function getExternalDBSyncState() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Retrieves the external DB identifier of this institution. Returns FALSE if no ID is known. |
||
| 321 | * |
||
| 322 | * @return string|FALSE the external identifier; or FALSE if no external ID is known |
||
| 323 | */ |
||
| 324 | public function getExternalDBId() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Associates the external DB id with a CAT id |
||
| 339 | * |
||
| 340 | * @param string $identifier the external DB id, which can be alpha-numeric |
||
| 341 | */ |
||
| 342 | public function setExternalDBId(string $identifier) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * removes the link between a CAT institution and the external DB |
||
| 355 | */ |
||
| 356 | public function removeExternalDBId() { |
||
| 364 | |||
| 365 | } |
||
| 366 |