1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CybozuHttp; |
4
|
|
|
|
5
|
|
|
use CybozuHttp\Exception\NotExistRequiredException; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author ochi51 <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Config |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array $config |
16
|
|
|
*/ |
17
|
|
|
private $config = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array $default |
21
|
|
|
*/ |
22
|
|
|
private $default = [ |
23
|
|
|
'domain' => "cybozu.com", |
24
|
|
|
'use_api_token' => false, |
25
|
|
|
'use_basic' => false, |
26
|
|
|
'use_client_cert' => false, |
27
|
|
|
'base_url' => null, |
28
|
|
|
'defaults' => [], |
29
|
|
|
'use_cache' => false, |
30
|
|
|
'cache_dir' => null, |
31
|
|
|
'cache_ttl' => 0, |
32
|
|
|
'debug' => false |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array $required |
37
|
|
|
*/ |
38
|
|
|
private $required = [ |
39
|
|
|
'domain', |
40
|
|
|
'subdomain', |
41
|
|
|
'use_api_token', |
42
|
|
|
'use_basic', |
43
|
|
|
'use_client_cert', |
44
|
|
|
'base_url', |
45
|
|
|
'defaults', |
46
|
|
|
'use_cache', |
47
|
|
|
'debug' |
48
|
|
|
]; |
49
|
|
|
|
50
|
20 |
|
public function __construct(array $config) |
51
|
|
|
{ |
52
|
20 |
|
$this->config = $config + $this->default; |
53
|
|
|
|
54
|
20 |
|
$this->config['base_url'] = $this->getBaseUrl(); |
55
|
|
|
|
56
|
20 |
|
$this->configureDefaults(); |
57
|
20 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* configure default options |
61
|
|
|
*/ |
62
|
20 |
|
private function configureDefaults() |
63
|
|
|
{ |
64
|
20 |
|
$this->configureAuth(); |
65
|
20 |
|
$this->configureBasicAuth(); |
66
|
20 |
|
$this->configureCert(); |
67
|
20 |
|
} |
68
|
|
|
|
69
|
20 |
|
private function configureAuth() |
70
|
|
|
{ |
71
|
20 |
|
if ($this->get('use_api_token')) { |
72
|
3 |
|
$this->config['defaults']['headers']['X-Cybozu-API-Token'] = $this->get('token'); |
73
|
3 |
|
} else { |
74
|
20 |
|
$this->config['defaults']['headers']['X-Cybozu-Authorization'] = |
75
|
20 |
|
base64_encode($this->get('login') . ':' . $this->get('password')); |
76
|
|
|
} |
77
|
20 |
|
} |
78
|
|
|
|
79
|
20 |
|
private function configureBasicAuth() |
80
|
|
|
{ |
81
|
20 |
|
if ($this->get('use_basic')) { |
82
|
7 |
|
$this->config['defaults']['auth'] = $this->getBasicAuthOptions(); |
83
|
7 |
|
} |
84
|
20 |
|
} |
85
|
|
|
|
86
|
20 |
|
private function configureCert() |
87
|
|
|
{ |
88
|
20 |
|
if ($this->get('use_client_cert')) { |
89
|
6 |
|
$this->config['defaults']['verify'] = true; |
90
|
6 |
|
$this->config['defaults']['cert'] = $this->getCertOptions(); |
91
|
6 |
|
} else { |
92
|
16 |
|
$this->config['defaults']['verify'] = false; |
93
|
|
|
} |
94
|
20 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
7 |
|
private function getBasicAuthOptions() |
100
|
|
|
{ |
101
|
7 |
|
if ($this->hasRequiredOnBasicAuth()) { |
102
|
|
|
return [ |
103
|
7 |
|
$this->get('basic_login'), |
104
|
7 |
|
$this->get('basic_password') |
105
|
7 |
|
]; |
106
|
|
|
} |
107
|
1 |
|
throw new NotExistRequiredException("kintone.empty_basic_password"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
6 |
|
private function getCertOptions() |
114
|
|
|
{ |
115
|
6 |
|
if ($this->hasRequiredOnCert()) { |
116
|
|
|
return [ |
117
|
6 |
|
$this->get('cert_file'), |
118
|
6 |
|
$this->get('cert_password') |
119
|
6 |
|
]; |
120
|
|
|
} |
121
|
1 |
|
throw new NotExistRequiredException("kintone.empty_cert"); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
9 |
|
public function toArray() |
128
|
|
|
{ |
129
|
9 |
|
return $this->config; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $key |
134
|
|
|
* @return string|bool |
135
|
|
|
*/ |
136
|
20 |
|
public function get($key) |
137
|
|
|
{ |
138
|
20 |
|
if (isset($this->config[$key])) { |
139
|
20 |
|
return $this->config[$key]; |
140
|
|
|
} |
141
|
|
|
|
142
|
5 |
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
9 |
|
public function hasRequired() |
149
|
|
|
{ |
150
|
9 |
|
foreach ($this->required as $r) { |
151
|
9 |
|
if (!array_key_exists($r, $this->config)) { |
152
|
1 |
|
return false; |
153
|
|
|
} |
154
|
9 |
|
} |
155
|
|
|
|
156
|
9 |
|
return $this->hasRequiredOnAuth() |
157
|
9 |
|
&& $this->hasRequiredOnBasicAuth() |
158
|
9 |
|
&& $this->hasRequiredOnCert() |
159
|
9 |
|
&& $this->hasRequiredOnCache(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
9 |
|
private function hasRequiredOnAuth() |
166
|
|
|
{ |
167
|
9 |
|
if ($this->get('use_api_token')) { |
168
|
1 |
|
return !empty($this->get('token')); |
169
|
|
|
} |
170
|
|
|
|
171
|
9 |
|
return $this->get('login') && $this->get('password'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
11 |
|
private function hasRequiredOnBasicAuth() |
178
|
|
|
{ |
179
|
11 |
|
return $this->hasKeysByUse('use_basic', ['basic_login', 'basic_password']); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
12 |
|
private function hasRequiredOnCert() |
186
|
|
|
{ |
187
|
12 |
|
return $this->hasKeysByUse('use_client_cert', ['cert_file', 'cert_password']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
9 |
|
private function hasRequiredOnCache() |
194
|
|
|
{ |
195
|
9 |
|
return $this->hasKeysByUse('use_cache', ['cache_dir', 'cache_ttl']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $use |
200
|
|
|
* @param string[] $keys |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
13 |
|
private function hasKeysByUse($use, array $keys) |
204
|
|
|
{ |
205
|
13 |
|
if (!$this->get($use)) { |
206
|
8 |
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
9 |
|
foreach ($keys as $key) { |
210
|
9 |
|
if (is_null($this->get($key)) || $this->get($key) === false) { |
211
|
2 |
|
return false; |
212
|
|
|
} |
213
|
9 |
|
} |
214
|
|
|
|
215
|
9 |
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
20 |
|
public function getBaseUrl() |
222
|
|
|
{ |
223
|
20 |
|
$subdomain = $this->get('subdomain'); |
224
|
20 |
|
$uri = "https://" . $subdomain; |
225
|
|
|
|
226
|
20 |
|
if (strpos($subdomain, '.') === false) { |
227
|
20 |
|
if ($this->get('use_client_cert')) { |
228
|
6 |
|
$uri .= ".s"; |
229
|
6 |
|
} |
230
|
|
|
|
231
|
20 |
|
$uri .= "." . $this->get('domain'); |
232
|
20 |
|
} |
233
|
|
|
|
234
|
20 |
|
return $uri; |
235
|
|
|
} |
236
|
|
|
} |