Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/ErrorParser/XmlErrorParser.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\ErrorParser;
3
4 use Aws\Api\Parser\AbstractParser;
5 use Aws\Api\Parser\PayloadParserTrait;
6 use Aws\Api\Parser\XmlParser;
7 use Aws\Api\Service;
8 use Aws\Api\StructureShape;
9 use Aws\CommandInterface;
10 use Psr\Http\Message\ResponseInterface;
11
12 /**
13 * Parses XML errors.
14 */
15 class XmlErrorParser extends AbstractErrorParser
16 {
17 use PayloadParserTrait;
18
19 protected $parser;
20
21 public function __construct(?Service $api = null, ?XmlParser $parser = null)
22 {
23 parent::__construct($api);
24 $this->parser = $parser ?: new XmlParser();
25 }
26
27 public function __invoke(
28 ResponseInterface $response,
29 ?CommandInterface $command = null
30 ) {
31 $response = AbstractParser::getResponseWithCachingStream($response);
32 $code = (string) $response->getStatusCode();
33
34 $data = [
35 'type' => $code[0] == '4' ? 'client' : 'server',
36 'request_id' => null,
37 'code' => null,
38 'message' => null,
39 'parsed' => null
40 ];
41
42 $rawBody = AbstractParser::getBodyContents($response);
43 if (!empty($rawBody)) {
44 $this->parseBody($this->parseXml($rawBody, $response), $data);
45 } else {
46 $this->parseHeaders($response, $data);
47 }
48
49 $this->populateShape($data, $response, $command);
50
51 return $data;
52 }
53
54 private function parseHeaders(ResponseInterface $response, array &$data)
55 {
56 if ($response->getStatusCode() == '404') {
57 $data['code'] = 'NotFound';
58 }
59
60 $data['message'] = $response->getStatusCode() . ' '
61 . $response->getReasonPhrase();
62
63 if ($requestId = $response->getHeaderLine('x-amz-request-id')) {
64 $data['request_id'] = $requestId;
65 $data['message'] .= " (Request-ID: $requestId)";
66 }
67 }
68
69 private function parseBody(\SimpleXMLElement $body, array &$data)
70 {
71 $data['parsed'] = $body;
72 $prefix = $this->registerNamespacePrefix($body);
73
74 if ($tempXml = $body->xpath("//{$prefix}Code[1]")) {
75 $data['code'] = (string) $tempXml[0];
76 }
77
78 if ($tempXml = $body->xpath("//{$prefix}Message[1]")) {
79 $data['message'] = (string) $tempXml[0];
80 }
81
82 $tempXml = $body->xpath("//{$prefix}RequestId[1]");
83 if (isset($tempXml[0])) {
84 $data['request_id'] = (string)$tempXml[0];
85 }
86 }
87
88 protected function registerNamespacePrefix(\SimpleXMLElement $element)
89 {
90 $namespaces = $element->getDocNamespaces();
91 if (!isset($namespaces[''])) {
92 return '';
93 }
94
95 // Account for the default namespace being defined and PHP not
96 // being able to handle it :(.
97 $element->registerXPathNamespace('ns', $namespaces['']);
98 return 'ns:';
99 }
100
101 protected function payload(
102 ResponseInterface $response,
103 StructureShape $member
104 ) {
105 $rawBody = AbstractParser::getBodyContents($response);
106
107 if (empty($rawBody)) {
108 return $rawBody;
109 }
110
111 $xmlBody = $this->parseXml($rawBody, $response);
112 $prefix = $this->registerNamespacePrefix($xmlBody);
113 $errorBody = $xmlBody->xpath("//{$prefix}Error");
114
115 if (is_array($errorBody) && !empty($errorBody[0])) {
116 return $this->parser->parse($member, $errorBody[0]);
117 }
118
119 return $rawBody;
120 }
121 }