1 | <?php |
||
28 | final class EventStore implements EventStoreInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $url; |
||
34 | |||
35 | /** |
||
36 | * @var Client |
||
37 | */ |
||
38 | private $httpClient; |
||
39 | |||
40 | /** |
||
41 | * @var ResponseInterface |
||
42 | */ |
||
43 | private $lastResponse; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $badCodeHandlers = []; |
||
49 | |||
50 | /** |
||
51 | * @param string $url Endpoint of the EventStore HTTP API |
||
52 | * @param HttpClientInterface the http client |
||
53 | */ |
||
54 | 26 | public function __construct($url, HttpClientInterface $httpClient = null) |
|
62 | |||
63 | /** |
||
64 | * Delete a stream |
||
65 | * |
||
66 | * @param string $streamName Name of the stream |
||
67 | * @param StreamDeletion $mode Deletion mode (soft or hard) |
||
68 | */ |
||
69 | 4 | public function deleteStream($streamName, StreamDeletion $mode) |
|
79 | |||
80 | /** |
||
81 | * Get the response from the last HTTP call to the EventStore API |
||
82 | * |
||
83 | * @return ResponseInterface |
||
84 | */ |
||
85 | 23 | public function getLastResponse() |
|
89 | |||
90 | /** |
||
91 | * Navigate stream feed through link relations |
||
92 | * |
||
93 | * @param StreamFeed $streamFeed The stream feed to navigate through |
||
94 | * @param LinkRelation $relation The "direction" expressed as link relation |
||
95 | * @return null|StreamFeed |
||
96 | */ |
||
97 | 8 | public function navigateStreamFeed(StreamFeed $streamFeed, LinkRelation $relation) |
|
107 | |||
108 | /** |
||
109 | * Open a stream feed for read and navigation |
||
110 | * |
||
111 | * @param string $streamName The stream name |
||
112 | * @param EntryEmbedMode $embedMode The event entries embed mode (none, rich or body) |
||
113 | * @return StreamFeed |
||
114 | */ |
||
115 | 18 | public function openStreamFeed($streamName, EntryEmbedMode $embedMode = null) |
|
121 | |||
122 | /** |
||
123 | * Read a single event |
||
124 | * |
||
125 | * @param string $eventUrl The url of the event |
||
126 | * @return Event |
||
127 | */ |
||
128 | 3 | public function readEvent($eventUrl) |
|
139 | |||
140 | /** |
||
141 | * Read a single event |
||
142 | * |
||
143 | * @param string $eventUrl The url of the event |
||
144 | * @return Event |
||
145 | */ |
||
146 | 7 | public function readEventBatch(array $eventUrls) |
|
168 | |||
169 | /** |
||
170 | * @param array $content |
||
171 | * @return Event |
||
172 | */ |
||
173 | 9 | protected function createEventFromResponseContent(array $content) |
|
182 | |||
183 | /** |
||
184 | * Write one or more events to a stream |
||
185 | * |
||
186 | * @param string $streamName The stream name |
||
187 | * @param WritableToStream $events Single event or a collection of events |
||
188 | * @param int $expectedVersion The expected version of the stream |
||
189 | * @throws Exception\WrongExpectedVersionException |
||
190 | */ |
||
191 | 22 | public function writeToStream($streamName, WritableToStream $events, $expectedVersion = ExpectedVersion::ANY) |
|
215 | |||
216 | /** |
||
217 | * @param string $streamName |
||
218 | * @return StreamFeedIterator |
||
219 | */ |
||
220 | 1 | public function forwardStreamFeedIterator($streamName) |
|
224 | |||
225 | /** |
||
226 | * @param string $streamName |
||
227 | * @return StreamFeedIterator |
||
228 | */ |
||
229 | 1 | public function backwardStreamFeedIterator($streamName) |
|
233 | |||
234 | /** |
||
235 | * @throws Exception\ConnectionFailedException |
||
236 | */ |
||
237 | 26 | private function checkConnection() |
|
246 | |||
247 | /** |
||
248 | * @param string $streamName |
||
249 | * @return string |
||
250 | */ |
||
251 | 24 | private function getStreamUrl($streamName) |
|
255 | |||
256 | /** |
||
257 | * @param string $streamUrl |
||
258 | * @param EntryEmbedMode $embedMode |
||
259 | * @return StreamFeed |
||
260 | * @throws Exception\StreamDeletedException |
||
261 | * @throws Exception\StreamNotFoundException |
||
262 | */ |
||
263 | 18 | private function readStreamFeed($streamUrl, EntryEmbedMode $embedMode = null) |
|
283 | |||
284 | /** |
||
285 | * @param string $uri |
||
286 | * @return \GuzzleHttp\Message\Request|RequestInterface |
||
287 | */ |
||
288 | 18 | private function getJsonRequest($uri) |
|
298 | |||
299 | /** |
||
300 | * @param RequestInterface $request |
||
301 | */ |
||
302 | 26 | private function sendRequest(RequestInterface $request) |
|
310 | |||
311 | /** |
||
312 | * @param string $streamUrl |
||
313 | * @throws Exception\StreamDeletedException |
||
314 | * @throws Exception\StreamNotFoundException |
||
315 | * @throws Exception\UnauthorizedException |
||
316 | */ |
||
317 | 18 | private function ensureStatusCodeIsGood($streamUrl) |
|
325 | |||
326 | 26 | private function initBadCodeHandlers() |
|
327 | { |
||
328 | 26 | $this->badCodeHandlers = [ |
|
329 | ResponseCode::HTTP_NOT_FOUND => function ($streamUrl) { |
||
330 | 1 | throw new StreamNotFoundException( |
|
331 | 1 | sprintf( |
|
332 | 1 | 'No stream found at %s', |
|
333 | $streamUrl |
||
334 | 1 | ) |
|
335 | 1 | ); |
|
336 | 26 | }, |
|
337 | |||
338 | ResponseCode::HTTP_GONE => function ($streamUrl) { |
||
339 | 2 | throw new StreamDeletedException( |
|
340 | 2 | sprintf( |
|
341 | 2 | 'Stream at %s has been permanently deleted', |
|
342 | $streamUrl |
||
343 | 2 | ) |
|
344 | 2 | ); |
|
345 | 26 | }, |
|
346 | |||
347 | 1 | ResponseCode::HTTP_UNAUTHORIZED => function ($streamUrl) { |
|
348 | 1 | throw new UnauthorizedException( |
|
349 | 1 | sprintf( |
|
350 | 1 | 'Tried to open stream %s got 401', |
|
351 | $streamUrl |
||
352 | 1 | ) |
|
353 | 1 | ); |
|
354 | } |
||
355 | 26 | ]; |
|
356 | 26 | } |
|
357 | |||
358 | 15 | private function lastResponseAsJson() |
|
362 | } |
||
363 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..