Passed
Push — master ( 3266fb...94512e )
by Thomas
03:56 queued 01:13
created

AttestationFormatRegistry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 49
ccs 20
cts 22
cp 0.9091
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A strictSupportedFormats() 0 3 1
A getVerifier() 0 10 3
A createStatement() 0 11 3
A addFormat() 0 3 1
1
<?php
2
3
namespace MadWizard\WebAuthn\Attestation\Registry;
4
5
use MadWizard\WebAuthn\Attestation\AttestationObject;
6
use MadWizard\WebAuthn\Attestation\Statement\AttestationStatementInterface;
7
use MadWizard\WebAuthn\Attestation\Statement\UnsupportedAttestationStatement;
8
use MadWizard\WebAuthn\Attestation\Verifier\AttestationVerifierInterface;
9
use MadWizard\WebAuthn\Attestation\Verifier\UnsupportedAttestationVerifier;
10
use MadWizard\WebAuthn\Exception\FormatNotSupportedException;
11
12
final class AttestationFormatRegistry implements AttestationFormatRegistryInterface
13
{
14
    /**
15
     * @var AttestationFormatInterface[]
16
     */
17
    private $formats = [];
18
19
    /**
20
     * @var bool
21
     */
22
    private $strictSupportedFormats = true;
23
24 28
    public function __construct()
25
    {
26 28
    }
27
28 22
    public function addFormat(AttestationFormatInterface $format): void
29
    {
30 22
        $this->formats[$format->getFormatId()] = $format;
31 22
    }
32
33 3
    public function createStatement(AttestationObject $attestationObject): AttestationStatementInterface
34
    {
35 3
        $formatId = $attestationObject->getFormat();
36 3
        $format = $this->formats[$formatId] ?? null;
37 3
        if ($format === null) {
38 1
            if ($this->strictSupportedFormats) {
39 1
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
40
            }
41
            return new UnsupportedAttestationStatement($attestationObject);
42
        }
43 2
        return $format->createStatement($attestationObject);
44
    }
45
46 3
    public function getVerifier(string $formatId): AttestationVerifierInterface
47
    {
48 3
        $format = $this->formats[$formatId] ?? null;
49 3
        if ($format === null) {
50 1
            if ($this->strictSupportedFormats) {
51 1
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
52
            }
53
            return new UnsupportedAttestationVerifier();
54
        }
55 2
        return $format->getVerifier();
56
    }
57
58 18
    public function strictSupportedFormats(bool $strict): void
59
    {
60 18
        $this->strictSupportedFormats = $strict;
61 18
    }
62
}
63