1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RayRutjes\GetEventStore\Client\Http; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use RayRutjes\GetEventStore\Client\Http\Feed\EventStreamViaPersistentSubscriptionFeed; |
7
|
|
|
use RayRutjes\GetEventStore\Client\Http\Feed\EventStreamViaPersistentSubscriptionFeedLink; |
8
|
|
|
use RayRutjes\GetEventStore\EventRecord; |
9
|
|
|
|
10
|
|
|
final class ReadEventStreamViaPersistentSubscriptionResponseInspector extends AbstractResponseInspector |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var EventStreamViaPersistentSubscriptionFeed |
14
|
|
|
*/ |
15
|
|
|
private $feed; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param ResponseInterface $response |
19
|
|
|
*/ |
20
|
|
|
public function inspect(ResponseInterface $response) |
21
|
|
|
{ |
22
|
|
|
$this->filterCommonErrors($response); |
23
|
|
|
switch ($response->getStatusCode()) { |
24
|
|
|
case 200: |
25
|
|
|
// OK. |
26
|
|
|
break; |
27
|
|
|
default: |
28
|
|
|
// KO. |
29
|
|
|
throw $this->newBadRequestException($response); |
30
|
|
|
} |
31
|
|
|
$data = $this->decodeResponseBody($response); |
32
|
|
|
|
33
|
|
|
$links = []; |
34
|
|
|
foreach ($data['links'] as $link) { |
35
|
|
|
$links[] = new EventStreamViaPersistentSubscriptionFeedLink($link['uri'], $link['relation']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$events = []; |
39
|
|
View Code Duplication |
foreach ($data['entries'] as $entry) { |
|
|
|
|
40
|
|
|
$streamId = $entry['streamId']; |
41
|
|
|
|
42
|
|
|
$number = $entry['eventNumber']; |
43
|
|
|
$type = $entry['eventType']; |
44
|
|
|
$eventData = isset($entry['data']) ? $this->decodeData($entry['data']) : []; |
45
|
|
|
$eventMetadata = isset($entry['metaData']) ? $this->decodeData($entry['metaData']) : []; |
46
|
|
|
$events[] = new EventRecord($streamId, $number, $type, $eventData, $eventMetadata); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$isHeadOfStream = $data['headOfStream']; |
50
|
|
|
$this->feed = new EventStreamViaPersistentSubscriptionFeed($events, $links, $isHeadOfStream); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return EventStreamViaPersistentSubscriptionFeed |
55
|
|
|
*/ |
56
|
|
|
public function getFeed(): EventStreamViaPersistentSubscriptionFeed |
57
|
|
|
{ |
58
|
|
|
return $this->feed; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.