| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api\Serializer; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Request; |
| 8 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 9 |
/** |
| 10 |
* Prepares a JSON-RPC request for transfer. |
| 11 |
* @internal |
| 12 |
*/ |
| 13 |
class JsonRpcSerializer |
| 14 |
{ |
| 15 |
/** @var JsonBody */ |
| 16 |
private $jsonFormatter; |
| 17 |
/** @var string */ |
| 18 |
private $endpoint; |
| 19 |
/** @var Service */ |
| 20 |
private $api; |
| 21 |
/** @var string */ |
| 22 |
private $contentType; |
| 23 |
/** |
| 24 |
* @param Service $api Service description |
| 25 |
* @param string $endpoint Endpoint to connect to |
| 26 |
* @param JsonBody $jsonFormatter Optional JSON formatter to use |
| 27 |
*/ |
| 28 |
public function __construct(Service $api, $endpoint, JsonBody $jsonFormatter = null) |
| 29 |
{ |
| 30 |
$this->endpoint = $endpoint; |
| 31 |
$this->api = $api; |
| 32 |
$this->jsonFormatter = $jsonFormatter ?: new JsonBody($this->api); |
| 33 |
$this->contentType = JsonBody::getContentType($api); |
| 34 |
} |
| 35 |
/** |
| 36 |
* When invoked with an AWS command, returns a serialization array |
| 37 |
* containing "method", "uri", "headers", and "body" key value pairs. |
| 38 |
* |
| 39 |
* @param CommandInterface $command |
| 40 |
* |
| 41 |
* @return RequestInterface |
| 42 |
*/ |
| 43 |
public function __invoke(CommandInterface $command) |
| 44 |
{ |
| 45 |
$name = $command->getName(); |
| 46 |
$operation = $this->api->getOperation($name); |
| 47 |
return new Request($operation['http']['method'], $this->endpoint, ['X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $name, 'Content-Type' => $this->contentType], $this->jsonFormatter->build($operation->getInput(), $command->toArray())); |
| 48 |
} |
| 49 |
} |
| 50 |
|