| Conditions | 47 |
| Paths | > 20000 |
| Total Lines | 201 |
| Code Lines | 112 |
| Lines | 79 |
| Ratio | 39.3 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 246 | public function magic() { |
||
| 247 | |||
| 248 | // simple things first: do we know anything about the realm, either |
||
| 249 | // because it's a CAT participant or because it's in the eduroam DB? |
||
| 250 | // if so, we can exclude the INFRA_NONEXISTENTREALM cause |
||
| 251 | |||
| 252 | $this->additionalFindings[AbstractTest::INFRA_NONEXISTENTREALM][] = ["ID1" => $this->catIdP, "ID2" => $this->dbIdP]; |
||
| 253 | |||
| 254 | if ($this->catIdP != \core\Federation::UNKNOWN_IDP || $this->dbIdP != \core\Federation::UNKNOWN_IDP) { |
||
| 255 | unset($this->possibleFailureReasons[AbstractTest::INFRA_NONEXISTENTREALM]); |
||
| 256 | } |
||
| 257 | |||
| 258 | // let's do the least amount of testing needed: |
||
| 259 | // - The CAT reachability test already covers ELTRs, IdP NRO level and the IdP itself. |
||
| 260 | // if the realm maps to a CAT IdP, we can run the more thorough tests; otherwise just |
||
| 261 | // the normal shallow ones |
||
| 262 | |||
| 263 | if ($this->catIdP > 0) { |
||
| 264 | $idpObject = new \core\IdP($this->catIdP); |
||
| 265 | $profileObjects = $idpObject->listProfiles(); |
||
| 266 | |||
| 267 | $bestProfile = FALSE; |
||
| 268 | |||
| 269 | |||
| 270 | foreach ($profileObjects as $profileObject) { |
||
| 271 | $mangledRealm = substr($profileObject->realm, strpos($profileObject->realm, "@") + 1); |
||
| 272 | $readinessLevel = $profileObject->readinessLevel(); |
||
| 273 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SHOWTIME && $mangledRealm == $this->realm) { |
||
| 274 | $bestProfile = $profileObject; |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SUFFICIENTCONFIG && $profileObject->realm == $this->realm) { |
||
| 278 | $bestProfile = $profileObject; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | if ($bestProfile == FALSE) { // huh? no match on the realm. Then let's take the next-best with SUFFICIENTCONFIG |
||
| 282 | foreach ($profileObjects as $profileObject) { |
||
| 283 | $readinessLevel = $profileObject->readinessLevel(); |
||
| 284 | if ($readinessLevel == \core\AbstractProfile::READINESS_LEVEL_SUFFICIENTCONFIG) { |
||
| 285 | $bestProfile = $profileObject; |
||
| 286 | break; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | if ($bestProfile != FALSE) { // still nothing? then there's only a very incomplete profile definition, and we can't work with that. Fall back to shallow |
||
| 291 | $this->additionalFindings[AbstractTest::INFRA_IDP_RADIUS][] = ["Profile" => $bestProfile->identifier]; |
||
| 292 | $this->testsuite = new RADIUSTests($this->realm, $bestProfile->getRealmCheckOuterUsername(), $bestProfile->getEapMethodsinOrderOfPreference(1), $bestProfile->getCollapsedAttributes()['eap:server_name'], $bestProfile->getCollapsedAttributes()["eap:ca_file"]); |
||
| 293 | } else { |
||
| 294 | $this->additionalFindings[AbstractTest::INFRA_IDP_RADIUS][] = ["Profile" => "UNCONCLUSIVE"]; |
||
| 295 | $this->testsuite = new RADIUSTests($this->realm, "anonymous@" . $this->realm); |
||
| 296 | } |
||
| 297 | } else { |
||
| 298 | $this->testsuite = new RADIUSTests($this->realm, "anonymous@" . $this->realm); |
||
| 299 | } |
||
| 300 | |||
| 301 | // these are the normal "realm check" tests covering ETLR, LINK_NRO_IDP, NRO, IDP_RADIUS |
||
| 302 | $this->CATInternalTests(); |
||
| 303 | // - if the test does NOT go through, we need to find out which of the three is guilty |
||
| 304 | // - then, the international "via ETLR" check can be used to find out if the IdP alone |
||
| 305 | // is guilty. If that one fails, the direct monitoring of servers and ETLRs themselves |
||
| 306 | // closes the loop. |
||
| 307 | // let's see if the ETLRs are up |
||
| 308 | View Code Duplication | if (array_key_exists(AbstractTest::INFRA_ETLR, $this->possibleFailureReasons)) { |
|
| 309 | |||
| 310 | $etlrStatus = $this->checkEtlrStatus(); |
||
| 311 | $this->additionalFindings[AbstractTest::INFRA_ETLR][] = $etlrStatus; |
||
| 312 | switch ($etlrStatus["STATUS"]) { |
||
| 313 | case AbstractTest::STATUS_GOOD: |
||
| 314 | unset($this->possibleFailureReasons[AbstractTest::INFRA_ETLR]); |
||
| 315 | break; |
||
| 316 | case AbstractTest::STATUS_PARTIAL: |
||
| 317 | case AbstractTest::STATUS_MONITORINGFAIL: |
||
| 318 | // one of the ETLRs is down, or there is a failure in the monitoring system? |
||
| 319 | // This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 320 | // keep ETLR as a possible problem with original probability |
||
| 321 | break; |
||
| 322 | case AbstractTest::STATUS_DOWN: |
||
| 323 | // Oh! Well if it is not international roaming, that still doesn't have an effect /in this case/. |
||
| 324 | if ($this->idPFederation == $this->visitedFlr) { |
||
| 325 | unset($this->possibleFailureReasons[AbstractTest::INFRA_ETLR]); |
||
| 326 | break; |
||
| 327 | } |
||
| 328 | // But it is about int'l roaming, and we are spot on here. |
||
| 329 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 330 | $this->possibleFailureReasons[AbstractTest::INFRA_ETLR] = 0.95; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | // then let's check the IdP's FLR, if we know the IdP federation at all |
||
| 335 | if ($this->idPFederation != NULL) { |
||
| 336 | if (array_key_exists(AbstractTest::INFRA_NRO_IDP, $this->possibleFailureReasons)) { |
||
| 337 | // first the direct connectivity to the server |
||
| 338 | $flrServerStatus = $this->checkFlrServerStatus($this->idPFederation); |
||
| 339 | $this->additionalFindings[AbstractTest::INFRA_NRO_IDP][] = $flrServerStatus; |
||
| 340 | View Code Duplication | switch ($flrServerStatus["STATUS"]) { |
|
| 341 | case AbstractTest::STATUS_GOOD: |
||
| 342 | unset($this->possibleFailureReasons[AbstractTest::INFRA_NRO_IDP]); |
||
| 343 | break; |
||
| 344 | case AbstractTest::STATUS_PARTIAL: |
||
| 345 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 346 | // keep FLR as a possible problem with original probability |
||
| 347 | break; |
||
| 348 | case AbstractTest::STATUS_DOWN: |
||
| 349 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 350 | $this->possibleFailureReasons[AbstractTest::INFRA_NRO_IDP] = 0.95; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | // now let's theck the link |
||
| 355 | View Code Duplication | if (array_key_exists(AbstractTest::INFRA_LINK_ETLR_NRO_IDP, $this->possibleFailureReasons)) { |
|
| 356 | $flrUplinkStatus = $this->checkFedEtlrUplink($this->idPFederation); |
||
| 357 | $this->additionalFindings[AbstractTest::INFRA_NRO_IDP][] = $flrUplinkStatus; |
||
| 358 | switch ($flrUplinkStatus["STATUS"]) { |
||
| 359 | case AbstractTest::STATUS_GOOD: |
||
| 360 | unset($this->possibleFailureReasons[AbstractTest::INFRA_NRO_IDP]); |
||
| 361 | unset($this->possibleFailureReasons[AbstractTest::INFRA_LINK_ETLR_NRO_IDP]); |
||
| 362 | break; |
||
| 363 | case AbstractTest::STATUS_PARTIAL: |
||
| 364 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 365 | // keep FLR as a possible problem with original probability |
||
| 366 | break; |
||
| 367 | case AbstractTest::STATUS_DOWN: |
||
| 368 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 369 | // if earlier test found the server itself to be the problem, keep it, otherwise put the blame on the link |
||
| 370 | if ($this->possibleFailureReasons[AbstractTest::INFRA_NRO_IDP] != 0.95) { |
||
| 371 | $this->possibleFailureReasons[AbstractTest::INFRA_LINK_ETLR_NRO_IDP] = 0.95; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | // now, if we know the country the user is currently in, let's see |
||
| 377 | // if the NRO SP-side is up |
||
| 378 | if ($this->visitedFlr !== NULL) { |
||
| 379 | $visitedFlrStatus = $this->checkFlrServerStatus($this->visitedFlr); |
||
| 380 | $this->additionalFindings[AbstractTest::INFRA_NRO_SP][] = $visitedFlrStatus; |
||
| 381 | // direct query to server |
||
| 382 | View Code Duplication | switch ($visitedFlrStatus["STATUS"]) { |
|
| 383 | case AbstractTest::STATUS_GOOD: |
||
| 384 | unset($this->possibleFailureReasons[AbstractTest::INFRA_NRO_SP]); |
||
| 385 | break; |
||
| 386 | case AbstractTest::STATUS_PARTIAL: |
||
| 387 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 388 | // keep FLR as a possible problem with original probability |
||
| 389 | break; |
||
| 390 | case AbstractTest::STATUS_DOWN: |
||
| 391 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 392 | $this->possibleFailureReasons[AbstractTest::INFRA_NRO_SP] = 0.95; |
||
| 393 | } |
||
| 394 | // and again its uplink to the ETLR |
||
| 395 | $visitedFlrUplinkStatus = $this->checkFedEtlrUplink($this->visitedFlr); |
||
| 396 | $this->additionalFindings[AbstractTest::INFRA_NRO_SP][] = $visitedFlrUplinkStatus; |
||
| 397 | switch ($visitedFlrUplinkStatus["STATUS"]) { |
||
| 398 | case AbstractTest::STATUS_GOOD: |
||
| 399 | unset($this->possibleFailureReasons[AbstractTest::INFRA_NRO_SP]); |
||
| 400 | unset($this->possibleFailureReasons[AbstractTest::INFRA_LINK_ETLR_NRO_SP]); |
||
| 401 | break; |
||
| 402 | case AbstractTest::STATUS_PARTIAL: |
||
| 403 | // a subset of the FLRs is down? This probably doesn't impact the user unless he's unlucky and has his session fall into failover. |
||
| 404 | // keep FLR as a possible problem with original probability |
||
| 405 | break; |
||
| 406 | case AbstractTest::STATUS_DOWN: |
||
| 407 | // Raise probability by much (even monitoring is sometimes wrong, or a few minutes behind reality) |
||
| 408 | // if earlier test found the server itself to be the problem, keep it, otherwise put the blame on the link |
||
| 409 | if ($this->possibleFailureReasons[AbstractTest::INFRA_NRO_SP] != 0.95) { |
||
| 410 | $this->possibleFailureReasons[AbstractTest::INFRA_LINK_ETLR_NRO_SP] = 0.95; |
||
| 411 | } |
||
| 412 | } |
||
| 413 | } |
||
| 414 | // the last thing we can do (but it's a bit redundant): check the country-to-country link |
||
| 415 | // it's only needed if all three and their links are up, but we want to exclude funny routing blacklists |
||
| 416 | // which occur only in the *combination* of source and dest |
||
| 417 | // if there is an issue at that point, blame the SP: once a request |
||
| 418 | // would have reached the ETLRs, things would be all good (assuming |
||
| 419 | // perfection on the ETLRs here!). So the SP has a wrong config. |
||
| 420 | if ($this->idPFederation !== NULL && |
||
| 421 | $this->visitedFlr !== NULL && |
||
| 422 | !array_key_exists(AbstractTest::INFRA_ETLR, $this->possibleFailureReasons) && |
||
| 423 | !array_key_exists(AbstractTest::INFRA_LINK_ETLR_NRO_IDP, $this->possibleFailureReasons) && |
||
| 424 | !array_key_exists(AbstractTest::INFRA_NRO_IDP, $this->possibleFailureReasons) && |
||
| 425 | !array_key_exists(AbstractTest::INFRA_LINK_ETLR_NRO_SP, $this->possibleFailureReasons) && |
||
| 426 | !array_key_exists(AbstractTest::INFRA_NRO_SP, $this->possibleFailureReasons) |
||
| 427 | ) { |
||
| 428 | $countryToCountryStatus = $this->checkNROFlow(); |
||
| 429 | $this->additionalFindings[AbstractTest::INFRA_NRO_SP][] = $countryToCountryStatus; |
||
| 430 | $this->additionalFindings[AbstractTest::INFRA_ETLR][] = $countryToCountryStatus; |
||
| 431 | $this->additionalFindings[AbstractTest::INFRA_NRO_IDP][] = $countryToCountryStatus; |
||
| 432 | View Code Duplication | switch ($countryToCountryStatus["STATUS"]) { |
|
| 433 | case AbstractTest::STATUS_GOOD: |
||
| 434 | // all routes work |
||
| 435 | break; |
||
| 436 | case AbstractTest::STATUS_PARTIAL: |
||
| 437 | // at least one, or even all have a routing problem |
||
| 438 | case AbstractTest::STATUS_DOWN: |
||
| 439 | // that's rather telling. |
||
| 440 | $this->possibleFailureReasons[AbstractTest::INFRA_NRO_SP] = 0.95; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | $this->normaliseResultSet(); |
||
| 445 | |||
| 446 | return ["SUSPECTS" => $this->possibleFailureReasons, "EVIDENCE" => $this->additionalFindings]; |
||
| 447 | } |
||
| 450 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.