]> git.mxchange.org Git - friendica-addons.git/blob - s3_storage/vendor/akeeba/s3/src/Response.php
Merge pull request #1236 from nupplaphil/feat/s3
[friendica-addons.git] / s3_storage / vendor / akeeba / s3 / src / Response.php
1 <?php
2 /**
3  * Akeeba Engine
4  *
5  * @package   akeebaengine
6  * @copyright Copyright (c)2006-2020 Nicholas K. Dionysopoulos / Akeeba Ltd
7  * @license   GNU General Public License version 3, or later
8  */
9
10 namespace Akeeba\Engine\Postproc\Connector\S3v4;
11
12 use Akeeba\Engine\Postproc\Connector\S3v4\Exception\PropertyNotFound;
13 use Akeeba\Engine\Postproc\Connector\S3v4\Response\Error;
14 use SimpleXMLElement;
15
16 // Protection against direct access
17 defined('AKEEBAENGINE') or die();
18
19 /**
20  * Amazon S3 API response object
21  *
22  * @property   Error                        $error    Response error object
23  * @property   string|SimpleXMLElement|null $body     Body data
24  * @property   int                          $code     Response code
25  * @property   array                        $headers  Any headers we may have
26  */
27 class Response
28 {
29         /**
30          * Error object
31          *
32          * @var  Error
33          */
34         private $error = null;
35
36         /**
37          * Response body
38          *
39          * @var  string|SimpleXMLElement|null
40          */
41         private $body = null;
42
43         /**
44          * Status code of the response, e.g. 200 for OK, 403 for Forbidden etc
45          *
46          * @var  int
47          */
48         private $code = 0;
49
50         /**
51          * Response headers
52          *
53          * @var  array
54          */
55         private $headers = [];
56
57         /**
58          * Response constructor.
59          */
60         public function __construct()
61         {
62                 $this->error = new Error();
63         }
64
65         /**
66          * Is this an error response?
67          *
68          * @return  bool
69          */
70         public function isError(): bool
71         {
72                 return is_null($this->error) || $this->error->isError();
73         }
74
75         /**
76          * Does this response have a body?
77          *
78          * @return  bool
79          */
80         public function hasBody(): bool
81         {
82                 return !empty($this->body);
83         }
84
85         /**
86          * Get the response error object
87          *
88          * @return  Error
89          */
90         public function getError(): Error
91         {
92                 return $this->error;
93         }
94
95         /**
96          * Set the response error object
97          *
98          * @param   Error  $error
99          */
100         public function setError(Error $error): void
101         {
102                 $this->error = $error;
103         }
104
105         /**
106          * Get the response body
107          *
108          * If there is no body set up you get NULL.
109          *
110          * If the body is binary data (e.g. downloading a file) or other non-XML data you get a string.
111          *
112          * If the body was an XML string – the standard Amazon S3 REST API response type – you get a SimpleXMLElement
113          * object.
114          *
115          * @return string|SimpleXMLElement|null
116          */
117         public function getBody()
118         {
119                 return $this->body;
120         }
121
122         /**
123          * Set the response body. If it's a string we'll try to parse it as XML.
124          *
125          * @param   string|SimpleXMLElement|null  $body
126          */
127         public function setBody($body): void
128         {
129                 $this->body = null;
130
131                 if (empty($body))
132                 {
133                         return;
134                 }
135
136                 $this->body = $body;
137
138                 $this->finaliseBody();
139         }
140
141         public function resetBody(): void
142         {
143                 $this->body = null;
144         }
145
146         public function addToBody(string $data): void
147         {
148                 if (empty($this->body))
149                 {
150                         $this->body = '';
151                 }
152
153                 $this->body .= $data;
154         }
155
156         public function finaliseBody(): void
157         {
158                 if (!$this->hasBody())
159                 {
160                         return;
161                 }
162
163                 if (!isset($this->headers['type']))
164                 {
165                         $this->headers['type'] = 'text/plain';
166                 }
167
168                 if (is_string($this->body) &&
169                         (($this->headers['type'] == 'application/xml') || (substr($this->body, 0, 5) == '<?xml'))
170                 )
171                 {
172                         $this->body = simplexml_load_string($this->body);
173                 }
174
175                 if (is_object($this->body) && ($this->body instanceof SimpleXMLElement))
176                 {
177                         $this->parseBody();
178                 }
179         }
180
181         /**
182          * Returns the status code of the response
183          *
184          * @return  int
185          */
186         public function getCode(): int
187         {
188                 return $this->code;
189         }
190
191         /**
192          * Sets the status code of the response
193          *
194          * @param   int  $code
195          */
196         public function setCode(int $code): void
197         {
198                 $this->code = $code;
199         }
200
201         /**
202          * Get the response headers
203          *
204          * @return  array
205          */
206         public function getHeaders(): array
207         {
208                 return $this->headers;
209         }
210
211         /**
212          * Set the response headers
213          *
214          * @param   array  $headers
215          */
216         public function setHeaders(array $headers): void
217         {
218                 $this->headers = $headers;
219         }
220
221         /**
222          * Set a single header
223          *
224          * @param   string  $name   The header name
225          * @param   string  $value  The header value
226          *
227          * @return  void
228          */
229         public function setHeader(string $name, string $value): void
230         {
231                 $this->headers[$name] = $value;
232         }
233
234         /**
235          * Does a header by this name exist?
236          *
237          * @param   string  $name  The header to look for
238          *
239          * @return  bool  True if it exists
240          */
241         public function hasHeader(string $name): bool
242         {
243                 return array_key_exists($name, $this->headers);
244         }
245
246         /**
247          * Unset a response header
248          *
249          * @param   string  $name  The header to unset
250          *
251          * @return  void
252          */
253         public function unsetHeader(string $name): void
254         {
255                 if ($this->hasHeader($name))
256                 {
257                         unset ($this->headers[$name]);
258                 }
259         }
260
261         /**
262          * Magic getter for the protected properties
263          *
264          * @param   string  $name
265          *
266          * @return  mixed
267          */
268         public function __get(string $name)
269         {
270                 switch ($name)
271                 {
272                         case 'error':
273                                 return $this->getError();
274                                 break;
275
276                         case 'body':
277                                 return $this->getBody();
278                                 break;
279
280                         case 'code':
281                                 return $this->getCode();
282                                 break;
283
284                         case 'headers':
285                                 return $this->getHeaders();
286                                 break;
287                 }
288
289                 throw new PropertyNotFound("Property $name not found in " . get_class($this));
290         }
291
292         /**
293          * Magic setter for the protected properties
294          *
295          * @param   string  $name   The name of the property
296          * @param   mixed   $value  The value of the property
297          *
298          * @return  void
299          */
300         public function __set(string $name, $value): void
301         {
302                 switch ($name)
303                 {
304                         case 'error':
305                                 $this->setError($value);
306                                 break;
307
308                         case 'body':
309                                 $this->setBody($value);
310                                 break;
311
312                         case 'code':
313                                 $this->setCode($value);
314                                 break;
315
316                         case 'headers':
317                                 $this->setHeaders($value);
318                                 break;
319
320                         default:
321                                 throw new PropertyNotFound("Property $name not found in " . get_class($this));
322                 }
323         }
324
325         /**
326          * Scans the SimpleXMLElement body for errors and propagates them to the Error object
327          */
328         protected function parseBody(): void
329         {
330                 if (!in_array($this->code, [200, 204]) &&
331                         isset($this->body->Code, $this->body->Message)
332                 )
333                 {
334                         $this->error = new Error(
335                                 $this->code,
336                                 (string) $this->body->Message
337                         );
338
339                         if (isset($this->body->Resource))
340                         {
341                                 $this->error->setResource((string) $this->body->Resource);
342                         }
343                 }
344         }
345 }