1 | <?php |
||
32 | final class FirehoseAdapter implements AdapterInterface |
||
33 | { |
||
34 | const BATCHSIZE_SEND = 100; |
||
35 | |||
36 | /** @var FirehoseClient */ |
||
37 | protected $client; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $options; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $deliveryStreamName; |
||
44 | |||
45 | /** |
||
46 | * @param FirehoseClient $client |
||
47 | * @param string $deliveryStreamName |
||
48 | * @param array $options - BatchSize <integer> The number of messages to send in each batch. |
||
49 | */ |
||
50 | 7 | public function __construct(FirehoseClient $client, $deliveryStreamName, array $options = []) |
|
56 | |||
57 | /** |
||
58 | * @param MessageInterface[] $messages |
||
59 | * |
||
60 | * @throws MethodNotSupportedException |
||
61 | */ |
||
62 | 1 | public function acknowledge(array $messages) |
|
70 | |||
71 | /** |
||
72 | * @param MessageFactoryInterface $factory |
||
73 | * @param int $limit |
||
74 | * |
||
75 | * @throws MethodNotSupportedException |
||
76 | */ |
||
77 | 1 | public function dequeue(MessageFactoryInterface $factory, $limit) |
|
78 | { |
||
79 | 1 | throw new MethodNotSupportedException( |
|
80 | 1 | __FUNCTION__, |
|
81 | 1 | $this, |
|
82 | 1 | [] |
|
83 | 1 | ); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param MessageInterface[] $messages |
||
88 | * |
||
89 | * @throws FailedEnqueueException |
||
90 | */ |
||
91 | 2 | public function enqueue(array $messages) |
|
92 | { |
||
93 | 2 | $failed = []; |
|
94 | 2 | $batches = array_chunk( |
|
95 | 2 | $messages, |
|
96 | 2 | $this->getOption('BatchSize', self::BATCHSIZE_SEND) |
|
97 | 2 | ); |
|
98 | |||
99 | 2 | foreach ($batches as $batch) { |
|
100 | 2 | $requestRecords = array_map(function (MessageInterface $message) { |
|
101 | return [ |
||
102 | 2 | 'Data' => $message->getBody() |
|
103 | 2 | ]; |
|
104 | 2 | }, $batch); |
|
105 | |||
106 | $request = [ |
||
107 | 2 | 'DeliveryStreamName' => $this->deliveryStreamName, |
|
108 | 2 | 'Records' => $requestRecords, |
|
109 | 2 | ]; |
|
110 | |||
111 | 2 | $results = $this->client->putRecordBatch($request); |
|
112 | |||
113 | 2 | foreach ($results->get('RequestResponses') as $idx => $response) { |
|
114 | if (isset($response['ErrorCode'])) { |
||
115 | $failed[] = $messages[$batch[$idx]['Id']]; |
||
116 | } |
||
117 | 2 | } |
|
118 | 2 | } |
|
119 | |||
120 | 2 | if (!empty($failed)) { |
|
121 | throw new FailedEnqueueException($this, $failed); |
||
122 | } |
||
123 | 2 | } |
|
124 | |||
125 | /** |
||
126 | * @param string $name |
||
127 | * @param mixed $default |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | 2 | protected function getOption($name, $default = null) |
|
135 | |||
136 | /** |
||
137 | * @throws MethodNotSupportedException |
||
138 | */ |
||
139 | 1 | public function purge() |
|
147 | |||
148 | /** |
||
149 | * @throws MethodNotSupportedException |
||
150 | */ |
||
151 | 1 | public function delete() |
|
159 | } |
||
160 |