Completed
Push — master ( cf2c3c...c63c7f )
by Florent
02:05
created

PublicKeyCredentialDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
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) 2014-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
use Assert\Assertion;
17
18
class PublicKeyCredentialDescriptor implements \JsonSerializable
19
{
20
    public const CREDENTIAL_TYPE_PUBLIC_KEY = 'public-key';
21
22
    public const AUTHENTICATOR_TRANSPORT_USB = 'usb';
23
    public const AUTHENTICATOR_TRANSPORT_NFC = 'nfc';
24
    public const AUTHENTICATOR_TRANSPORT_BLE = 'ble';
25
    public const AUTHENTICATOR_TRANSPORT_INTERNAL = 'internal';
26
27
    private $type;
28
29
    private $id;
30
31
    /**
32
     * @var string[]
33
     */
34
    private $transports;
35
36
    /**
37
     * @param string[] $transports
38
     */
39
    public function __construct(string $type, string $id, array $transports = [])
40
    {
41
        $this->type = $type;
42
        $this->id = $id;
43
        $this->transports = $transports;
44
    }
45
46
    public function getType(): string
47
    {
48
        return $this->type;
49
    }
50
51
    public function getId(): string
52
    {
53
        return $this->id;
54
    }
55
56
    /**
57
     * @return string[]
58
     */
59
    public function getTransports(): array
60
    {
61
        return $this->transports;
62
    }
63
64
    public static function createFromJson(string $json): self
65
    {
66
        $data = \Safe\json_decode($json, true);
67
        Assertion::isArray($data, 'Invalid input.');
68
        Assertion::keyExists($data, 'type', 'Invalid input.');
69
        Assertion::keyExists($data, 'id', 'Invalid input.');
70
71
        return new self(
72
            $data['type'],
73
            \Safe\base64_decode($data['id'], true),
74
            $data['transports'] ?? null
0 ignored issues
show
Bug introduced by
It seems like $data['transports'] ?? null can also be of type null; however, parameter $transports of U2FAuthentication\Fido2\...scriptor::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            /** @scrutinizer ignore-type */ $data['transports'] ?? null
Loading history...
75
        );
76
    }
77
78
    public function jsonSerialize()
79
    {
80
        $json = [
81
            'type' => $this->type,
82
            'id' => base64_encode($this->id),
83
        ];
84
        if (!empty($this->transports)) {
85
            $json['transports'] = $this->transports;
86
        }
87
88
        return $json;
89
    }
90
}
91