ReadEventStreamFeedResponseInspector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B inspect() 0 35 6
A getFeed() 0 4 1
1
<?php
2
3
namespace RayRutjes\GetEventStore\Client\Http;
4
5
use Psr\Http\Message\ResponseInterface;
6
use RayRutjes\GetEventStore\EventRecord;
7
use RayRutjes\GetEventStore\Client\Http\Feed\EventStreamFeed;
8
use RayRutjes\GetEventStore\Client\Http\Feed\EventStreamFeedLink;
9
10
class ReadEventStreamFeedResponseInspector extends AbstractResponseInspector
11
{
12
    /**
13
     * @var EventStreamFeed
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
        // Todo: Handle parsing exceptions and throw corresponding errors.
34
        $links = [];
35
        foreach ($data['links'] as $link) {
36
            $links[] = new EventStreamFeedLink($link['uri'], $link['relation']);
37
        }
38
39
        $events = [];
40
        foreach ($data['entries'] as $entry) {
41
            $streamId = $entry['streamId'];
42
43
            $number = $entry['eventNumber'];
44
            $type = $entry['eventType'];
45
            $eventData = isset($entry['data']) ? $this->decodeData($entry['data']) : [];
46
            $eventMetadata = isset($entry['metaData']) ? $this->decodeData($entry['metaData']) : [];
47
48
            $events[] = new EventRecord($streamId, $number, $type, $eventData, $eventMetadata);
49
        }
50
51
        $isHeadOfStream = $data['headOfStream'];
52
        $eTag = $data['eTag'] ?? null;
53
        $this->feed = new EventStreamFeed($events, $links, $isHeadOfStream, $eTag);
54
    }
55
56
    /**
57
     * @return EventStreamFeed
58
     */
59
    public function getFeed(): EventStreamFeed
60
    {
61
        return $this->feed;
62
    }
63
}
64