-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConfig.php
285 lines (256 loc) · 7.49 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php declare(strict_types=1);
namespace MyENA\RGW;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Class Config
*
* @package MyENA\RGW
*/
class Config implements LoggerAwareInterface
{
use LoggerAwareTrait;
const DEFAULT_CLIENT_TIMEOUT = 10;
const DEFAULT_SKIP_SSL_VERIFY = false;
const FIELD_CLIENT_TIMEOUT = 'clientTimeout';
const FIELD_ADDRESS = 'address';
const FIELD_ADMIN_PATH = 'adminPath';
const FIELD_API_KEY = 'apiKey';
const FIELD_API_SECRET = 'apiSecret';
const FIELD_NO_SSL = 'noSSL';
const FIELD_HTTP_CLIENT = 'httpClient';
const FIELD_SILENT = 'silent';
/** @var int */
private $clientTimeout = self::DEFAULT_CLIENT_TIMEOUT;
/** @var string */
private $address = '';
/** @var string */
private $adminPath = '';
/** @var string */
private $apiKey = '';
/** @var string */
private $apiSecret = '';
/** @var bool */
private $noSSL = false;
/** @var \GuzzleHttp\ClientInterface */
private $httpClient;
/** @var bool */
private $silent = false;
/**
* Config constructor.
*
* @param array $config
* @param \GuzzleHttp\ClientInterface|null $httpClient
* @param \Psr\Log\LoggerInterface|null $logger
*/
public function __construct(array $config = [], ClientInterface $httpClient = null, LoggerInterface $logger = null)
{
if (null === $logger) {
$logger = new NullLogger();
}
$this->setLogger($logger);
// add env values
$config += self::getConfigFromEnvironment();
// parse conf
foreach ($config as $rawK => $v) {
$k = sanitizeName($rawK, '_', true);
if (self::FIELD_CLIENT_TIMEOUT === $k) {
$this->setClientTimeout($config[self::FIELD_CLIENT_TIMEOUT]);
} elseif (self::FIELD_ADDRESS === $k) {
$this->setAddress($config[self::FIELD_ADDRESS]);
} elseif (self::FIELD_ADMIN_PATH === $k) {
$this->setAdminPath($config[self::FIELD_ADMIN_PATH]);
} elseif (self::FIELD_API_KEY === $k) {
$this->setApiKey($config[self::FIELD_API_KEY]);
} elseif (self::FIELD_API_SECRET === $k) {
$this->setApiSecret($config[self::FIELD_API_SECRET]);
} elseif (self::FIELD_NO_SSL === $k) {
$this->setNoSSL($config[self::FIELD_NO_SSL]);
} elseif (self::FIELD_HTTP_CLIENT === $k) {
$httpClient = $config[self::FIELD_HTTP_CLIENT];
} elseif (self::FIELD_SILENT === $k) {
$this->setSilent($config[self::FIELD_SILENT]);
} else {
throw new \OutOfBoundsException(sprintf(
'Unknown configuration key "%s" seen.',
$rawK
));
}
}
if (null === $httpClient) {
$httpClient = new GuzzleClient();
}
$this->httpClient = $httpClient;
if (!($this->httpClient instanceof ClientInterface)) {
throw new \InvalidArgumentException(sprintf('httpClient must implement %s', ClientInterface::class));
}
}
/**
* @return array
*/
public static function getEnvironmentConfig(): array
{
$ret = [];
foreach ([
ENV_RGW_API_HTTP_ADDR => tryGetEnvParam(ENV_RGW_API_HTTP_ADDR, '127.0.0.1'),
ENV_RGW_API_NO_SSL => tryGetBoolEnvParam(ENV_RGW_API_NO_SSL, '0'),
ENV_RGW_API_ADMIN_PATH => tryGetEnvParam(ENV_RGW_API_ADMIN_PATH, 'admin'),
ENV_RGW_API_KEY => tryGetEnvParam(ENV_RGW_API_KEY),
ENV_RGW_API_SECRET => tryGetEnvParam(ENV_RGW_API_SECRET),
ENV_RGW_LOG_SILENT => tryGetBoolEnvParam(ENV_RGW_LOG_SILENT, '0'),
] as $k => $v) {
if ('' !== $v) {
$ret[$k] = $v;
}
}
return $ret;
}
/**
* @return array
*/
public static function getConfigFromEnvironment(): array
{
$conf = [];
foreach (self::getEnvironmentConfig() as $k => $v) {
if (ENV_RGW_API_HTTP_ADDR === $k) {
$conf[self::FIELD_ADDRESS] = $v;
} elseif (ENV_RGW_API_NO_SSL === $k) {
$conf[self::FIELD_NO_SSL] = $v;
} elseif (ENV_RGW_API_ADMIN_PATH === $k) {
$conf[self::FIELD_ADMIN_PATH] = $v;
} elseif (ENV_RGW_API_KEY === $k) {
$conf[self::FIELD_API_KEY] = $v;
} elseif (ENV_RGW_API_SECRET === $k) {
$conf[self::FIELD_API_SECRET] = $v;
} elseif (ENV_RGW_LOG_SILENT === $k) {
$conf[self::FIELD_SILENT] = $v;
}
}
return $conf;
}
/**
* @param \GuzzleHttp\Client|null $client
* @param null|\Psr\Log\LoggerInterface $logger
* @return \MyENA\RGW\Config
*/
public static function defaultConfig(?GuzzleClient $client = null, ?LoggerInterface $logger = null): Config
{
return new static(self::getConfigFromEnvironment(), $client, $logger);
}
/**
* @return int
*/
public function getClientTimeout(): int
{
return $this->clientTimeout;
}
/**
* @param int $clientTimeout
*/
public function setClientTimeout(int $clientTimeout): void
{
$this->clientTimeout = $clientTimeout;
}
/**
* @return string
*/
public function getAddress(): string
{
return $this->address;
}
/**
* @param string $address
*/
public function setAddress(string $address): void
{
$this->address = trim($address, " \t\n\r\0\x0B/");
}
/**
* @return string
*/
public function getAdminPath(): string
{
return $this->adminPath;
}
/**
* @param string $adminPath
*/
public function setAdminPath(string $adminPath): void
{
$this->adminPath = trim($adminPath, " \t\n\r\0\x0B/");
}
/**
* @return string
*/
public function getApiKey(): string
{
return $this->apiKey;
}
/**
* @param string $apiKey
*/
public function setApiKey(string $apiKey): void
{
$this->apiKey = $apiKey;
}
/**
* @return string
*/
public function getApiSecret(): string
{
return $this->apiSecret;
}
/**
* @param string $apiSecret
*/
public function setApiSecret(string $apiSecret): void
{
$this->apiSecret = $apiSecret;
}
/**
* @return bool
*/
public function isNoSSL(): bool
{
return $this->noSSL;
}
/**
* @param bool $noSSL
*/
public function setNoSSL(bool $noSSL): void
{
$this->noSSL = $noSSL;
}
/**
* @return \GuzzleHttp\ClientInterface
*/
public function getHttpClient(): ClientInterface
{
return $this->httpClient;
}
/**
* @return bool
*/
public function isSilent(): bool
{
return $this->silent;
}
/**
* @param bool $silent
*/
public function setSilent(bool $silent): void
{
$this->silent = $silent;
}
/**
* @return \Psr\Log\LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}