1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocialiteProviders\Manager\Helpers; |
4
|
|
|
|
5
|
|
|
use SocialiteProviders\Manager\Config; |
6
|
|
|
use SocialiteProviders\Manager\Contracts\ConfigInterface; |
7
|
|
|
use SocialiteProviders\Manager\Exception\MissingConfigException; |
8
|
|
|
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface; |
9
|
|
|
|
10
|
|
|
class ConfigRetriever implements ConfigRetrieverInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $providerName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $providerIdentifier; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $servicesArray; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $additionalConfigKeys; |
31
|
|
|
|
32
|
|
|
/** |
33
|
2 |
|
* @param string $providerIdentifier |
34
|
|
|
* @param array $additionalConfigKeys |
35
|
2 |
|
* |
36
|
|
|
* @throws MissingConfigException |
37
|
2 |
|
* |
38
|
2 |
|
* @return ConfigInterface |
39
|
1 |
|
*/ |
40
|
1 |
|
public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) |
41
|
|
|
{ |
42
|
1 |
|
$this->providerIdentifier = $providerIdentifier; |
43
|
1 |
|
$this->additionalConfigKeys = $additionalConfigKeys; |
44
|
|
|
|
45
|
|
|
return new Config( |
46
|
|
|
$this->getFromEnv('KEY'), |
47
|
|
|
$this->getFromEnv('SECRET'), |
48
|
|
|
$this->getFromEnv('REDIRECT_URI'), |
49
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
50
|
|
|
return $this->getFromEnv(strtoupper($key)); |
51
|
|
|
})); |
52
|
|
|
} |
53
|
2 |
|
|
54
|
|
|
/** |
55
|
2 |
|
* @param string $providerName |
56
|
2 |
|
* @param array $additionalConfigKeys |
57
|
|
|
* |
58
|
1 |
|
* @throws MissingConfigException |
59
|
1 |
|
* |
60
|
1 |
|
* @return ConfigInterface |
61
|
1 |
|
*/ |
62
|
1 |
|
public function fromServices($providerName, array $additionalConfigKeys = []) |
63
|
1 |
|
{ |
64
|
1 |
|
$this->providerName = $providerName; |
65
|
|
|
$this->getConfigFromServicesArray($providerName); |
66
|
|
|
|
67
|
|
|
$this->additionalConfigKeys = $additionalConfigKeys; |
68
|
|
|
|
69
|
|
|
return new Config( |
70
|
|
|
$this->getFromServices('client_id'), |
71
|
|
|
$this->getFromServices('client_secret'), |
72
|
|
|
$this->getFromServices('redirect'), |
73
|
2 |
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
74
|
|
|
return $this->getFromServices(strtolower($key)); |
75
|
2 |
|
})); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
2 |
|
* @param array $configKeys |
80
|
|
|
* @param \Closure $keyRetrievalClosure |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) |
85
|
|
|
{ |
86
|
|
|
if (count($configKeys) < 1) { |
87
|
|
|
return []; |
88
|
2 |
|
} |
89
|
|
|
|
90
|
2 |
|
return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure); |
91
|
|
|
} |
92
|
2 |
|
|
93
|
2 |
|
/** |
94
|
2 |
|
* @param array $keys |
95
|
|
|
* @param \Closure $keyRetrievalClosure |
96
|
2 |
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) |
100
|
|
|
{ |
101
|
|
|
$out = []; |
102
|
|
|
|
103
|
|
|
foreach ($keys as $key) { |
104
|
|
|
$out[$key] = $keyRetrievalClosure($key); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
return $out; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
1 |
|
* @param string $key |
112
|
|
|
* |
113
|
|
|
* @throws MissingConfigException |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
private function getFromServices($key) |
118
|
|
|
{ |
119
|
|
|
$keyExists = array_key_exists($key, $this->servicesArray); |
120
|
2 |
|
|
121
|
|
|
// ADDITIONAL value is empty |
122
|
2 |
|
if (!$keyExists && $this->isAdditionalConfig($key)) { |
123
|
2 |
|
return; |
124
|
|
|
} |
125
|
2 |
|
|
126
|
1 |
|
// REQUIRED value is empty |
127
|
|
|
if (!$keyExists) { |
128
|
|
|
throw new MissingConfigException("Missing services entry for {$this->providerName}.$key"); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
return $this->servicesArray[$key]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
* |
137
|
|
|
* @throws MissingConfigException |
138
|
2 |
|
* |
139
|
|
|
* @return string |
140
|
2 |
|
*/ |
141
|
2 |
|
private function getFromEnv($key) |
142
|
|
|
{ |
143
|
2 |
|
$providerKey = "{$this->providerIdentifier}_{$key}"; |
144
|
1 |
|
$item = env($providerKey); |
145
|
|
|
|
146
|
1 |
|
// ADDITIONAL value is empty |
147
|
|
|
if (empty($item) && $this->isAdditionalConfig($key)) { |
148
|
1 |
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// REQUIRED value is empty |
152
|
|
|
if (empty($item)) { |
153
|
|
|
throw new MissingConfigException("Configuration for $providerKey is missing."); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $item; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $providerName |
161
|
|
|
* |
162
|
|
|
* @throws MissingConfigException |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
protected function getConfigFromServicesArray($providerName) |
167
|
|
|
{ |
168
|
|
|
/** @var array $configArray */ |
169
|
|
|
$configArray = config('services.'.$providerName); |
170
|
|
|
|
171
|
|
|
if (empty($configArray)) { |
172
|
|
|
throw new MissingConfigException("There is no services entry for $providerName"); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->servicesArray = $configArray; |
176
|
|
|
|
177
|
|
|
return $this->servicesArray; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $key |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
private function isAdditionalConfig($key) |
186
|
|
|
{ |
187
|
|
|
return in_array(strtolower($key), $this->additionalConfigKeys); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|