|
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
|
1 |
|
public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->providerIdentifier = $providerIdentifier; |
|
36
|
|
|
|
|
37
|
1 |
|
return new Config( |
|
38
|
1 |
|
$this->getFromEnv("KEY"), |
|
39
|
1 |
|
$this->getFromEnv("SECRET"), |
|
40
|
1 |
|
$this->getFromEnv("REDIRECT_URI"), |
|
41
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
|
42
|
|
|
return $this->getFromEnv($key); |
|
43
|
1 |
|
})); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $providerName |
|
48
|
|
|
* @param array $additionalConfigKeys |
|
49
|
|
|
* |
|
50
|
|
|
* @return ConfigInterface |
|
51
|
|
|
* @throws MissingConfigException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function fromServices($providerName, array $additionalConfigKeys = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->providerName = $providerName; |
|
56
|
|
|
|
|
57
|
|
|
return new Config( |
|
58
|
|
|
$this->getFromServices('client_id'), |
|
59
|
|
|
$this->getFromServices('client_secret'), |
|
60
|
|
|
$this->getFromServices('redirect'), |
|
61
|
|
|
$this->getConfigItems($additionalConfigKeys, function ($key) { |
|
62
|
|
|
return $this->getFromServices($key); |
|
63
|
|
|
})); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $configKeys |
|
68
|
|
|
* @param \Closure $keyRetrievalClosure |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
1 |
|
private function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure) |
|
73
|
|
|
{ |
|
74
|
1 |
|
if (count($configKeys) < 1) { |
|
75
|
1 |
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $keys |
|
83
|
|
|
* @param \Closure $keyRetrievalClosure |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
private function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure) |
|
88
|
|
|
{ |
|
89
|
|
|
$out = []; |
|
90
|
|
|
|
|
91
|
|
|
foreach ($keys as $key) { |
|
92
|
|
|
$out[$key] = $keyRetrievalClosure($key); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $out; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getFromServices($key) |
|
99
|
|
|
{ |
|
100
|
|
|
if (!array_key_exists($key, $this->servicesArray)) { |
|
101
|
|
|
throw new MissingConfigException("Missing services entry for {$this->providerName}.$key"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->servicesArray[$key]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $key |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
* @throws MissingConfigException |
|
112
|
|
|
*/ |
|
113
|
1 |
|
private function getFromEnv($key) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$providerKey = "{$this->providerIdentifier}_{$key}"; |
|
116
|
1 |
|
$item = env($providerKey); |
|
117
|
|
|
|
|
118
|
1 |
|
if (empty($item)) { |
|
119
|
|
|
throw new MissingConfigException("Configuration for $providerKey is missing."); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
return $item; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $providerName |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
* @throws MissingConfigException |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getConfigFromServicesArray($providerName) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->servicesArray) { |
|
|
|
|
|
|
134
|
|
|
$providerArray = app()->offsetGet('config')['services.'.$providerName]; |
|
135
|
|
|
|
|
136
|
|
|
if (empty($providerArray)) { |
|
137
|
|
|
throw new MissingConfigException("There is no services entry for $providerName"); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->servicesArray; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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.