Completed
Push — master ( c81a8a...8da93f )
by Pavel
02:13
created

JsonRpcClient::invoke()   B

Complexity

Conditions 5
Paths 20

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.8667

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 22
cts 38
cp 0.5789
rs 8.6868
c 0
b 0
f 0
cc 5
eloc 33
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 = null)
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
                    $httpRequest->getParameters(),
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
81 1
            $this->client->request(
82 1
                $httpRequest->getMethod(),
83 1
                $httpRequest->getUri(),
84 1
                $httpRequest->getParameters(),
85 1
                [],
86 1
                $httpRequest->getServer(),
87 1
                $httpRequest->getContent()
88 1
            );
89
90 1
            return new JsonRpcResponseCollection(
91 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...
92
                $requests
93 1
            );
94
        } catch (\Exception $exception) {
95
            throw new RemoteCallFailedException($exception->getMessage(), 0, $exception);
96
        }
97
    }
98
99
    /**
100
     * @param $requestBody
101
     *
102
     * @return Request
103
     */
104 1
    private function createHttpRequest($requestBody)
105
    {
106 1
        return new Request(
107 1
            $this->uri,
108 1
            'POST',
109 1
            [],
110 1
            [],
111 1
            [],
112
            [
113 1
                'HTTP_CONTENT_TYPE' => 'application/json',
114 1
                'HTTP_ACCEPT'       => 'application/json',
115 1
            ],
116 1
            json_encode($requestBody, JSON_PRETTY_PRINT)
117 1
        );
118
    }
119
120
    /**
121
     * @param RpcRequestInterface $call
122
     *
123
     * @return JsonRpcRequest
124
     */
125 1
    private function transformCall(RpcRequestInterface $call)
126
    {
127 1
        $transformedCall = $call;
128 1
        if ($call instanceof RpcRequestInterface && !($call instanceof JsonRpcRequestInterface)) {
129
            $transformedCall = JsonRpcRequest::fromRpcRequest($call, $this->idGenerator->getRequestIdentifier($call));
130
        }
131
132 1
        return $transformedCall;
133
    }
134
135
    /**
136
     * @return Response
137
     */
138 1
    private function checkResponse()
139
    {
140 1
        return $this->client->getInternalResponse();
141
    }
142
}
143