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

inspect()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 52
rs 7.2396
cc 7
eloc 35
nc 17
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace RayRutjes\GetEventStore\Client\Http;
4
5
use Psr\Http\Message\ResponseInterface;
6
use RayRutjes\GetEventStore\Client\Http\Feed\PersistentSubscriptionInfoFeed;
7
use RayRutjes\GetEventStore\Client\Http\Feed\PersistentSubscriptionInfoFeedLink;
8
use RayRutjes\GetEventStore\PersistentSubscriptionInfo;
9
use RayRutjes\GetEventStore\PersistentSubscriptionSettings;
10
use RayRutjes\GetEventStore\StreamId;
11
12
final class GetPersistentSubscriptionInfoResponseInspector extends AbstractResponseInspector
13
{
14
    /**
15
     * @var PersistentSubscriptionInfoFeed
16
     */
17
    private $feed;
18
19
    /**
20
     * @param ResponseInterface $response
21
     */
22
    public function inspect(ResponseInterface $response)
23
    {
24
        $this->filterCommonErrors($response);
25
        switch ($response->getStatusCode()) {
26
            case 200:
27
                // OK.
28
                break;
29
            default:
30
                // KO.
31
                throw $this->newBadRequestException($response);
32
        }
33
        $data = $this->decodeResponseBody($response);
34
35
        $links = [];
36
        foreach ($data['links'] as $link) {
37
            $links[] = new PersistentSubscriptionInfoFeedLink($link['href'], $link['rel']);
38
        }
39
40
        $settings = new PersistentSubscriptionSettings();
41
        $config = $data['config'];
42
43
        if ($config['resolveLinktos']) {
44
            $settings->resolveLinktos();
45
        } else {
46
            $settings->doNotResolveLinktos();
47
        }
48
        $settings->startFrom($config['startFrom']);
49
        $settings->withMessageTimeoutInMillisecondsOf($config['messageTimeoutMilliseconds']);
50
        if ($config['extraStatistics']) {
51
            $settings->withExtraStatistics();
52
        }
53
        $settings->withMaxRetriesOf($config['maxRetryCount']);
54
        $settings->WithReadBatchOf($config['readBatchSize']);
55
56
        // todo: not sure of how preferRoundRobin relates to namedConsumerStrategy.
57
        if ($config['preferRoundRobin'] || $config['namedConsumerStrategy'] == PersistentSubscriptionSettings::STRATEGY_ROUND_ROBIN) {
58
            $settings->preferRoundRobin();
59
        } else {
60
            $settings->preferDispatchToSingle();
61
        }
62
        $settings->checkPointAfter($config['checkPointAfterMilliseconds']);
63
        $settings->minCheckPointOf($config['minCheckPointCount']);
64
        $settings->maxCheckPointOf($config['maxCheckPointCount']);
65
        $settings->withMaxSubscribersOf($config['maxSubscriberCount']);
66
67
        $streamId = new StreamId($data['eventStreamId']);
68
        $groupName = $data['groupName'];
69
70
        $info = new PersistentSubscriptionInfo($streamId, $groupName, $settings);
71
72
        $this->feed = new PersistentSubscriptionInfoFeed($links, $info);
73
    }
74
75
    /**
76
     * @return PersistentSubscriptionInfoFeed
77
     */
78
    public function getFeed(): PersistentSubscriptionInfoFeed
79
    {
80
        return $this->feed;
81
    }
82
}
83