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

AttestationFormatRegistry::getVerifier()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
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