Passed
Push — master ( f80f70...56399a )
by Thomas Mauro
03:05 queued 10s
created

TokenSet::fromParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\Token;
6
7
use function array_key_exists;
8
use JsonSerializable;
9
10
/**
11
 * @psalm-import-type TokenSetType from TokenSetInterface
12
 * @psalm-import-type TokenSetClaimsType from TokenSetInterface
13
 * @psalm-import-type TokenSetMixedType from TokenSetInterface
14
 */
15
final class TokenSet implements TokenSetInterface, JsonSerializable
16
{
17
    /**
18
     * @var array<string, mixed>
19
     * @psalm-var TokenSetType
20
     */
21
    private $attributes = [];
22
23
    /**
24
     * @var array<string, mixed>
25
     * @psalm-var TokenSetClaimsType
26
     */
27
    private $claims = [];
28
29
    /**
30
     * @psalm-param TokenSetType $attributes
31
     * @psalm-param TokenSetClaimsType $claims
32
     */
33 8
    private function __construct(array $attributes, array $claims)
34
    {
35 8
        $this->attributes = $attributes;
36 8
        $this->claims = $claims;
37 8
    }
38
39
    /**
40
     * @param array<string, mixed> $data
41
     * @psalm-param TokenSetMixedType $data
42
     *
43
     * @return TokenSetInterface
44
     */
45 8
    public static function fromParams(array $data): TokenSetInterface
46
    {
47 8
        $claims = [];
48 8
        if (array_key_exists('claims', $data)) {
49
            $claims = $data['claims'];
50
            unset($data['claims']);
51
        }
52
53 8
        return new static($data, $claims);
54
    }
55
56
    public function getAttributes(): array
57
    {
58
        return $this->attributes;
59
    }
60
61 1
    public function getCode(): ?string
62
    {
63 1
        return $this->attributes['code'] ?? null;
64
    }
65
66 1
    public function getState(): ?string
67
    {
68 1
        return $this->attributes['state'] ?? null;
69
    }
70
71 1
    public function getTokenType(): ?string
72
    {
73 1
        return $this->attributes['token_type'] ?? null;
74
    }
75
76 1
    public function getAccessToken(): ?string
77
    {
78 1
        return $this->attributes['access_token'] ?? null;
79
    }
80
81 1
    public function getIdToken(): ?string
82
    {
83 1
        return $this->attributes['id_token'] ?? null;
84
    }
85
86 1
    public function getRefreshToken(): ?string
87
    {
88 1
        return $this->attributes['refresh_token'] ?? null;
89
    }
90
91 1
    public function getExpiresIn(): ?int
92
    {
93
        /** @var int|string|null $expiresIn */
94 1
        $expiresIn = $this->attributes['expires_in'] ?? null;
95
96 1
        return null !== $expiresIn ? (int) $expiresIn : null;
97
    }
98
99 1
    public function getCodeVerifier(): ?string
100
    {
101 1
        return $this->attributes['code_verifier'] ?? null;
102
    }
103
104
    public function withIdToken(string $idToken): TokenSetInterface
105
    {
106
        $clone = clone $this;
107
        $clone->attributes['id_token'] = $idToken;
108
109
        return $clone;
110
    }
111
112
    public function withClaims(array $claims): TokenSetInterface
113
    {
114
        $clone = clone $this;
115
        $clone->claims = $claims;
116
117
        return $clone;
118
    }
119
120
    /**
121
     * @return array<string, mixed>
122
     * @psalm-return TokenSetType
123
     */
124
    public function jsonSerialize(): array
125
    {
126
        return $this->attributes;
127
    }
128
129
    public function claims(): array
130
    {
131
        return $this->claims;
132
    }
133
}
134