Failed Conditions
Push — master ( bbfade...32fb37 )
by Florent
02:44
created

PublicKeyCredential::createFromJson()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace U2FAuthentication\Fido2;
15
16
class PublicKeyCredential extends Credential
17
{
18
    /**
19
     * @var string
20
     */
21
    private $rawId;
22
23
    /**
24
     * @var AuthenticatorAttestationResponse
25
     */
26
    private $response;
27
28
    /**
29
     * PublicKeyCredential constructor.
30
     *
31
     * @param string                           $id
32
     * @param string                           $type
33
     * @param string                           $rawId
34
     * @param AuthenticatorAttestationResponse $response
35
     */
36
    public function __construct(string $id, string $type, string $rawId, AuthenticatorAttestationResponse $response)
37
    {
38
        parent::__construct($id, $type);
39
        $this->rawId = $rawId;
40
        $this->response = $response;
41
    }
42
43
    /**
44
     * @param array $json
45
     *
46
     * @return PublicKeyCredential
47
     */
48
    public static function createFromJson(array $json): self
49
    {
50
        if (!array_key_exists('id', $json)) {
51
            throw new \InvalidArgumentException();
52
        }
53
        if (!array_key_exists('rawId', $json)) {
54
            throw new \InvalidArgumentException();
55
        }
56
        if (!array_key_exists('type', $json)) {
57
            throw new \InvalidArgumentException();
58
        }
59
        if (!array_key_exists('response', $json)) {
60
            throw new \InvalidArgumentException();
61
        }
62
63
        return new self(
64
            $json['id'],
65
            $json['type'],
66
            $json['rawId'],
67
            AuthenticatorAttestationResponse::createFromJson($json['response'])
68
        );
69
    }
70
71
    /**
72
     * @param string $data
73
     *
74
     * @return PublicKeyCredential
75
     */
76
    public static function createFromReceivedData(string $data): self
77
    {
78
        $json = json_decode($data, true);
79
80
        return self::createFromJson($json);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getRawId(): string
87
    {
88
        return $this->rawId;
89
    }
90
91
    /**
92
     * @return AuthenticatorAttestationResponse
93
     */
94
    public function getResponse(): AuthenticatorAttestationResponse
95
    {
96
        return $this->response;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function jsonSerialize(): array
103
    {
104
        return [
105
            'id'       => $this->getId(),
106
            'type'     => $this->getType(),
107
            'rawId'    => $this->getRawId(),
108
            'response' => $this->response,
109
        ];
110
    }
111
}
112