1
|
|
|
<?php |
2
|
|
|
namespace SocialiteProviders\Manager\Helpers; |
3
|
|
|
|
4
|
|
|
use SocialiteProviders\Manager\Config; |
5
|
|
|
use SocialiteProviders\Manager\Contracts\ConfigInterface; |
6
|
|
|
use SocialiteProviders\Manager\Exception\MissingConfigException; |
7
|
|
|
|
8
|
|
|
class ConfigRetriever implements \SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $providerName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $providerIdentifier; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $servicesArray; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $providerIdentifier |
28
|
|
|
* @param array $additionalConfigKeys |
29
|
|
|
* |
30
|
|
|
* @return ConfigInterface |
31
|
|
|
* @throws MissingConfigException |
32
|
|
|
*/ |
33
|
2 |
|
public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) |
34
|
|
|
{ |
35
|
2 |
|
$this->providerIdentifier = $providerIdentifier; |
36
|
|
|
|
37
|
2 |
|
return new Config( |
38
|
2 |
|
$this->getFromEnv("KEY"), |
39
|
1 |
|
$this->getFromEnv("SECRET"), |
40
|
1 |
|
$this->getFromEnv("REDIRECT_URI"), |
41
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
42
|
1 |
|
return $this->getFromEnv(strtoupper($key)); |
43
|
1 |
|
})); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $providerName |
48
|
|
|
* @param array $additionalConfigKeys |
49
|
|
|
* |
50
|
|
|
* @return ConfigInterface |
51
|
|
|
* @throws MissingConfigException |
52
|
|
|
*/ |
53
|
2 |
|
public function fromServices($providerName, array $additionalConfigKeys = []) |
54
|
|
|
{ |
55
|
2 |
|
$this->providerName = $providerName; |
56
|
2 |
|
$this->getConfigFromServicesArray($providerName); |
57
|
|
|
|
58
|
1 |
|
return new Config( |
59
|
1 |
|
$this->getFromServices('client_id'), |
60
|
1 |
|
$this->getFromServices('client_secret'), |
61
|
1 |
|
$this->getFromServices('redirect'), |
62
|
1 |
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
63
|
1 |
|
return $this->getFromServices(strtolower($key)); |
64
|
1 |
|
})); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $configKeys |
69
|
|
|
* @param \Closure $keyRetrievalClosure |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
2 |
|
private function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) |
74
|
|
|
{ |
75
|
2 |
|
if (count($configKeys) < 1) { |
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $keys |
84
|
|
|
* @param \Closure $keyRetrievalClosure |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
2 |
|
private function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) |
89
|
|
|
{ |
90
|
2 |
|
$out = []; |
91
|
|
|
|
92
|
2 |
|
foreach ($keys as $key) { |
93
|
2 |
|
$out[$key] = $keyRetrievalClosure($key); |
94
|
2 |
|
} |
95
|
|
|
|
96
|
2 |
|
return $out; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $key |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* @throws MissingConfigException |
104
|
|
|
*/ |
105
|
1 |
|
private function getFromServices($key) |
106
|
|
|
{ |
107
|
1 |
|
if (!array_key_exists($key, $this->servicesArray)) { |
108
|
|
|
throw new MissingConfigException("Missing services entry for {$this->providerName}.$key"); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $this->servicesArray[$key]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $key |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
* @throws MissingConfigException |
119
|
|
|
*/ |
120
|
2 |
|
private function getFromEnv($key) |
121
|
|
|
{ |
122
|
2 |
|
$providerKey = "{$this->providerIdentifier}_{$key}"; |
123
|
2 |
|
$item = env($providerKey); |
124
|
|
|
|
125
|
2 |
|
if (empty($item)) { |
126
|
1 |
|
throw new MissingConfigException("Configuration for $providerKey is missing."); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $item; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $providerName |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
* @throws MissingConfigException |
137
|
|
|
*/ |
138
|
2 |
|
protected function getConfigFromServicesArray($providerName) |
139
|
|
|
{ |
140
|
2 |
|
if (!$this->servicesArray) { |
|
|
|
|
141
|
2 |
|
$this->servicesArray = config('services.'.$providerName); |
|
|
|
|
142
|
|
|
|
143
|
2 |
|
if (empty($this->servicesArray)) { |
144
|
1 |
|
throw new MissingConfigException("There is no services entry for $providerName"); |
145
|
|
|
} |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
return $this->servicesArray; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.