Passed
Push — master ( 68b106...6cdc56 )
by Thomas
01:58
created

AttestationFormatRegistry::setAllowUnsupportedFormats()   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 $allowUnsupportedFormats = false;
23
24
    /**
25
     * @var bool
26
     */
27
    private $strictSupportedFormats = true;
28
29 28
    public function __construct()
30
    {
31 28
    }
32
33
    public function setAllowUnsupportedFormats(bool $allowed): void
34
    {
35
        $this->allowUnsupportedFormats = $allowed;
36
    }
37
38 22
    public function addFormat(AttestationFormatInterface $format)
39
    {
40 22
        $this->formats[$format->getFormatId()] = $format;
41 22
    }
42
43 3
    public function createStatement(AttestationObject $attestationObject): AttestationStatementInterface
44
    {
45 3
        $formatId = $attestationObject->getFormat();
46 3
        $format = $this->formats[$formatId] ?? null;
47 3
        if ($format === null) {
48 1
            if ($this->strictSupportedFormats) {
49 1
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
50
            }
51
            return new UnsupportedAttestationStatement($attestationObject);
52
        }
53 2
        return $format->createStatement($attestationObject);
54
    }
55
56 3
    public function getVerifier(string $formatId): AttestationVerifierInterface
57
    {
58 3
        $format = $this->formats[$formatId] ?? null;
59 3
        if ($format === null) {
60 1
            if ($this->strictSupportedFormats) {
61 1
                throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId));
62
            }
63
            return new UnsupportedAttestationVerifier();
64
        }
65 2
        return $format->getVerifier();
66
    }
67
68 18
    public function strictSupportedFormats(bool $strict)
69
    {
70 18
        $this->strictSupportedFormats = $strict;
71 18
    }
72
}
73