|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Store the configuration of a provider |
|
9
|
|
|
* |
|
10
|
|
|
* Note: Do not create directly, use ProviderConfigPool::create methods instead |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ProviderConfig implements \ArrayAccess |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $url; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array<string, mixed> |
|
21
|
|
|
*/ |
|
22
|
|
|
private $config; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $openid; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var CacheInterface|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $cache; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @internal Use ProviderConfigPool create instead |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $url |
|
39
|
|
|
* @param array<string, mixed> $config |
|
40
|
|
|
* @param bool $openid |
|
41
|
|
|
*/ |
|
42
|
199 |
|
public function __construct(string $url, array $config, bool $openid) |
|
43
|
|
|
{ |
|
44
|
199 |
|
$this->url = $url; |
|
45
|
199 |
|
$this->config = $config; |
|
46
|
199 |
|
$this->openid = $openid; |
|
47
|
199 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public function url(): string |
|
53
|
|
|
{ |
|
54
|
6 |
|
return $this->url; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function config(): array |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->config; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
136 |
|
public function openid(): bool |
|
69
|
|
|
{ |
|
70
|
136 |
|
return $this->openid; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $offset |
|
77
|
|
|
*/ |
|
78
|
188 |
|
public function offsetExists($offset): bool |
|
79
|
|
|
{ |
|
80
|
188 |
|
return isset($this->config[$offset]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $offset |
|
87
|
|
|
*/ |
|
88
|
|
|
#[\ReturnTypeWillChange] |
|
89
|
138 |
|
public function offsetGet($offset) |
|
90
|
|
|
{ |
|
91
|
138 |
|
return $this->config[$offset]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $offset |
|
98
|
|
|
* @param mixed $value |
|
99
|
|
|
*/ |
|
100
|
24 |
|
public function offsetSet($offset, $value): void |
|
101
|
|
|
{ |
|
102
|
24 |
|
$this->config[$offset] = $value; |
|
103
|
24 |
|
$this->save(); |
|
104
|
24 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $offset |
|
110
|
|
|
*/ |
|
111
|
|
|
public function offsetUnset($offset): void |
|
112
|
|
|
{ |
|
113
|
|
|
unset($this->config[$offset]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param CacheInterface|null $cache |
|
118
|
|
|
* @internal |
|
119
|
|
|
*/ |
|
120
|
186 |
|
public function setCache(?CacheInterface $cache): void |
|
121
|
|
|
{ |
|
122
|
186 |
|
$this->cache = $cache; |
|
123
|
186 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Save the config into cache |
|
127
|
|
|
*/ |
|
128
|
90 |
|
public function save(): void |
|
129
|
|
|
{ |
|
130
|
90 |
|
if ($this->cache) { |
|
131
|
3 |
|
$this->cache->set(ProviderConfigPool::urlToKey($this->url), $this); |
|
132
|
|
|
} |
|
133
|
90 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function __sleep() |
|
139
|
|
|
{ |
|
140
|
|
|
// Ignore cache |
|
141
|
|
|
return ['url', 'config', 'openid']; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|