Passed
Pull Request — master (#11)
by Pol
07:45
created

TokenSet   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 60.98%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 117
ccs 25
cts 41
cp 0.6098
rs 10
c 2
b 0
f 0
wmc 17

15 Methods

Rating   Name   Duplication   Size   Complexity  
A fromParams() 0 9 2
A getCodeVerifier() 0 3 1
A getExpiresIn() 0 6 2
A jsonSerialize() 0 3 1
A getState() 0 3 1
A withClaims() 0 6 1
A getTokenType() 0 3 1
A claims() 0 3 1
A getRefreshToken() 0 3 1
A getAccessToken() 0 3 1
A __construct() 0 4 1
A withIdToken() 0 6 1
A getCode() 0 3 1
A getIdToken() 0 3 1
A getAttributes() 0 3 1
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