PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Api / Service.php

Service.php in Media Cloud Sync 1.4.1, at includes/sdk/s3/Aws/Api/Service.php

441 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Api;
4
5 /**
6 * Represents a web service API model.
7 */
8 class Service extends AbstractModel
9 {
10 /** @var callable */
11 private $apiProvider;
12 /** @var string */
13 private $serviceName;
14 /** @var string */
15 private $apiVersion;
16 /** @var array */
17 private $clientContextParams = [];
18 /** @var Operation[] */
19 private $operations = [];
20 /** @var array */
21 private $paginators = null;
22 /** @var array */
23 private $waiters = null;
24 /** @var boolean */
25 private $modifiedModel = \false;
26 /** @var string */
27 private $protocol;
28 /**
29 * @param array $definition
30 * @param callable $provider
31 *
32 * @internal param array $definition Service description
33 */
34 public function __construct(array $definition, callable $provider)
35 {
36 static $defaults = ['operations' => [], 'shapes' => [], 'metadata' => [], 'clientContextParams' => []], $defaultMeta = ['apiVersion' => null, 'serviceFullName' => null, 'serviceId' => null, 'endpointPrefix' => null, 'signingName' => null, 'signatureVersion' => null, 'protocol' => null, 'uid' => null];
37 $definition += $defaults;
38 $definition['metadata'] += $defaultMeta;
39 $this->definition = $definition;
40 $this->apiProvider = $provider;
41 parent::__construct($definition, new ShapeMap($definition['shapes']));
42 if (isset($definition['metadata']['serviceIdentifier'])) {
43 $this->serviceName = $this->getServiceName();
44 } else {
45 $this->serviceName = $this->getEndpointPrefix();
46 }
47 $this->apiVersion = $this->getApiVersion();
48 if (isset($definition['clientContextParams'])) {
49 $this->clientContextParams = $definition['clientContextParams'];
50 }
51 $this->protocol = $this->selectProtocol($definition);
52 }
53 /**
54 * Creates a request serializer for the provided API object.
55 *
56 * @param Service $api API that contains a protocol.
57 * @param string $endpoint Endpoint to send requests to.
58 *
59 * @return callable
60 * @throws \UnexpectedValueException
61 */
62 public static function createSerializer(Service $api, $endpoint)
63 {
64 static $mapping = ['json' => Serializer\JsonRpcSerializer::class, 'query' => Serializer\QuerySerializer::class, 'rest-json' => Serializer\RestJsonSerializer::class, 'rest-xml' => Serializer\RestXmlSerializer::class];
65 $proto = $api->getProtocol();
66 if (isset($mapping[$proto])) {
67 return new $mapping[$proto]($api, $endpoint);
68 }
69 if ($proto == 'ec2') {
70 return new Serializer\QuerySerializer($api, $endpoint, new Serializer\Ec2ParamBuilder());
71 }
72 throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol());
73 }
74 /**
75 * Creates an error parser for the given protocol.
76 *
77 * Redundant method signature to preserve backwards compatibility.
78 *
79 * @param string $protocol Protocol to parse (e.g., query, json, etc.)
80 *
81 * @return callable
82 * @throws \UnexpectedValueException
83 */
84 public static function createErrorParser($protocol, ?Service $api = null)
85 {
86 static $mapping = ['json' => ErrorParser\JsonRpcErrorParser::class, 'query' => ErrorParser\XmlErrorParser::class, 'rest-json' => ErrorParser\RestJsonErrorParser::class, 'rest-xml' => ErrorParser\XmlErrorParser::class, 'ec2' => ErrorParser\XmlErrorParser::class];
87 if (isset($mapping[$protocol])) {
88 return new $mapping[$protocol]($api);
89 }
90 throw new \UnexpectedValueException("Unknown protocol: {$protocol}");
91 }
92 /**
93 * Applies the listeners needed to parse client models.
94 *
95 * @param Service $api API to create a parser for
96 * @return callable
97 * @throws \UnexpectedValueException
98 */
99 public static function createParser(Service $api)
100 {
101 static $mapping = ['json' => Parser\JsonRpcParser::class, 'query' => Parser\QueryParser::class, 'rest-json' => Parser\RestJsonParser::class, 'rest-xml' => Parser\RestXmlParser::class];
102 $proto = $api->getProtocol();
103 if (isset($mapping[$proto])) {
104 return new $mapping[$proto]($api);
105 }
106 if ($proto == 'ec2') {
107 return new Parser\QueryParser($api, null, \false);
108 }
109 throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol());
110 }
111 /**
112 * Get the full name of the service
113 *
114 * @return string
115 */
116 public function getServiceFullName()
117 {
118 return $this->definition['metadata']['serviceFullName'];
119 }
120 /**
121 * Get the service id
122 *
123 * @return string
124 */
125 public function getServiceId()
126 {
127 return $this->definition['metadata']['serviceId'];
128 }
129 /**
130 * Get the API version of the service
131 *
132 * @return string
133 */
134 public function getApiVersion()
135 {
136 return $this->definition['metadata']['apiVersion'];
137 }
138 /**
139 * Get the API version of the service
140 *
141 * @return string
142 */
143 public function getEndpointPrefix()
144 {
145 return $this->definition['metadata']['endpointPrefix'];
146 }
147 /**
148 * Get the signing name used by the service.
149 *
150 * @return string
151 */
152 public function getSigningName()
153 {
154 return $this->definition['metadata']['signingName'] ?: $this->definition['metadata']['endpointPrefix'];
155 }
156 /**
157 * Get the service name.
158 *
159 * @return string
160 */
161 public function getServiceName()
162 {
163 return $this->definition['metadata']['serviceIdentifier'] ?? null;
164 }
165 /**
166 * Get the default signature version of the service.
167 *
168 * Note: this method assumes "v4" when not specified in the model.
169 *
170 * @return string
171 */
172 public function getSignatureVersion()
173 {
174 return $this->definition['metadata']['signatureVersion'] ?: 'v4';
175 }
176 /**
177 * Get the protocol used by the service.
178 *
179 * @return string
180 */
181 public function getProtocol()
182 {
183 return $this->protocol;
184 }
185 /**
186 * Get the uid string used by the service
187 *
188 * @return string
189 */
190 public function getUid()
191 {
192 return $this->definition['metadata']['uid'];
193 }
194 /**
195 * Check if the description has a specific operation by name.
196 *
197 * @param string $name Operation to check by name
198 *
199 * @return bool
200 */
201 public function hasOperation($name)
202 {
203 return isset($this['operations'][$name]);
204 }
205 /**
206 * Get an operation by name.
207 *
208 * @param string $name Operation to retrieve by name
209 *
210 * @return Operation
211 * @throws \InvalidArgumentException If the operation is not found
212 */
213 public function getOperation($name)
214 {
215 if (!isset($this->operations[$name])) {
216 if (!isset($this->definition['operations'][$name])) {
217 throw new \InvalidArgumentException("Unknown operation: {$name}");
218 }
219 $this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap);
220 } elseif ($this->modifiedModel) {
221 $this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap);
222 }
223 return $this->operations[$name];
224 }
225 /**
226 * Get all of the operations of the description.
227 *
228 * @return Operation[]
229 */
230 public function getOperations()
231 {
232 $result = [];
233 foreach ($this->definition['operations'] as $name => $definition) {
234 $result[$name] = $this->getOperation($name);
235 }
236 return $result;
237 }
238 /**
239 * Get all of the error shapes of the service
240 *
241 * @return array
242 */
243 public function getErrorShapes()
244 {
245 $result = [];
246 foreach ($this->definition['shapes'] as $name => $definition) {
247 if (!empty($definition['exception'])) {
248 $definition['name'] = $name;
249 $result[] = new StructureShape($definition, $this->getShapeMap());
250 }
251 }
252 return $result;
253 }
254 /**
255 * Get all of the service metadata or a specific metadata key value.
256 *
257 * @param string|null $key Key to retrieve or null to retrieve all metadata
258 *
259 * @return mixed Returns the result or null if the key is not found
260 */
261 public function getMetadata($key = null)
262 {
263 if (!$key) {
264 return $this['metadata'];
265 }
266 if (isset($this->definition['metadata'][$key])) {
267 return $this->definition['metadata'][$key];
268 }
269 return null;
270 }
271 /**
272 * Gets an associative array of available paginator configurations where
273 * the key is the name of the paginator, and the value is the paginator
274 * configuration.
275 *
276 * @return array
277 * @unstable The configuration format of paginators may change in the future
278 */
279 public function getPaginators()
280 {
281 if (!isset($this->paginators)) {
282 $res = \call_user_func($this->apiProvider, 'paginator', $this->serviceName, $this->apiVersion);
283 $this->paginators = isset($res['pagination']) ? $res['pagination'] : [];
284 }
285 return $this->paginators;
286 }
287 /**
288 * Determines if the service has a paginator by name.
289 *
290 * @param string $name Name of the paginator.
291 *
292 * @return bool
293 */
294 public function hasPaginator($name)
295 {
296 return isset($this->getPaginators()[$name]);
297 }
298 /**
299 * Retrieve a paginator by name.
300 *
301 * @param string $name Paginator to retrieve by name. This argument is
302 * typically the operation name.
303 * @return array
304 * @throws \UnexpectedValueException if the paginator does not exist.
305 * @unstable The configuration format of paginators may change in the future
306 */
307 public function getPaginatorConfig($name)
308 {
309 static $defaults = ['input_token' => null, 'output_token' => null, 'limit_key' => null, 'result_key' => null, 'more_results' => null];
310 if ($this->hasPaginator($name)) {
311 return $this->paginators[$name] + $defaults;
312 }
313 throw new \UnexpectedValueException("There is no {$name} " . "paginator defined for the {$this->serviceName} service.");
314 }
315 /**
316 * Gets an associative array of available waiter configurations where the
317 * key is the name of the waiter, and the value is the waiter
318 * configuration.
319 *
320 * @return array
321 */
322 public function getWaiters()
323 {
324 if (!isset($this->waiters)) {
325 $res = \call_user_func($this->apiProvider, 'waiter', $this->serviceName, $this->apiVersion);
326 $this->waiters = isset($res['waiters']) ? $res['waiters'] : [];
327 }
328 return $this->waiters;
329 }
330 /**
331 * Determines if the service has a waiter by name.
332 *
333 * @param string $name Name of the waiter.
334 *
335 * @return bool
336 */
337 public function hasWaiter($name)
338 {
339 return isset($this->getWaiters()[$name]);
340 }
341 /**
342 * Get a waiter configuration by name.
343 *
344 * @param string $name Name of the waiter by name.
345 *
346 * @return array
347 * @throws \UnexpectedValueException if the waiter does not exist.
348 */
349 public function getWaiterConfig($name)
350 {
351 // Error if the waiter is not defined
352 if ($this->hasWaiter($name)) {
353 return $this->waiters[$name];
354 }
355 throw new \UnexpectedValueException("There is no {$name} waiter " . "defined for the {$this->serviceName} service.");
356 }
357 /**
358 * Get the shape map used by the API.
359 *
360 * @return ShapeMap
361 */
362 public function getShapeMap()
363 {
364 return $this->shapeMap;
365 }
366 /**
367 * Get all the context params of the description.
368 *
369 * @return array
370 */
371 public function getClientContextParams()
372 {
373 return $this->clientContextParams;
374 }
375 /**
376 * Get the service's api provider.
377 *
378 * @return callable
379 */
380 public function getProvider()
381 {
382 return $this->apiProvider;
383 }
384 /**
385 * Get the service's definition.
386 *
387 * @return callable
388 */
389 public function getDefinition()
390 {
391 return $this->definition;
392 }
393 /**
394 * Sets the service's api definition.
395 * Intended for internal use only.
396 *
397 * @return void
398 *
399 * @internal
400 */
401 public function setDefinition($definition)
402 {
403 $this->definition = $definition;
404 $this->shapeMap = new ShapeMap($definition['shapes']);
405 $this->modifiedModel = \true;
406 }
407 /**
408 * Denotes whether or not a service's definition has
409 * been modified. Intended for internal use only.
410 *
411 * @return bool
412 *
413 * @internal
414 */
415 public function isModifiedModel()
416 {
417 return $this->modifiedModel;
418 }
419 /**
420 * Accepts a list of protocols derived from the service model.
421 * Returns the highest priority compatible auth scheme if the `protocols` trait is present.
422 * Otherwise, returns the value of the `protocol` field, if set, or null.
423 *
424 * @param array $definition
425 *
426 * @return string|null
427 */
428 private function selectProtocol(array $definition) : string|null
429 {
430 $modeledProtocols = $definition['metadata']['protocols'] ?? null;
431 if (!empty($modeledProtocols)) {
432 foreach (SupportedProtocols::cases() as $protocol) {
433 if (\in_array($protocol->value, $modeledProtocols)) {
434 return $protocol->value;
435 }
436 }
437 }
438 return $definition['metadata']['protocol'] ?? null;
439 }
440 }
441