| Total Complexity | 105 |
| Total Lines | 610 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MetaLoader 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 MetaLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class MetaLoader |
||
| 15 | { |
||
| 16 | /** @var int|null */ |
||
| 17 | private $expire; |
||
| 18 | |||
| 19 | /** @var array */ |
||
| 20 | private $metadata = []; |
||
| 21 | |||
| 22 | /** @var object|null */ |
||
| 23 | private $oldMetadataSrc; |
||
| 24 | |||
| 25 | /** @var string|null */ |
||
| 26 | private $stateFile = null; |
||
| 27 | |||
| 28 | /** @var bool*/ |
||
| 29 | private $changed = false; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $state = []; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | private $types = [ |
||
| 36 | 'saml20-idp-remote', |
||
| 37 | 'saml20-sp-remote', |
||
| 38 | 'attributeauthority-remote' |
||
| 39 | ]; |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * |
||
| 45 | * @param int|null $expire |
||
| 46 | * @param string|null $stateFile |
||
| 47 | * @param object|null $oldMetadataSrc |
||
| 48 | */ |
||
| 49 | public function __construct(int $expire = null, string $stateFile = null, object $oldMetadataSrc = null) |
||
| 50 | { |
||
| 51 | $this->expire = $expire; |
||
| 52 | $this->oldMetadataSrc = $oldMetadataSrc; |
||
| 53 | $this->stateFile = $stateFile; |
||
| 54 | |||
| 55 | // Read file containing $state from disk |
||
| 56 | /** @psalm-var array|null */ |
||
| 57 | $state = null; |
||
| 58 | if (!is_null($stateFile) && is_readable($stateFile)) { |
||
| 59 | include($stateFile); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!empty($state)) { |
||
|
|
|||
| 63 | $this->state = $state; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the types of entities that will be loaded. |
||
| 70 | * |
||
| 71 | * @return array The entity types allowed. |
||
| 72 | */ |
||
| 73 | public function getTypes(): array |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Set the types of entities that will be loaded. |
||
| 81 | * |
||
| 82 | * @param string|array $types Either a string with the name of one single type allowed, or an array with a list of |
||
| 83 | * types. Pass an empty array to reset to all types of entities. |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function setTypes($types): void |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * This function processes a SAML metadata file. |
||
| 97 | * |
||
| 98 | * @param $source array |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function loadSource(array $source): void |
||
| 225 | } |
||
| 226 | |||
| 227 | |||
| 228 | /* |
||
| 229 | * Recursively checks whether array $dst contains array $src. If $src |
||
| 230 | * is not an array, a literal comparison is being performed. |
||
| 231 | */ |
||
| 232 | private function containsArray($src, $dst): bool |
||
| 233 | { |
||
| 234 | if (is_array($src)) { |
||
| 235 | if (!is_array($dst)) { |
||
| 236 | return false; |
||
| 237 | } |
||
| 238 | $dstKeys = array_keys($dst); |
||
| 239 | |||
| 240 | /* Loop over all src keys */ |
||
| 241 | foreach ($src as $srcKey => $srcval) { |
||
| 242 | if (is_int($srcKey)) { |
||
| 243 | /* key is number, check that the key appears as one |
||
| 244 | * of the destination keys: if not, then src has |
||
| 245 | * more keys than dst */ |
||
| 246 | if (!array_key_exists($srcKey, $dst)) { |
||
| 247 | return false; |
||
| 248 | } |
||
| 249 | |||
| 250 | /* loop over dest keys, to find value: we don't know |
||
| 251 | * whether they are in the same order */ |
||
| 252 | $submatch = false; |
||
| 253 | foreach ($dstKeys as $dstKey) { |
||
| 254 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||
| 255 | $submatch = true; |
||
| 256 | break; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | if (!$submatch) { |
||
| 260 | return false; |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | /* key is regexp: find matching keys */ |
||
| 264 | /** @var array|false $matchingDstKeys */ |
||
| 265 | $matchingDstKeys = preg_grep($srcKey, $dstKeys); |
||
| 266 | if (!is_array($matchingDstKeys)) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | |||
| 270 | $match = false; |
||
| 271 | foreach ($matchingDstKeys as $dstKey) { |
||
| 272 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||
| 273 | /* Found a match */ |
||
| 274 | $match = true; |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | if (!$match) { |
||
| 279 | /* none of the keys has a matching value */ |
||
| 280 | return false; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | /* each src key/value matches */ |
||
| 285 | return true; |
||
| 286 | } else { |
||
| 287 | /* src is not an array, do a regexp match against dst */ |
||
| 288 | return (preg_match($src, $dst) === 1); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Create HTTP context, with any available caches taken into account |
||
| 294 | * |
||
| 295 | * @param array $source |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | private function createContext(array $source): array |
||
| 299 | { |
||
| 300 | $config = Configuration::getInstance(); |
||
| 301 | $name = $config->getString('technicalcontact_name', null); |
||
| 302 | $mail = $config->getString('technicalcontact_email', null); |
||
| 303 | |||
| 304 | $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n"; |
||
| 305 | |||
| 306 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||
| 307 | if (array_key_exists($source['src'], $this->state)) { |
||
| 308 | $sourceState = $this->state[$source['src']]; |
||
| 309 | |||
| 310 | if (isset($sourceState['last-modified'])) { |
||
| 311 | $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n"; |
||
| 312 | } |
||
| 313 | |||
| 314 | if (isset($sourceState['etag'])) { |
||
| 315 | $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n"; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return ['http' => ['header' => $rawheader]]; |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * @param array $source |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | private function addCachedMetadata(array $source): void |
||
| 329 | { |
||
| 330 | if (isset($this->oldMetadataSrc)) { |
||
| 331 | foreach ($this->types as $type) { |
||
| 332 | foreach ($this->oldMetadataSrc->getMetadataSet($type) as $entity) { |
||
| 333 | if (array_key_exists('metarefresh:src', $entity)) { |
||
| 334 | if ($entity['metarefresh:src'] == $source['src']) { |
||
| 335 | $this->addMetadata($source['src'], $entity, $type); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Store caching state data for a source |
||
| 346 | * |
||
| 347 | * @param array $source |
||
| 348 | * @param array|null $responseHeaders |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | private function saveState(array $source, ?array $responseHeaders): void |
||
| 369 | } |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Parse XML metadata and return entities |
||
| 376 | * |
||
| 377 | * @param string $data |
||
| 378 | * @param array $source |
||
| 379 | * @return \SimpleSAML\Metadata\SAMLParser[] |
||
| 380 | * @throws \Exception |
||
| 381 | */ |
||
| 382 | private function loadXML(string $data, array $source): array |
||
| 383 | { |
||
| 384 | try { |
||
| 385 | $doc = \SAML2\DOMDocumentFactory::fromString($data); |
||
| 386 | } catch (\Exception $e) { |
||
| 387 | throw new \Exception('Failed to read XML from ' . $source['src']); |
||
| 388 | } |
||
| 389 | return \SimpleSAML\Metadata\SAMLParser::parseDescriptorsElement($doc->documentElement); |
||
| 390 | } |
||
| 391 | |||
| 392 | |||
| 393 | /** |
||
| 394 | * This function writes the state array back to disk |
||
| 395 | * |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | public function writeState(): void |
||
| 399 | { |
||
| 400 | if ($this->changed && !is_null($this->stateFile)) { |
||
| 401 | Logger::debug('Writing: ' . $this->stateFile); |
||
| 402 | \SimpleSAML\Utils\System::writeFile( |
||
| 403 | $this->stateFile, |
||
| 404 | "<?php\n/* This file was generated by the metarefresh module at " . $this->getTime() . ".\n" . |
||
| 405 | " Do not update it manually as it will get overwritten. */\n" . |
||
| 406 | '$state = ' . var_export($this->state, true) . ";\n?>\n", |
||
| 407 | 0644 |
||
| 408 | ); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * This function writes the metadata to stdout. |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | public function dumpMetadataStdOut(): void |
||
| 419 | { |
||
| 420 | foreach ($this->metadata as $category => $elements) { |
||
| 421 | echo '/* The following data should be added to metadata/' . $category . '.php. */' . "\n"; |
||
| 422 | |||
| 423 | foreach ($elements as $m) { |
||
| 424 | $filename = $m['filename']; |
||
| 425 | $entityID = $m['metadata']['entityid']; |
||
| 426 | |||
| 427 | echo "\n"; |
||
| 428 | echo '/* The following metadata was generated from ' . $filename . ' on ' . $this->getTime() . '. */' . "\n"; |
||
| 429 | echo '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n"; |
||
| 430 | } |
||
| 431 | |||
| 432 | echo "\n"; |
||
| 433 | echo '/* End of data which should be added to metadata/' . $category . '.php. */' . "\n"; |
||
| 434 | echo "\n"; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * This function adds metadata from the specified file to the list of metadata. |
||
| 441 | * This function will return without making any changes if $metadata is NULL. |
||
| 442 | * |
||
| 443 | * @param string $filename The filename the metadata comes from. |
||
| 444 | * @param \SAML2\XML\md\AttributeAuthorityDescriptor[]|null $metadata The metadata. |
||
| 445 | * @param string $type The metadata type. |
||
| 446 | * @param array|null $template The template. |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | private function addMetadata(string $filename, ?array $metadata, string $type, array $template = null): void |
||
| 450 | { |
||
| 451 | if ($metadata === null) { |
||
| 452 | return; |
||
| 453 | } |
||
| 454 | |||
| 455 | if (isset($template)) { |
||
| 456 | $metadata = array_merge($metadata, $template); |
||
| 457 | } |
||
| 458 | |||
| 459 | $metadata['metarefresh:src'] = $filename; |
||
| 460 | if (!array_key_exists($type, $this->metadata)) { |
||
| 461 | $this->metadata[$type] = []; |
||
| 462 | } |
||
| 463 | |||
| 464 | // If expire is defined in constructor... |
||
| 465 | if (!empty($this->expire)) { |
||
| 466 | // If expire is already in metadata |
||
| 467 | if (array_key_exists('expire', $metadata)) { |
||
| 468 | // Override metadata expire with more restrictive global config |
||
| 469 | if ($this->expire < $metadata['expire']) { |
||
| 470 | $metadata['expire'] = $this->expire; |
||
| 471 | } |
||
| 472 | |||
| 473 | // If expire is not already in metadata use global config |
||
| 474 | } else { |
||
| 475 | $metadata['expire'] = $this->expire; |
||
| 476 | } |
||
| 477 | } |
||
| 478 | $this->metadata[$type][] = ['filename' => $filename, 'metadata' => $metadata]; |
||
| 479 | } |
||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * This function writes the metadata to an ARP file |
||
| 484 | * |
||
| 485 | * @param \SimpleSAML\Configuration $config |
||
| 486 | * @return void |
||
| 487 | */ |
||
| 488 | public function writeARPfile(Configuration $config): void |
||
| 489 | { |
||
| 490 | $arpfile = $config->getValue('arpfile'); |
||
| 491 | $types = ['saml20-sp-remote']; |
||
| 492 | |||
| 493 | $md = []; |
||
| 494 | foreach ($this->metadata as $category => $elements) { |
||
| 495 | if (!in_array($category, $types, true)) { |
||
| 496 | continue; |
||
| 497 | } |
||
| 498 | $md = array_merge($md, $elements); |
||
| 499 | } |
||
| 500 | |||
| 501 | // $metadata, $attributemap, $prefix, $suffix |
||
| 502 | $arp = new \SimpleSAML\Module\metarefresh\ARP( |
||
| 503 | $md, |
||
| 504 | $config->getValue('attributemap', ''), |
||
| 505 | $config->getValue('prefix', ''), |
||
| 506 | $config->getValue('suffix', '') |
||
| 507 | ); |
||
| 508 | |||
| 509 | |||
| 510 | $arpxml = $arp->getXML(); |
||
| 511 | |||
| 512 | Logger::info('Writing ARP file: ' . $arpfile . "\n"); |
||
| 513 | file_put_contents($arpfile, $arpxml); |
||
| 514 | } |
||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * This function writes the metadata to to separate files in the output directory. |
||
| 519 | * |
||
| 520 | * @param string $outputDir |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | public function writeMetadataFiles(string $outputDir): void |
||
| 561 | } |
||
| 562 | } |
||
| 563 | } |
||
| 564 | } |
||
| 565 | |||
| 566 | |||
| 567 | /** |
||
| 568 | * Save metadata for loading with the 'serialize' metadata loader. |
||
| 569 | * |
||
| 570 | * @param string $outputDir The directory we should save the metadata to. |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function writeMetadataSerialize(string $outputDir): void |
||
| 611 | } |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | |||
| 616 | /** |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | private function getTime(): string |
||
| 626 |