Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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