1 | <?php |
||
12 | class Config |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var array $config |
||
17 | */ |
||
18 | private $config; |
||
19 | |||
20 | /** |
||
21 | * @var array $default |
||
22 | */ |
||
23 | private static $default = [ |
||
24 | 'domain' => 'cybozu.com', |
||
25 | 'use_api_token' => false, |
||
26 | 'use_basic' => false, |
||
27 | 'use_client_cert' => false, |
||
28 | 'base_uri' => null, |
||
29 | 'concurrency' => 1, |
||
30 | 'response_middleware' => true, |
||
31 | 'debug' => false |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @var array $required |
||
36 | */ |
||
37 | private static $required = [ |
||
38 | 'handler', |
||
39 | 'domain', |
||
40 | 'subdomain', |
||
41 | 'use_api_token', |
||
42 | 'use_basic', |
||
43 | 'use_client_cert', |
||
44 | 'base_uri', |
||
45 | 'debug' |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Config constructor. |
||
50 | 20 | * @param array $config |
|
51 | */ |
||
52 | 20 | public function __construct(array $config) |
|
53 | { |
||
54 | 20 | $this->config = array_merge(self::$default, $config); |
|
55 | |||
56 | 20 | $this->config['base_uri'] = $this->getBaseUri(); |
|
57 | 20 | $this->config['handler'] = $handler = HandlerStack::create(); |
|
58 | |||
59 | $this->configureAuth(); |
||
60 | $this->configureBasicAuth(); |
||
61 | $this->configureCert(); |
||
62 | 20 | ||
63 | if ($this->config['response_middleware']) { |
||
64 | 20 | $handler->before('http_errors', new FinishMiddleware(), 'cybozu_http.finish'); |
|
65 | 20 | } |
|
66 | 20 | } |
|
67 | 20 | ||
68 | |||
69 | 20 | private function configureAuth(): void |
|
78 | |||
79 | 20 | private function configureBasicAuth(): void |
|
85 | |||
86 | 20 | private function configureCert(): void |
|
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | * @throws NotExistRequiredException |
||
99 | 7 | */ |
|
100 | private function getBasicAuthOptions(): array |
||
110 | |||
111 | /** |
||
112 | * @return array |
||
113 | 6 | * @throws NotExistRequiredException |
|
114 | */ |
||
115 | 6 | private function getCertOptions(): array |
|
125 | |||
126 | /** |
||
127 | 9 | * @return array |
|
128 | */ |
||
129 | 9 | public function toGuzzleConfig(): array |
|
130 | { |
||
131 | $config = [ |
||
132 | 'handler' => $this->get('handler'), |
||
133 | 'base_uri' => $this->get('base_uri'), |
||
134 | 'headers' => $this->get('headers'), |
||
135 | 'debug' => $this->get('debug') ? fopen($this->get('logfile'), 'ab') : false, |
||
136 | 20 | 'concurrency' => $this->get('concurrency') |
|
137 | ]; |
||
138 | 20 | if ($this->get('auth')) { |
|
139 | 20 | $config['auth'] = $this->get('auth'); |
|
140 | } |
||
141 | $config['verify'] = $this->get('verify'); |
||
142 | 5 | if ($this->get('cert')) { |
|
143 | $config['cert'] = $this->get('cert'); |
||
144 | } |
||
145 | |||
146 | return $config; |
||
147 | } |
||
148 | 9 | ||
149 | /** |
||
150 | 9 | * @param $key |
|
151 | 9 | * @return string|bool |
|
152 | 1 | */ |
|
153 | public function get($key) |
||
157 | 9 | ||
158 | 9 | /** |
|
159 | 9 | * @return array |
|
160 | */ |
||
161 | public function getConfig(): array |
||
162 | { |
||
163 | return $this->config; |
||
164 | } |
||
165 | 9 | ||
166 | /** |
||
167 | 9 | * @return bool |
|
168 | 1 | */ |
|
169 | public function hasRequired(): bool |
||
170 | { |
||
171 | 9 | foreach (self::$required as $r) { |
|
172 | if (!array_key_exists($r, $this->config)) { |
||
173 | return false; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 11 | return $this->hasRequiredOnAuth() |
|
178 | && $this->hasRequiredOnBasicAuth() |
||
179 | 11 | && $this->hasRequiredOnCert(); |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | 12 | private function hasRequiredOnAuth(): bool |
|
186 | { |
||
187 | 12 | if ($this->get('use_api_token')) { |
|
188 | return !empty($this->get('token')); |
||
189 | } |
||
190 | |||
191 | return $this->get('login') && $this->get('password'); |
||
192 | } |
||
193 | 9 | ||
194 | /** |
||
195 | 9 | * @return bool |
|
196 | */ |
||
197 | private function hasRequiredOnBasicAuth(): bool |
||
198 | { |
||
199 | return $this->hasKeysByUse('use_basic', ['basic_login', 'basic_password']); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | 13 | * @return bool |
|
204 | */ |
||
205 | 13 | private function hasRequiredOnCert(): bool |
|
209 | 9 | ||
210 | 9 | /** |
|
211 | 2 | * @param string $use |
|
212 | * @param string[] $keys |
||
213 | 9 | * @return bool |
|
214 | */ |
||
215 | 9 | private function hasKeysByUse($use, array $keys): bool |
|
229 | 6 | ||
230 | /** |
||
231 | 20 | * @return string |
|
232 | 20 | */ |
|
233 | public function getBaseUri(): string |
||
234 | 20 | { |
|
235 | $subdomain = $this->get('subdomain'); |
||
248 | } |
||
249 |