| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\MetadataService\Service; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use Psr\EventDispatcher\EventDispatcherInterface; |
| 9 |
use Webauthn\Event\CanDispatchEvents; |
| 10 |
use Webauthn\Event\MetadataStatementFound; |
| 11 |
use Webauthn\Event\NullEventDispatcher; |
| 12 |
use Webauthn\Exception\MissingMetadataStatementException; |
| 13 |
use Webauthn\MetadataService\Statement\MetadataStatement; |
| 14 |
|
| 15 |
final class InMemoryMetadataService implements MetadataService, CanDispatchEvents |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private array $statements = []; |
| 21 |
|
| 22 |
private EventDispatcherInterface $dispatcher; |
| 23 |
|
| 24 |
public function __construct(MetadataStatement ...$statements) |
| 25 |
{ |
| 26 |
foreach ($statements as $statement) { |
| 27 |
$this->addStatements($statement); |
| 28 |
} |
| 29 |
$this->dispatcher = new NullEventDispatcher(); |
| 30 |
} |
| 31 |
|
| 32 |
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
| 33 |
{ |
| 34 |
$this->dispatcher = $eventDispatcher; |
| 35 |
} |
| 36 |
|
| 37 |
public static function create(MetadataStatement ...$statements): self |
| 38 |
{ |
| 39 |
return new self(...$statements); |
| 40 |
} |
| 41 |
|
| 42 |
public function addStatements(MetadataStatement ...$statements): self |
| 43 |
{ |
| 44 |
foreach ($statements as $statement) { |
| 45 |
$aaguid = $statement->aaguid; |
| 46 |
if ($aaguid === null) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
$this->statements[$aaguid] = $statement; |
| 50 |
} |
| 51 |
|
| 52 |
return $this; |
| 53 |
} |
| 54 |
|
| 55 |
public function list(): iterable |
| 56 |
{ |
| 57 |
yield from array_keys($this->statements); |
| 58 |
} |
| 59 |
|
| 60 |
public function has(string $aaguid): bool |
| 61 |
{ |
| 62 |
return array_key_exists($aaguid, $this->statements); |
| 63 |
} |
| 64 |
|
| 65 |
public function get(string $aaguid): MetadataStatement |
| 66 |
{ |
| 67 |
array_key_exists($aaguid, $this->statements) || throw MissingMetadataStatementException::create($aaguid); |
| 68 |
$mds = $this->statements[$aaguid]; |
| 69 |
$this->dispatcher->dispatch(MetadataStatementFound::create($mds)); |
| 70 |
|
| 71 |
return $mds; |
| 72 |
} |
| 73 |
} |