7
7
use Psr \Http \Message \RequestFactoryInterface ;
8
8
use Psr \Http \Message \RequestInterface ;
9
9
use Psr \Cache \CacheItemPoolInterface ;
10
+ use Psr \Cache \CacheItemInterface ;
10
11
use LogicException ;
11
12
use OutOfBoundsException ;
12
13
use RuntimeException ;
13
14
15
+ /**
16
+ * @template-implements ArrayAccess<string, Key>
17
+ */
14
18
class CachedKeySet implements ArrayAccess
15
19
{
20
+ /**
21
+ * @var string
22
+ */
16
23
private $ jwkUri ;
24
+ /**
25
+ * @var ClientInterface
26
+ */
17
27
private $ httpClient ;
28
+ /**
29
+ * @var RequestFactoryInterface
30
+ */
18
31
private $ httpFactory ;
32
+ /**
33
+ * @var CacheItemPoolInterface
34
+ */
19
35
private $ cache ;
36
+ /**
37
+ * @var int
38
+ */
20
39
private $ expiresAfter ;
21
40
41
+ /**
42
+ * @var ?CacheItemInterface
43
+ */
22
44
private $ cacheItem ;
45
+ /**
46
+ * @var array<string, Key>
47
+ */
23
48
private $ keySet ;
49
+ /**
50
+ * @var string
51
+ */
52
+ private $ cacheKey ;
53
+ /**
54
+ * @var string
55
+ */
24
56
private $ cacheKeyPrefix = 'jwk ' ;
57
+ /**
58
+ * @var int
59
+ */
25
60
private $ maxKeyLength = 64 ;
26
61
27
62
public function __construct (
28
- $ jwkUri ,
63
+ string $ jwkUri ,
29
64
ClientInterface $ httpClient ,
30
65
RequestFactoryInterface $ httpFactory ,
31
66
CacheItemPoolInterface $ cache ,
32
- $ expiresAfter = null
67
+ int $ expiresAfter = null
33
68
) {
34
69
$ this ->jwkUri = $ jwkUri ;
35
70
$ this ->cacheKey = $ this ->getCacheKey ($ jwkUri );
@@ -39,30 +74,45 @@ public function __construct(
39
74
$ this ->expiresAfter = $ expiresAfter ;
40
75
}
41
76
42
- public function offsetGet ($ keyId )
77
+ /**
78
+ * @param string $keyId
79
+ * @return Key
80
+ */
81
+ public function offsetGet ($ keyId ): Key
43
82
{
44
83
if (!$ this ->keyIdExists ($ keyId )) {
45
84
throw new OutOfBoundsException ('Key ID not found ' );
46
85
}
47
86
return $ this ->keySet [$ keyId ];
48
87
}
49
88
50
- public function offsetExists ($ keyId )
89
+ /**
90
+ * @param string $keyId
91
+ * @return bool
92
+ */
93
+ public function offsetExists ($ keyId ): bool
51
94
{
52
95
return $ this ->keyIdExists ($ keyId );
53
96
}
54
97
55
- public function offsetSet ($ offset , $ value )
98
+ /**
99
+ * @param string $offset
100
+ * @param Key $value
101
+ */
102
+ public function offsetSet ($ offset , $ value ): void
56
103
{
57
104
throw new LogicException ('Method not implemented ' );
58
105
}
59
106
60
- public function offsetUnset ($ offset )
107
+ /**
108
+ * @param string $offset
109
+ */
110
+ public function offsetUnset ($ offset ): void
61
111
{
62
112
throw new LogicException ('Method not implemented ' );
63
113
}
64
114
65
- private function keyIdExists ($ keyId )
115
+ private function keyIdExists (string $ keyId ): bool
66
116
{
67
117
$ keySetToCache = null ;
68
118
if (null === $ this ->keySet ) {
@@ -97,18 +147,18 @@ private function keyIdExists($keyId)
97
147
return true ;
98
148
}
99
149
100
- private function getCacheItem ()
150
+ private function getCacheItem (): CacheItemInterface
101
151
{
102
- if ($ this ->cacheItem ) {
103
- return $ this ->cacheItem ;
152
+ if (is_null ( $ this ->cacheItem ) ) {
153
+ $ this ->cacheItem = $ this -> cache -> getItem ( $ this -> cacheKey ) ;
104
154
}
105
155
106
- return $ this ->cacheItem = $ this -> cache -> getItem ( $ this -> cacheKey ) ;
156
+ return $ this ->cacheItem ;
107
157
}
108
158
109
- private function getCacheKey ($ jwkUri )
159
+ private function getCacheKey (string $ jwkUri ): string
110
160
{
111
- if (is_null ($ jwkUri )) {
161
+ if (empty ($ jwkUri )) {
112
162
throw new RuntimeException ('JWK URI is empty ' );
113
163
}
114
164
0 commit comments