Test Failed
Push — master ( 9eb68a...5271f3 )
by Thomas
02:48
created

ExtensionRegistry::getExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace MadWizard\WebAuthn\Extension;
4
5
use MadWizard\WebAuthn\Exception\ConfigurationException;
6
use MadWizard\WebAuthn\Exception\UnsupportedException;
7
8
class ExtensionRegistry implements ExtensionRegistryInterface
9
{
10
    /**
11
     * @var array<string, ExtensionInterface>
12
     */
13
    private $extensions = [];
14
15
    public function __construct()
16
    {
17
    }
18
19
    public function addExtension(ExtensionInterface $extension)
20
    {
21
        $id = $extension->getIdentifier();
22
        if (isset($this->extensions[$id])) {
23
            throw new ConfigurationException(sprintf('Extension with identifier %s is already registered.', $id));
24
        }
25
        $this->extensions[$id] = $extension;
26
    }
27
28
    public function getExtension(string $extensionId): ExtensionInterface
29
    {
30
        $ext = $this->extensions[$extensionId] ?? null;
31
        if ($ext === null) {
32
            throw new UnsupportedException(sprintf('Extension with id %s not supported.', $extensionId));
33
        }
34
        return $ext;
35
    }
36
}
37