Passed
Push — master ( 898cc7...450add )
by Thomas Mauro
03:07
created

TokenSet::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\Token;
6
7
use function TMV\OpenIdClient\base64url_decode;
8
use TMV\OpenIdClient\Exception\RuntimeException;
9
10
class TokenSet implements TokenSetInterface
11
{
12
    /** @var null|string */
13
    private $code;
14
15
    /** @var null|string */
16
    private $state;
17
18
    /** @var string|null */
19
    private $tokenType;
20
21
    /** @var string|null */
22
    private $accessToken;
23
24
    /** @var string|null */
25
    private $idToken;
26
27
    /** @var string|null */
28
    private $refreshToken;
29
30
    /** @var int|null */
31
    private $expiresIn;
32
33
    /** @var string|null */
34
    private $codeVerifier;
35
36 8
    public static function fromParams(array $data): TokenSetInterface
37
    {
38 8
        $token = new static();
39 8
        $token->code = $data['code'] ?? null;
40 8
        $token->state = $data['state'] ?? null;
41 8
        $token->tokenType = $data['token_type'] ?? null;
42 8
        $token->accessToken = $data['access_token'] ?? null;
43 8
        $token->idToken = $data['id_token'] ?? null;
44 8
        $token->refreshToken = $data['refresh_token'] ?? null;
45 8
        $token->expiresIn = \array_key_exists('expires_in', $data) ? (int) $data['expires_in'] : null;
46 8
        $token->codeVerifier = $data['code_verifier'] ?? null;
47
48 8
        return $token;
49
    }
50
51
    /**
52
     * @return string|null
53
     */
54 1
    public function getCode(): ?string
55
    {
56 1
        return $this->code;
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62 1
    public function getState(): ?string
63
    {
64 1
        return $this->state;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70 1
    public function getTokenType(): ?string
71
    {
72 1
        return $this->tokenType;
73
    }
74
75
    /**
76
     * @return string|null
77
     */
78 1
    public function getAccessToken(): ?string
79
    {
80 1
        return $this->accessToken;
81
    }
82
83
    /**
84
     * @return string|null
85
     */
86 1
    public function getIdToken(): ?string
87
    {
88 1
        return $this->idToken;
89
    }
90
91
    /**
92
     * @return string|null
93
     */
94 1
    public function getRefreshToken(): ?string
95
    {
96 1
        return $this->refreshToken;
97
    }
98
99
    /**
100
     * @return int|null
101
     */
102 1
    public function getExpiresIn(): ?int
103
    {
104 1
        return $this->expiresIn;
105
    }
106
107
    /**
108
     * @return string|null
109
     */
110 1
    public function getCodeVerifier(): ?string
111
    {
112 1
        return $this->codeVerifier;
113
    }
114
115
    /**
116
     * @return array<string, mixed>
117
     */
118
    public function jsonSerialize(): array
119
    {
120
        $data = [
121
            'code' => $this->code,
122
            'state' => $this->state,
123
            'token_type' => $this->tokenType,
124
            'access_token' => $this->accessToken,
125
            'id_token' => $this->idToken,
126
            'refresh_token' => $this->refreshToken,
127
            'expires_in' => $this->expiresIn,
128
            'code_verifier' => $this->codeVerifier,
129
        ];
130
131
        return \array_filter($data, static function ($value) {
132
            return null !== $value;
133
        });
134
    }
135
136
    /**
137
     * @return array<string, mixed>
138
     */
139
    public function claims(): array
140
    {
141
        if (! $this->idToken) {
142
            throw new RuntimeException('Unable to retrieve claims without an id_token');
143
        }
144
145
        $data = \json_decode(base64url_decode(\explode('.', $this->idToken)[1] ?? ''), true);
146
147
        if (! \is_array($data)) {
148
            throw new RuntimeException('Unable to decode id_token payload');
149
        }
150
151
        return $data;
152
    }
153
}
154