Test Failed
Push — master ( 4771cb...51d50e )
by Thomas
08:50
created

edFormats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    public function __construct()
25
    {
26
    }
27
28
    public function addFormat(AttestationFormatInterface $format): void
29 28
    {
30
        $this->formats[$format->getFormatId()] = $format;
31 28
    }
32
33
    public function createStatement(AttestationObject $attestationObject): AttestationStatementInterface
34
    {
35
        $formatId = $attestationObject->getFormat();
36
        $format = $this->formats[$formatId] ?? null;
37
        if ($format === null) {
38 22
            if ($this->strictSupportedFormats) {
39
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
40 22
            }
41 22
            return new UnsupportedAttestationStatement($attestationObject);
42
        }
43 3
        return $format->createStatement($attestationObject);
44
    }
45 3
46 3
    public function getVerifier(string $formatId): AttestationVerifierInterface
47 3
    {
48 1
        $format = $this->formats[$formatId] ?? null;
49 1
        if ($format === null) {
50
            if ($this->strictSupportedFormats) {
51
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
52
            }
53 2
            return new UnsupportedAttestationVerifier();
54
        }
55
        return $format->getVerifier();
56 3
    }
57
58 3
    public function strictSupportedFormats(bool $strict): void
59 3
    {
60 1
        $this->strictSupportedFormats = $strict;
61 1
    }
62
}
63