|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace CCT\Kong; |
|
6
|
|
|
|
|
7
|
|
|
use Assert\Assert; |
|
8
|
|
|
use CCT\Kong\EventListener\DeserializeExtraFieldsSubscriber; |
|
9
|
|
|
use CCT\Kong\Exception\InvalidParameterException; |
|
10
|
|
|
use CCT\Kong\Helper\ObjectConstructor; |
|
11
|
|
|
use CCT\Kong\Http\Request\ApiRequest; |
|
12
|
|
|
use CCT\Kong\Http\Request\ConsumerRequest; |
|
13
|
|
|
use CCT\Kong\Http\Request\CertificateRequest; |
|
14
|
|
|
use CCT\Kong\Http\Request\KongRequest; |
|
15
|
|
|
use CCT\Kong\Http\Request\PluginRequest; |
|
16
|
|
|
use CCT\Kong\Http\Request\SNIRequest; |
|
17
|
|
|
use CCT\Kong\Http\Request\StatusRequest; |
|
18
|
|
|
use CCT\Kong\Http\RequestInterface; |
|
19
|
|
|
use CCT\Kong\Model\Api; |
|
20
|
|
|
use CCT\Kong\Model\Consumer; |
|
21
|
|
|
use CCT\Kong\Model\Info; |
|
|
|
|
|
|
22
|
|
|
use CCT\Kong\Model\Kong; |
|
23
|
|
|
use CCT\Kong\Model\Plugin; |
|
24
|
|
|
use CCT\Kong\Model\SNI; |
|
25
|
|
|
use CCT\Kong\Model\Status; |
|
26
|
|
|
use CCT\Kong\Serializer\Handler\BooleanToStringHandler; |
|
27
|
|
|
use CCT\Kong\Serializer\Handler\TimestampDatetimeHandler; |
|
28
|
|
|
use CCT\Kong\Serializer\SerializerBuilder; |
|
29
|
|
|
use CCT\Kong\Transformer\Response\CollectionObjectTransformer; |
|
30
|
|
|
use CCT\Kong\Transformer\Response\ObjectTransformer; |
|
31
|
|
|
use CCT\Kong\Model\Certificate; |
|
32
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
33
|
|
|
use JMS\Serializer\SerializerInterface; |
|
34
|
|
|
|
|
35
|
|
|
class Client |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var GuzzleClient |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $client; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Config |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $config; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $defaultConfig = true; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var SerializerInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
protected static $serializer; |
|
56
|
|
|
|
|
57
|
7 |
|
public function __construct(Config $config, bool $defaultConfig = true) |
|
58
|
|
|
{ |
|
59
|
7 |
|
Assert::that($config->all())->keyExists(Config::ENDPOINT); |
|
60
|
|
|
|
|
61
|
7 |
|
$this->defaultConfig = $defaultConfig; |
|
62
|
7 |
|
$this->config = $config; |
|
63
|
7 |
|
$this->client = new GuzzleClient([ |
|
64
|
7 |
|
'base_uri' => $config->get(Config::ENDPOINT) |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
7 |
|
if ($defaultConfig) { |
|
68
|
7 |
|
$this->applyDefaults(); |
|
69
|
|
|
} |
|
70
|
7 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return KongRequest |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function kong(): KongRequest |
|
76
|
|
|
{ |
|
77
|
1 |
|
$config = clone $this->config; |
|
78
|
1 |
|
$modelClass = $config->get('kong.model.class', Kong::class); |
|
79
|
|
|
|
|
80
|
1 |
|
if ($this->isDefaultConfig()) { |
|
81
|
1 |
|
$config->set(Config::OBJECT_CONSTRUCTOR, new ObjectConstructor()); |
|
82
|
1 |
|
$config->set(Config::EVENT_SUBSCRIBERS, [ |
|
83
|
1 |
|
new DeserializeExtraFieldsSubscriber([ |
|
84
|
1 |
|
$modelClass => [ |
|
85
|
|
|
'tagline', |
|
86
|
|
|
'lua_version', |
|
87
|
|
|
'hostname', |
|
88
|
|
|
'version', |
|
89
|
|
|
] |
|
90
|
|
|
]), |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$serializer = $this->buildSerializer($config); |
|
95
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
96
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $this->createRequestInstance(KongRequest::class, $serializer, $config); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return StatusRequest |
|
104
|
|
|
*/ |
|
105
|
|
|
public function status(): StatusRequest |
|
106
|
|
|
{ |
|
107
|
|
|
$config = clone $this->config; |
|
108
|
|
|
$modelClass = $config->get('status.model.class', Status::class); |
|
109
|
|
|
|
|
110
|
|
|
$serializer = $this->getBuiltSerializer($config); |
|
111
|
|
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
112
|
|
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->createRequestInstance(StatusRequest::class, $serializer, $config); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return ApiRequest |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function apis(): ApiRequest |
|
122
|
|
|
{ |
|
123
|
1 |
|
$config = clone $this->config; |
|
124
|
1 |
|
$modelClass = $config->get('api.model.class', Api::class); |
|
125
|
|
|
|
|
126
|
1 |
|
$serializer = $this->getBuiltSerializer($config); |
|
127
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
128
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
return $this->createRequestInstance(ApiRequest::class, $serializer, $config); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return ConsumerRequest |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function consumers(): ConsumerRequest |
|
138
|
|
|
{ |
|
139
|
1 |
|
$config = clone $this->config; |
|
140
|
1 |
|
$modelClass = $config->get('consumer.model.class', Consumer::class); |
|
141
|
|
|
|
|
142
|
1 |
|
$serializer = $this->getBuiltSerializer($config); |
|
143
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
144
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
return $this->createRequestInstance(ConsumerRequest::class, $serializer, $config); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return PluginRequest |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function plugins(): PluginRequest |
|
154
|
|
|
{ |
|
155
|
1 |
|
$config = clone $this->config; |
|
156
|
1 |
|
$modelClass = $config->get('plugin.model.class', Plugin::class); |
|
157
|
|
|
|
|
158
|
1 |
|
$serializer = $this->getBuiltSerializer($config); |
|
159
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
160
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
return $this->createRequestInstance(PluginRequest::class, $serializer, $config); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return CertificateRequest |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function certificates(): CertificateRequest |
|
170
|
|
|
{ |
|
171
|
1 |
|
$config = clone $this->config; |
|
172
|
1 |
|
$modelClass = $config->get('certificate.model.class', Certificate::class); |
|
173
|
|
|
|
|
174
|
1 |
|
$serializer = $this->getBuiltSerializer($config); |
|
175
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
176
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
return $this->createRequestInstance(CertificateRequest::class, $serializer, $config); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return SNIRequest |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function snis(): SNIRequest |
|
186
|
|
|
{ |
|
187
|
1 |
|
$config = clone $this->config; |
|
188
|
1 |
|
$modelClass = $config->get('sni.model.class', SNI::class); |
|
189
|
|
|
|
|
190
|
1 |
|
$serializer = $this->getBuiltSerializer($config); |
|
191
|
1 |
|
if ($this->shouldUseDefaultResponseTransformers()) { |
|
192
|
1 |
|
$this->applyDefaultResponseTransformers($config, $serializer, $modelClass); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
1 |
|
return $this->createRequestInstance(SNIRequest::class, $serializer, $config); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function enableDefaultConfig() |
|
199
|
|
|
{ |
|
200
|
|
|
$this->defaultConfig = true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function disableDefaultConfig() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->defaultConfig = false; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
public function isDefaultConfig(): bool |
|
209
|
|
|
{ |
|
210
|
1 |
|
return $this->defaultConfig; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
7 |
|
protected function applyDefaults() |
|
214
|
|
|
{ |
|
215
|
7 |
|
$this->config->set(Config::METADATA_DIRS, [ |
|
216
|
|
|
[ |
|
217
|
7 |
|
'dir' => __DIR__ . '/Resources/metadata', |
|
218
|
|
|
'namespacePrefix' => 'CCT\\Kong\\Model', |
|
219
|
|
|
] |
|
220
|
|
|
]); |
|
221
|
|
|
|
|
222
|
7 |
|
$this->config->set(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS, true); |
|
223
|
7 |
|
$this->config->set(Config::SERIALIZATION_HANDLERS, [ |
|
224
|
7 |
|
new BooleanToStringHandler(), |
|
225
|
7 |
|
new TimestampDatetimeHandler() |
|
226
|
|
|
]); |
|
227
|
7 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function clearDefaults() |
|
230
|
|
|
{ |
|
231
|
|
|
$this->config->remove(Config::METADATA_DIRS); |
|
232
|
|
|
$this->config->remove(Config::DEBUG); |
|
233
|
|
|
$this->config->remove(Config::EVENT_SUBSCRIBERS); |
|
234
|
|
|
$this->config->remove(Config::SERIALIZATION_HANDLERS); |
|
235
|
|
|
$this->config->remove(Config::OBJECT_CONSTRUCTOR); |
|
236
|
|
|
$this->config->remove(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
2 |
|
protected function buildSerializer(Config $config) |
|
240
|
|
|
{ |
|
241
|
2 |
|
return SerializerBuilder::createByConfig($config) |
|
242
|
2 |
|
->configureDefaults() |
|
243
|
2 |
|
->build() |
|
244
|
|
|
; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
5 |
|
protected function getBuiltSerializer(Config $config) |
|
248
|
|
|
{ |
|
249
|
5 |
|
if (null === static::$serializer) { |
|
250
|
1 |
|
static::$serializer = $this->buildSerializer($config); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
5 |
|
return static::$serializer; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
6 |
|
protected function createRequestInstance($class, SerializerInterface $serializer, Config $config) |
|
257
|
|
|
{ |
|
258
|
6 |
|
$reflectionClass = new \ReflectionClass($class); |
|
259
|
|
|
|
|
260
|
6 |
|
if (!$reflectionClass->implementsInterface(RequestInterface::class)) { |
|
261
|
|
|
throw new InvalidParameterException(sprintf( |
|
262
|
|
|
'The class must be an instance of %s', |
|
263
|
|
|
RequestInterface::class |
|
264
|
|
|
)); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
6 |
|
return $reflectionClass->newInstance( |
|
268
|
6 |
|
$this->client, |
|
269
|
6 |
|
$serializer, |
|
270
|
6 |
|
$config |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Should use the default response transformers? |
|
276
|
|
|
* |
|
277
|
|
|
* @return bool |
|
278
|
|
|
*/ |
|
279
|
6 |
|
protected function shouldUseDefaultResponseTransformers(): bool |
|
280
|
|
|
{ |
|
281
|
6 |
|
return (bool) $this->config->get(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS, true); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
6 |
|
protected function applyDefaultResponseTransformers(Config $config, SerializerInterface $serializer, $modelClass) |
|
285
|
|
|
{ |
|
286
|
6 |
|
$config->set(Config::RESPONSE_TRANSFORMERS, [ |
|
287
|
6 |
|
new ObjectTransformer($serializer, $modelClass), |
|
288
|
6 |
|
new CollectionObjectTransformer($serializer, $modelClass) |
|
289
|
|
|
]); |
|
290
|
6 |
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths