AbstractAuthenticatorResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 50
ccs 14
cts 19
cp 0.7368
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A asAssertionResponse() 0 3 1
A asAttestationResponse() 0 3 1
A getParsedClientData() 0 3 1
A __construct() 0 16 5
A getClientDataJson() 0 3 1
1
<?php
2
3
namespace MadWizard\WebAuthn\Dom;
4
5
use MadWizard\WebAuthn\Exception\ParseException;
6
use MadWizard\WebAuthn\Exception\WebAuthnException;
7
use function json_last_error;
8
9
abstract class AbstractAuthenticatorResponse implements AuthenticatorResponseInterface
10
{
11
    public const UTF8_BOM = "\xEF\xBB\xBF";
12
13
    /**
14
     * @var string
15
     */
16
    private $clientDataJson;
17
18
    /**
19
     * @var CollectedClientData
20
     */
21
    private $clientData;
22
23 26
    public function __construct(string $clientDataJson)
24
    {
25 26
        $this->clientDataJson = $clientDataJson;
26
27
        // Specification says to remove the UTF-8 byte order mark, if any
28 26
        if (\substr($clientDataJson, 0, 3) === self::UTF8_BOM) {
29 1
            $clientDataJson = substr($clientDataJson, 3);
30
        }
31 26
        $data = \json_decode($clientDataJson, true, 10);
32 26
        if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
33 3
            throw new ParseException('Unparseable client data JSON');
34
        }
35 23
        if (!\is_array($data)) {
36
            throw new ParseException('Expected object for client data');
37
        }
38 23
        $this->clientData = CollectedClientData::fromJson($data);
39 20
    }
40
41 11
    public function getClientDataJson(): string
42
    {
43 11
        return $this->clientDataJson;
44
    }
45
46 11
    public function getParsedClientData(): CollectedClientData
47
    {
48 11
        return $this->clientData;
49
    }
50
51
    public function asAttestationResponse(): AuthenticatorAttestationResponseInterface
52
    {
53
        throw new WebAuthnException('Response is not an attestation response.');
54
    }
55
56
    public function asAssertionResponse(): AuthenticatorAssertionResponseInterface
57
    {
58
        throw new WebAuthnException('Response is not an assertion response.');
59
    }
60
}
61