|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\SAML2; |
| 6 | + |
| 7 | +use SimpleSAML\SAML2\Metadata\IdentityProvider; |
| 8 | +use SimpleSAML\SAML2\Metadata\ServiceProvider; |
| 9 | +use SimpleSAML\SAML2\MetadataProviderInterface; |
| 10 | + |
| 11 | +use function sha1; |
| 12 | + |
| 13 | +final class MockMetadataProvider implements MetadataProviderInterface |
| 14 | +{ |
| 15 | + /** @var \SimpleSAML\SAML2\Metadata\AbstractProvider[] $entities */ |
| 16 | + protected array $entities; |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * @param \SimpleSAML\SAML2\Metadata\AbstractProvider[] $entities |
| 21 | + */ |
| 22 | + public function __construct(array $entities) |
| 23 | + { |
| 24 | + $this->entities = $entities; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * Find IdP-metadata based on a SHA-1 hash of the entityID. Return `null` if not found. |
| 30 | + */ |
| 31 | + public function getIdPMetadataForSha1(string $hash): ?IdentityProvider |
| 32 | + { |
| 33 | + foreach ($this->entities as $entity) { |
| 34 | + if (($entity instanceof IdentityProvider) && ($hash === sha1($entity->getEntityId()))) { |
| 35 | + return $entity; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Find SP-metadata based on a SHA-1 hash of the entityID. Return `null` if not found. |
| 45 | + */ |
| 46 | + public function getSPMetadataForSha1(string $hash): ?ServiceProvider |
| 47 | + { |
| 48 | + foreach ($this->entities as $entity) { |
| 49 | + if (($entity instanceof ServiceProvider) && ($hash === sha1($entity->getEntityId()))) { |
| 50 | + return $entity; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Find IdP-metadata based on an entityID. Return `null` if not found. |
| 60 | + */ |
| 61 | + public function getIdPMetadata(string $entityId): ?IdentityProvider |
| 62 | + { |
| 63 | + foreach ($this->entities as $entity) { |
| 64 | + if (($entity instanceof IdentityProvider) && ($entityId === $entity->getEntityId())) { |
| 65 | + return $entity; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * Find SP-metadata based on an entityID. Return `null` if not found. |
| 75 | + */ |
| 76 | + public function getSPMetadata(string $entityId): ?ServiceProvider |
| 77 | + { |
| 78 | + foreach ($this->entities as $entity) { |
| 79 | + if (($entity instanceof ServiceProvider) && ($entityId === $entity->getEntityId())) { |
| 80 | + return $entity; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + } |
| 86 | +} |
0 commit comments