Passed
Push — master ( 101ad5...2f664d )
by Pavel
01:50
created

JsonRpcClient::invoke()   B

Complexity

Conditions 5
Paths 20

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.8667

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 22
cts 38
cp 0.5789
rs 8.6588
c 0
b 0
f 0
cc 5
eloc 32
nc 20
nop 1
crap 6.8667

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 Bankiru\Api\BrowserKit;
4
5
use ScayTrase\Api\IdGenerator\IdGeneratorInterface;
6
use ScayTrase\Api\JsonRpc\JsonRpcRequest;
7
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
8
use ScayTrase\Api\JsonRpc\RequestTransformation;
9
use ScayTrase\Api\Rpc\Exception\RemoteCallFailedException;
10
use ScayTrase\Api\Rpc\RpcClientInterface;
11
use ScayTrase\Api\Rpc\RpcRequestInterface;
12
use Symfony\Component\BrowserKit\Client;
13
use Symfony\Component\BrowserKit\Request;
14
use Symfony\Component\BrowserKit\Response;
15
16
final class JsonRpcClient implements RpcClientInterface
17
{
18
    /** @var Client */
19
    private $client;
20
    /**
21
     * @var string
22
     */
23
    private $uri;
24
    /**
25
     * @var IdGeneratorInterface
26
     */
27
    private $idGenerator;
28
29
    /**
30
     * JsonRpcClient constructor.
31
     *
32
     * @param Client               $client
33
     * @param string               $uri
34
     * @param IdGeneratorInterface $idGenerator
35
     */
36 1
    public function __construct(Client $client, $uri, IdGeneratorInterface $idGenerator)
37
    {
38 1
        $this->client      = $client;
39 1
        $this->uri         = $uri;
40 1
        $this->idGenerator = $idGenerator;
41 1
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function invoke($calls)
47
    {
48
        try {
49 1
            if (!is_array($calls) && ($calls instanceof RpcRequestInterface)) {
50
                $transformedCall = $this->transformCall($calls);
51
52
                $httpRequest = $this->createHttpRequest($transformedCall);
53
                $this->client->request(
54
                    $httpRequest->getMethod(),
55
                    $httpRequest->getUri(),
56
                    [],
57
                    [],
58
                    $httpRequest->getServer(),
59
                    $httpRequest->getContent()
60
                );
61
62
                return new JsonRpcResponseCollection(
63
                    $this->checkResponse(),
0 ignored issues
show
Bug introduced by
It seems like $this->checkResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
64
                    [new RequestTransformation($calls, $transformedCall)]
65
                );
66
            }
67
68 1
            $requests     = [];
69 1
            $batchRequest = [];
70
71 1
            foreach ((array)$calls as $key => $call) {
72 1
                $transformedCall                  = $this->transformCall($call);
73 1
                $requests[spl_object_hash($call)] = new RequestTransformation($call, $transformedCall);
74 1
                $batchRequest[]                   = $transformedCall;
75 1
            }
76
77 1
            $this->client->restart();
78
79 1
            $httpRequest = $this->createHttpRequest($batchRequest);
80 1
            $this->client->request(
81 1
                $httpRequest->getMethod(),
82 1
                $httpRequest->getUri(),
83 1
                $httpRequest->getParameters(),
84 1
                [],
85 1
                $httpRequest->getServer(),
86 1
                $httpRequest->getContent()
87 1
            );
88
89 1
            return new JsonRpcResponseCollection(
90 1
                $this->checkResponse(),
0 ignored issues
show
Bug introduced by
It seems like $this->checkResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
91
                $requests
92 1
            );
93
        } catch (\Exception $exception) {
94
            throw new RemoteCallFailedException($exception->getMessage(), 0, $exception);
95
        }
96
    }
97
98
    /**
99
     * @param $requestBody
100
     *
101
     * @return Request
102
     */
103 1
    private function createHttpRequest($requestBody)
104
    {
105 1
        return new Request(
106 1
            $this->uri,
107 1
            'POST',
108 1
            [],
109 1
            [],
110 1
            [],
111
            [
112 1
                'HTTP_CONTENT_TYPE' => 'application/json',
113 1
                'HTTP_ACCEPT'       => 'application/json',
114 1
            ],
115 1
            json_encode($requestBody, JSON_PRETTY_PRINT)
116 1
        );
117
    }
118
119
    /**
120
     * @param RpcRequestInterface $call
121
     *
122
     * @return JsonRpcRequest
123
     */
124 1
    private function transformCall(RpcRequestInterface $call)
125
    {
126 1
        $transformedCall = $call;
127 1
        if ($call instanceof RpcRequestInterface && !($call instanceof JsonRpcRequestInterface)) {
128
            $transformedCall = JsonRpcRequest::fromRpcRequest($call, $this->idGenerator->getRequestIdentifier($call));
129
        }
130
131 1
        return $transformedCall;
132
    }
133
134
    /**
135
     * @return Response
136
     */
137 1
    private function checkResponse()
138
    {
139 1
        return $this->client->getInternalResponse();
140
    }
141
}
142