TokenSet::getState()   A
last analyzed

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