Completed
Pull Request — master (#1)
by Raymond
02:20
created

ReadEventStreamViaPersistentSubscriptionResponseInspector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 7
lcom 1
cbo 6
dl 9
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B inspect() 9 32 6
A getFeed() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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