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

CollectedClientData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 6
rs 9.4285
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 CollectedClientData
17
{
18
    /**
19
     * @var string
20
     */
21
    private $type;
22
23
    /**
24
     * @var string
25
     */
26
    private $challenge;
27
28
    /**
29
     * @var string
30
     */
31
    private $origin;
32
33
    /**
34
     * @var null|TokenBinding
35
     */
36
    private $tokenBinding;
37
38
    /**
39
     * CollectedClientData constructor.
40
     *
41
     * @param string            $type
42
     * @param string            $challenge
43
     * @param string            $origin
44
     * @param null|TokenBinding $tokenBinding
45
     */
46
    public function __construct(string $type, string $challenge, string $origin, ?TokenBinding $tokenBinding)
47
    {
48
        $this->type = $type;
49
        $this->challenge = $challenge;
50
        $this->origin = $origin;
51
        $this->tokenBinding = $tokenBinding;
52
    }
53
54
    /**
55
     * @param array $json
56
     *
57
     * @return CollectedClientData
58
     */
59
    public static function createFormJson(array $json): self
60
    {
61
        if (!array_key_exists('type', $json)) {
62
            throw new \InvalidArgumentException();
63
        }
64
        if (!array_key_exists('challenge', $json)) {
65
            throw new \InvalidArgumentException();
66
        }
67
        if (!array_key_exists('origin', $json)) {
68
            throw new \InvalidArgumentException();
69
        }
70
        if (array_key_exists('tokenBinding', $json)) {
71
            $tokenBinding = TokenBinding::createFormJson($json['tokenBinding']);
72
        } else {
73
            $tokenBinding = null;
74
        }
75
76
        return new self(
77
            $json['type'],
78
            $json['challenge'],
79
            $json['origin'],
80
            $tokenBinding
81
        );
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getType(): string
88
    {
89
        return $this->type;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getChallenge(): string
96
    {
97
        return $this->challenge;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getOrigin(): string
104
    {
105
        return $this->origin;
106
    }
107
108
    /**
109
     * @return null|TokenBinding
110
     */
111
    public function getTokenBinding(): ?TokenBinding
112
    {
113
        return $this->tokenBinding;
114
    }
115
}
116