Completed
Push — master ( d7d555...8d86ce )
by Thomas
06:51 queued 03:43
created

AttestationObject::parse()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 3
rs 9.8666
1
<?php
2
3
namespace MadWizard\WebAuthn\Attestation;
4
5
use MadWizard\WebAuthn\Exception\CborException;
6
use MadWizard\WebAuthn\Exception\ParseException;
7
use MadWizard\WebAuthn\Exception\WebAuthnException;
8
use MadWizard\WebAuthn\Format\ByteBuffer;
9
use MadWizard\WebAuthn\Format\CborDecoder;
10
use MadWizard\WebAuthn\Format\DataValidator;
11
use function is_array;
12
13
final class AttestationObject
14
{
15
    /**
16
     * @var string
17
     */
18
    private $format;
19
20
    /**
21
     * @var array
22
     */
23
    private $statement;
24
25
    /**
26
     * @var ByteBuffer
27
     */
28
    private $authData;
29
30 33
    public function __construct(string $format, array $statement, ByteBuffer $authData)
31
    {
32 33
        $this->format = $format;
33 33
        $this->statement = $statement;
34 33
        $this->authData = $authData;
35 33
    }
36
37 35
    public static function parse(ByteBuffer $buffer)
38
    {
39
        try {
40 35
            $data = CborDecoder::decode($buffer);
41 34
            if (!is_array($data)) {
42 1
                throw new WebAuthnException('Expecting attestation object to be a CBOR map.');
43
            }
44
45 33
            DataValidator::checkTypes(
46 33
                $data,
47
                [
48 33
                    'fmt' => 'string',
49
                    'attStmt' => 'array',
50
                    'authData' => ByteBuffer::class,
51
                ]
52
            );
53 30
            return new self($data['fmt'], $data['attStmt'], $data['authData']);
54 5
        } catch (CborException $e) {
55 1
            throw new ParseException('Failed to parse CBOR attestation object.', 0, $e);
56
        }
57
    }
58
59 32
    public function getFormat(): string
60
    {
61 32
        return $this->format;
62
    }
63
64 29
    public function getStatement(): array
65
    {
66 29
        return $this->statement;
67
    }
68
69 11
    public function getAuthenticatorData(): ByteBuffer
70
    {
71 11
        return $this->authData;
72
    }
73
}
74