PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
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 / Parser / EventParsingIterator.php

EventParsingIterator.php in Media Cloud Sync 1.2.11, at includes/sdk/s3/Aws/Api/Parser/EventParsingIterator.php

87 lines 2.9 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\Parser;
4
5 use Iterator;
6 use Dudlewebs\WPMCS\s3\Aws\Exception\EventStreamDataException;
7 use Dudlewebs\WPMCS\s3\Aws\Api\Parser\Exception\ParserException;
8 use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
10 /**
11 * @internal Implements a decoder for a binary encoded event stream that will
12 * decode, validate, and provide individual events from the stream.
13 */
14 class EventParsingIterator implements Iterator
15 {
16 /** @var StreamInterface */
17 private $decodingIterator;
18 /** @var StructureShape */
19 private $shape;
20 /** @var AbstractParser */
21 private $parser;
22 public function __construct(StreamInterface $stream, StructureShape $shape, AbstractParser $parser)
23 {
24 $this->decodingIterator = new DecodingEventStreamIterator($stream);
25 $this->shape = $shape;
26 $this->parser = $parser;
27 }
28 #[\ReturnTypeWillChange]
29 public function current()
30 {
31 return $this->parseEvent($this->decodingIterator->current());
32 }
33 #[\ReturnTypeWillChange]
34 public function key()
35 {
36 return $this->decodingIterator->key();
37 }
38 #[\ReturnTypeWillChange]
39 public function next()
40 {
41 $this->decodingIterator->next();
42 }
43 #[\ReturnTypeWillChange]
44 public function rewind()
45 {
46 $this->decodingIterator->rewind();
47 }
48 #[\ReturnTypeWillChange]
49 public function valid()
50 {
51 return $this->decodingIterator->valid();
52 }
53 private function parseEvent(array $event)
54 {
55 if (!empty($event['headers'][':message-type'])) {
56 if ($event['headers'][':message-type'] === 'error') {
57 return $this->parseError($event);
58 }
59 if ($event['headers'][':message-type'] !== 'event') {
60 throw new ParserException('Failed to parse unknown message type.');
61 }
62 }
63 if (empty($event['headers'][':event-type'])) {
64 throw new ParserException('Failed to parse without event type.');
65 }
66 $eventShape = $this->shape->getMember($event['headers'][':event-type']);
67 $parsedEvent = [];
68 foreach ($eventShape['members'] as $shape => $details) {
69 if (!empty($details['eventpayload'])) {
70 $payloadShape = $eventShape->getMember($shape);
71 if ($payloadShape['type'] === 'blob') {
72 $parsedEvent[$shape] = $event['payload'];
73 } else {
74 $parsedEvent[$shape] = $this->parser->parseMemberFromStream($event['payload'], $payloadShape, null);
75 }
76 } else {
77 $parsedEvent[$shape] = $event['headers'][$shape];
78 }
79 }
80 return [$event['headers'][':event-type'] => $parsedEvent];
81 }
82 private function parseError(array $event)
83 {
84 throw new EventStreamDataException($event['headers'][':error-code'], $event['headers'][':error-message']);
85 }
86 }
87