| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\Aws\Api\Parser; |
| 4 | 4 | |
| 5 | +use Dudlewebs\WPMCS\s3\Aws\Api\Operation; | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape; |
| 6 | 7 | use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 7 | 8 | use Dudlewebs\WPMCS\s3\Aws\Result; |
| 8 | 9 | use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| @@ -17,9 +18,9 @@ | ||
| 17 | 18 | /** |
| 18 | 19 | * @param Service $api Service description |
| 19 | 20 | * @param JsonParser $parser JSON body builder |
| 20 | 21 | */ |
| 21 | - public function __construct(Service $api, JsonParser $parser = null) | |
| 22 | + public function __construct(Service $api, ?JsonParser $parser = null) | |
| 22 | 23 | { |
| 23 | 24 | parent::__construct($api); |
| 24 | 25 | $this->parser = $parser ?: new JsonParser(); |
| 25 | 26 | } |
| @@ -25,10 +26,32 @@ | ||
| 25 | 26 | } |
| 26 | 27 | public function __invoke(CommandInterface $command, ResponseInterface $response) |
| 27 | 28 | { |
| 28 | 29 | $operation = $this->api->getOperation($command->getName()); |
| 29 | - $result = null === $operation['output'] ? null : $this->parseMemberFromStream($response->getBody(), $operation->getOutput(), $response); | |
| 30 | - return new Result($result ?: []); | |
| 30 | + return $this->parseResponse($response, $operation); | |
| 31 | + } | |
| 32 | + /** | |
| 33 | + * This method parses a response based on JSON RPC protocol. | |
| 34 | + * | |
| 35 | + * @param ResponseInterface $response the response to parse. | |
| 36 | + * @param Operation $operation the operation which holds information for | |
| 37 | + * parsing the response. | |
| 38 | + * | |
| 39 | + * @return Result | |
| 40 | + */ | |
| 41 | + private function parseResponse(ResponseInterface $response, Operation $operation) | |
| 42 | + { | |
| 43 | + if (null === $operation['output']) { | |
| 44 | + return new Result([]); | |
| 45 | + } | |
| 46 | + $outputShape = $operation->getOutput(); | |
| 47 | + foreach ($outputShape->getMembers() as $memberName => $memberProps) { | |
| 48 | + if (!empty($memberProps['eventstream'])) { | |
| 49 | + return new Result([$memberName => new EventParsingIterator($response->getBody(), $outputShape->getMember($memberName), $this)]); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + $result = $this->parseMemberFromStream($response->getBody(), $operation->getOutput(), $response); | |
| 53 | + return new Result(\is_null($result) ? [] : $result); | |
| 31 | 54 | } |
| 32 | 55 | public function parseMemberFromStream(StreamInterface $stream, StructureShape $member, $response) |
| 33 | 56 | { |
| 34 | 57 | return $this->parser->parse($member, $this->parseJson($stream, $response)); |