Total Complexity | 80 |
Total Lines | 506 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 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 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 |
||
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" => 0, |
||
113 | "flag" => NULL]; |
||
114 | |||
115 | $this->name = $this->languageInstance->getLocalisedValue($this->getAttributes('general:instname')); |
||
116 | $eligibility = $this->eligibility(); |
||
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(3, "--- END Constructing new IdP object ... ---\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) |
||
136 | { |
||
137 | $query = "SELECT profile_id FROM profile WHERE inst_id = $this->identifier" . ($activeOnly ? " AND showtime = 1" : ""); |
||
138 | $allProfiles = $this->databaseHandle->exec($query); |
||
139 | $returnarray = []; |
||
140 | // SELECT -> resource, not boolean |
||
141 | while ($profileQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
142 | $oneProfile = ProfileFactory::instantiate($profileQuery->profile_id, $this); |
||
143 | $oneProfile->institution = $this->identifier; |
||
144 | $returnarray[] = $oneProfile; |
||
145 | } |
||
146 | |||
147 | $this->loggerInstance->debug(4, "listProfiles: " . /** @scrutinizer ignore-type */ print_r($returnarray, true)); |
||
148 | return $returnarray; |
||
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 | |||
175 | /** |
||
176 | * looks through all the profiles of the inst and determines the highest prod-ready level among the profiles |
||
177 | * @return int highest level of completeness of all the profiles of the inst |
||
178 | */ |
||
179 | public function maxProfileStatus() |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * looks through all the profiles of the inst and determines the highest |
||
191 | * participation/conformance level for OpenRoaming |
||
192 | * |
||
193 | * @return int highest level of completeness of all the profiles of the inst |
||
194 | */ |
||
195 | public function maxOpenRoamingStatus() |
||
196 | { |
||
197 | $allProfiles = $this->databaseHandle->exec("SELECT MIN(openroaming) AS maxlevel FROM profile WHERE inst_id = $this->identifier"); |
||
198 | // SELECT yields a resource, not a boolean |
||
199 | while ($res = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
200 | return (is_numeric($res->maxlevel) ? (int)$res->maxlevel : AbstractProfile::OVERALL_OPENROAMING_LEVEL_NO ); // insts without profiles should get a "NO" |
||
201 | } |
||
202 | return AbstractProfile::OVERALL_OPENROAMING_LEVEL_NO; |
||
203 | } |
||
204 | |||
205 | |||
206 | /** This function retrieves an array of authorised users which can |
||
207 | * manipulate this institution. |
||
208 | * |
||
209 | * @return array owners of the institution; numbered array with members ID, MAIL and LEVEL |
||
210 | */ |
||
211 | public function listOwners() |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Primary owners are allowed to invite other (secondary) admins to the institution |
||
224 | * |
||
225 | * @param string $user ID of a logged-in user |
||
226 | * @return boolean TRUE if this user is an admin with FED-level blessing |
||
227 | */ |
||
228 | public function isPrimaryOwner($user) |
||
229 | { |
||
230 | foreach ($this->listOwners() as $oneOwner) { |
||
231 | if ($oneOwner['ID'] == $user && $oneOwner['LEVEL'] == "FED") { |
||
232 | return TRUE; |
||
233 | } |
||
234 | } |
||
235 | return FALSE; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * This function gets the profile count for a given IdP. |
||
240 | * |
||
241 | * The count could be retreived from the listProfiles method |
||
242 | * but this is less expensive. |
||
243 | * |
||
244 | * @return int profile count |
||
245 | */ |
||
246 | public function profileCount() |
||
247 | { |
||
248 | $result = $this->databaseHandle->exec("SELECT profile_id FROM profile |
||
249 | WHERE inst_id = $this->identifier"); |
||
250 | // SELECT -> resource, not boolean |
||
251 | $numberOfRows = mysqli_num_rows(/** @scrutinizer ignore-type */ $result); |
||
252 | if (is_string($numberOfRows)) { |
||
253 | throw new Exception("Number of profiles > PHP_MAX_INT?"); |
||
254 | } |
||
255 | return $numberOfRows; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * This function gets the deployment count for a given IdP. |
||
260 | * |
||
261 | * @return int deployment count |
||
262 | */ |
||
263 | public function deploymentCount() |
||
264 | { |
||
265 | $result = $this->databaseHandle->exec("SELECT deployment_id FROM deployment |
||
266 | WHERE inst_id = $this->identifier"); |
||
267 | // SELECT -> resource, not boolean |
||
268 | $numberOfRows = mysqli_num_rows(/** @scrutinizer ignore-type */ $result); |
||
269 | if (is_string($numberOfRows)) { |
||
270 | throw new Exception("Number of deployments > PHP_MAX_INT?"); |
||
271 | } |
||
272 | return $numberOfRows; |
||
273 | } |
||
274 | |||
275 | const ELIGIBILITY_IDP = "IdP"; |
||
276 | const ELIGIBILITY_SP = "SP"; |
||
277 | |||
278 | /** |
||
279 | * checks whether the participant is an IdP, an SP, or both. |
||
280 | * |
||
281 | * @return array list of eligibilities |
||
282 | */ |
||
283 | public function eligibility() |
||
284 | { |
||
285 | $eligibilites = $this->databaseHandle->exec("SELECT type FROM institution WHERE inst_id = $this->identifier"); |
||
286 | while ($iterator = mysqli_fetch_object(/** @scrutinizer ignore-type */ $eligibilites)) { |
||
287 | switch ($iterator->type) { |
||
288 | case "IdP": |
||
289 | return [IdP::ELIGIBILITY_IDP]; |
||
290 | case "SP": |
||
291 | return [IdP::ELIGIBILITY_SP]; |
||
292 | default: |
||
293 | return [IdP::ELIGIBILITY_IDP, IdP::ELIGIBILITY_SP]; |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * This function sets the timestamp of last modification of the child profiles to the current timestamp. |
||
300 | * |
||
301 | * This is needed for installer caching: all installers which are on disk |
||
302 | * must be re-created if an attribute changes. This timestamp here |
||
303 | * is used to determine if the installer on disk is still new enough. |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function updateFreshness() |
||
308 | { |
||
309 | // freshness is always defined for *Profiles* |
||
310 | // IdP needs to update timestamp of all its profiles if an IdP-wide attribute changed |
||
311 | $this->databaseHandle->exec("UPDATE profile SET last_change = CURRENT_TIMESTAMP WHERE inst_id = '$this->identifier'"); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Adds a new profile to this IdP. |
||
316 | * |
||
317 | * Only creates the DB entry for the Profile. If you want to add attributes later, see Profile::addAttribute(). |
||
318 | * |
||
319 | * @param string $type exactly "RADIUS" or "SILVERBULLET", all other values throw an Exception |
||
320 | * @return AbstractProfile|NULL new Profile object if successful, or NULL if an error occured |
||
321 | * @throws Exception |
||
322 | */ |
||
323 | public function newProfile(string $type) |
||
324 | { |
||
325 | $this->databaseHandle->exec("INSERT INTO profile (inst_id) VALUES($this->identifier)"); |
||
326 | $identifier = $this->databaseHandle->lastID(); |
||
327 | if ($identifier > 0) { |
||
328 | switch ($type) { |
||
329 | case AbstractProfile::PROFILETYPE_RADIUS: |
||
330 | return new ProfileRADIUS($identifier, $this); |
||
331 | case AbstractProfile::PROFILETYPE_SILVERBULLET: |
||
332 | $theProfile = new ProfileSilverbullet($identifier, $this); |
||
333 | $theProfile->addSupportedEapMethod(new \core\common\EAP(\core\common\EAP::EAPTYPE_SILVERBULLET), 1); |
||
334 | $theProfile->setRealm($this->identifier . "-" . $theProfile->identifier . "." . strtolower($this->federation) . strtolower(\config\ConfAssistant::SILVERBULLET['realm_suffix'])); |
||
335 | return $theProfile; |
||
336 | default: |
||
337 | throw new Exception("This type of profile is unknown and can not be added."); |
||
338 | } |
||
339 | } |
||
340 | return NULL; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Adds a new hotspot deployment to this IdP. |
||
345 | * |
||
346 | * Only creates the DB entry for the deployment. If you want to add attributes later, see Profile::addAttribute(). |
||
347 | * |
||
348 | * @param string $type exactly "RADIUS-SP" or "MANAGED-SP", all other values throw an Exception |
||
349 | * @param string $consortium name of the consortium to attach this *Managed* SP to |
||
350 | * @return DeploymentManaged the newly created deployment |
||
351 | * @throws Exception |
||
352 | */ |
||
353 | public function newDeployment(string $type, string $consortium = "eduroam") |
||
354 | { |
||
355 | switch ($type) { |
||
356 | case AbstractDeployment::DEPLOYMENTTYPE_CLASSIC: |
||
357 | // classic deployment exist in the eduroam DB. We don't do anything here. |
||
358 | throw new Exception("This type of deployment is handled externally and requesting it here makes no sense."); |
||
359 | case AbstractDeployment::DEPLOYMENTTYPE_MANAGED: |
||
360 | $this->databaseHandle->exec("INSERT INTO deployment (inst_id) VALUES($this->identifier)"); |
||
361 | $identifier = $this->databaseHandle->lastID(); |
||
362 | return new DeploymentManaged($this, $identifier, $consortium); |
||
363 | default: |
||
364 | throw new Exception("This type of deployment is unknown and can not be added."); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * deletes the IdP and all its profiles |
||
370 | * |
||
371 | * @return void |
||
372 | * @throws Exception |
||
373 | */ |
||
374 | public function destroy() |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Performs a lookup in an external database to determine matching entities to this IdP. |
||
413 | * |
||
414 | * The business logic of this function is roaming consortium specific; if no match algorithm is known for the consortium, FALSE is returned. |
||
415 | * |
||
416 | * @param string $type which type of entity are you looking for? |
||
417 | * @return array list of entities in external database that correspond to this IdP |
||
418 | */ |
||
419 | public function getExternalDBSyncCandidates($type) |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * returns the state of sync with the external DB. |
||
457 | * |
||
458 | * @return int |
||
459 | */ |
||
460 | public function getExternalDBSyncState() |
||
461 | { |
||
462 | if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
463 | return $this->externalDbSyncstate; |
||
464 | } |
||
465 | return self::EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Retrieves the external DB identifier of this institution. Returns FALSE if no ID is known. |
||
470 | * |
||
471 | * @return string|boolean the external identifier; or FALSE if no external ID is known |
||
472 | */ |
||
473 | public function getExternalDBId() |
||
474 | { |
||
475 | if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
476 | $idQuery = $this->databaseHandle->exec("SELECT external_db_id FROM institution WHERE inst_id = $this->identifier AND external_db_syncstate = " . self::EXTERNAL_DB_SYNCSTATE_SYNCED); |
||
477 | // SELECT -> it's a resource, not a boolean |
||
478 | if (mysqli_num_rows(/** @scrutinizer ignore-type */ $idQuery) == 0) { |
||
479 | return FALSE; |
||
480 | } |
||
481 | $externalIdQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idQuery); |
||
482 | return $externalIdQuery->external_db_id; |
||
483 | } |
||
484 | return FALSE; |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Associates the external DB id with a CAT id |
||
489 | * |
||
490 | * @param string $identifier the external DB id, which can be alpha-numeric |
||
491 | * @return void |
||
492 | */ |
||
493 | public function setExternalDBId(string $identifier) |
||
494 | { |
||
495 | if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
496 | $syncState = self::EXTERNAL_DB_SYNCSTATE_SYNCED; |
||
497 | $alreadyUsed = $this->databaseHandle->exec("SELECT DISTINCT external_db_id FROM institution WHERE external_db_id = ? AND external_db_syncstate = ?", "si", $identifier, $syncState); |
||
498 | // SELECT -> resource, not boolean |
||
499 | if (mysqli_num_rows(/** @scrutinizer ignore-type */ $alreadyUsed) == 0) { |
||
500 | $this->databaseHandle->exec("UPDATE institution SET external_db_id = ?, external_db_syncstate = ? WHERE inst_id = ?", "sii", $identifier, $syncState, $this->identifier); |
||
501 | } |
||
502 | } |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * removes the link between a CAT institution and the external DB |
||
507 | * |
||
508 | * @return void |
||
509 | */ |
||
510 | public function removeExternalDBId() |
||
511 | { |
||
512 | if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
513 | if ($this->getExternalDBId() !== FALSE) { |
||
514 | $syncState = self::EXTERNAL_DB_SYNCSTATE_NOT_SYNCED; |
||
515 | $this->databaseHandle->exec("UPDATE institution SET external_db_id = NULL, external_db_syncstate = ? WHERE inst_id = ?", "ii", $syncState, $this->identifier); |
||
516 | } |
||
517 | } |
||
518 | } |
||
519 | |||
520 | public const INSTNAME_CHANGED = 1; |
||
521 | |||
522 | /** |
||
523 | * |
||
524 | * @param IdP $old the IdP instance with the old state |
||
525 | * @param IdP $new the IdP instance with the new state |
||
526 | * @return array list of changed things, and details about the change |
||
527 | */ |
||
528 | public static function significantChanges($old, $new) |
||
555 | } |
||
556 | } |