# media-cloud-sync/1.0.1/includes/sdk/s3/Aws/Api/Parser/EventParsingIterator.php

Media Cloud Sync, version 1.0.1. 87 lines.

- Page: https://pluginprobe.com/plugins/media-cloud-sync/1.0.1/code/includes/sdk/s3/Aws/Api/Parser/EventParsingIterator.php
- Raw: https://pluginprobe.com/plugins/media-cloud-sync/1.0.1/raw/includes/sdk/s3/Aws/Api/Parser/EventParsingIterator.php
- Modified: 2024-10-27T10:40:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/media-cloud-sync/1.0.1/code/includes/sdk/s3/Aws/Api/Parser/EventParsingIterator.php#L10-L20`.

```php
<?php

namespace Dudlewebs\WPMCS\s3\Aws\Api\Parser;

use Iterator;
use Dudlewebs\WPMCS\s3\Aws\Exception\EventStreamDataException;
use Dudlewebs\WPMCS\s3\Aws\Api\Parser\Exception\ParserException;
use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape;
use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
/**
 * @internal Implements a decoder for a binary encoded event stream that will
 * decode, validate, and provide individual events from the stream.
 */
class EventParsingIterator implements Iterator
{
    /** @var StreamInterface */
    private $decodingIterator;
    /** @var StructureShape */
    private $shape;
    /** @var AbstractParser */
    private $parser;
    public function __construct(StreamInterface $stream, StructureShape $shape, AbstractParser $parser)
    {
        $this->decodingIterator = new DecodingEventStreamIterator($stream);
        $this->shape = $shape;
        $this->parser = $parser;
    }
    #[\ReturnTypeWillChange]
    public function current()
    {
        return $this->parseEvent($this->decodingIterator->current());
    }
    #[\ReturnTypeWillChange]
    public function key()
    {
        return $this->decodingIterator->key();
    }
    #[\ReturnTypeWillChange]
    public function next()
    {
        $this->decodingIterator->next();
    }
    #[\ReturnTypeWillChange]
    public function rewind()
    {
        $this->decodingIterator->rewind();
    }
    #[\ReturnTypeWillChange]
    public function valid()
    {
        return $this->decodingIterator->valid();
    }
    private function parseEvent(array $event)
    {
        if (!empty($event['headers'][':message-type'])) {
            if ($event['headers'][':message-type'] === 'error') {
                return $this->parseError($event);
            }
            if ($event['headers'][':message-type'] !== 'event') {
                throw new ParserException('Failed to parse unknown message type.');
            }
        }
        if (empty($event['headers'][':event-type'])) {
            throw new ParserException('Failed to parse without event type.');
        }
        $eventShape = $this->shape->getMember($event['headers'][':event-type']);
        $parsedEvent = [];
        foreach ($eventShape['members'] as $shape => $details) {
            if (!empty($details['eventpayload'])) {
                $payloadShape = $eventShape->getMember($shape);
                if ($payloadShape['type'] === 'blob') {
                    $parsedEvent[$shape] = $event['payload'];
                } else {
                    $parsedEvent[$shape] = $this->parser->parseMemberFromStream($event['payload'], $payloadShape, null);
                }
            } else {
                $parsedEvent[$shape] = $event['headers'][$shape];
            }
        }
        return [$event['headers'][':event-type'] => $parsedEvent];
    }
    private function parseError(array $event)
    {
        throw new EventStreamDataException($event['headers'][':error-code'], $event['headers'][':error-message']);
    }
}

```
