Completed
Push — master ( 888209...3c23c4 )
by Pavel
02:26
created

JsonRpcClient.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
15
final class JsonRpcClient implements RpcClientInterface
16
{
17
    /** @var Client */
18
    private $client;
19
    /**
20
     * @var string
21
     */
22
    private $uri;
23
    /**
24
     * @var IdGeneratorInterface
25
     */
26
    private $idGenerator;
27
28
    /**
29
     * JsonRpcClient constructor.
30
     *
31
     * @param Client $client
32
     * @param string $uri
33
     */
34 1
    public function __construct(Client $client, $uri, IdGeneratorInterface $idGenerator = null)
35
    {
36 1
        $this->client      = $client;
37 1
        $this->uri         = $uri;
38 1
        $this->idGenerator = $idGenerator;
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function invoke($calls)
45
    {
46
        try {
47 1
            if (!is_array($calls) && ($calls instanceof RpcRequestInterface)) {
48
                $transformedCall = $this->transformCall($calls);
49
50
                $httpRequest = $this->createHttpRequest($transformedCall);
51
                $this->client->request(
52
                    $httpRequest->getMethod(),
53
                    $httpRequest->getUri(),
54
                    $httpRequest->getParameters(),
55
                    [],
56
                    $httpRequest->getServer(),
57
                    $httpRequest->getContent()
58
                );
59
60
                return new JsonRpcResponseCollection(
61
                    $this->client->getResponse(),
0 ignored issues
show
$this->client->getResponse() is of type object|null, but the function expects a object<Symfony\Component\BrowserKit\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
                    [new RequestTransformation($calls, $transformedCall)]
63
                );
64
            }
65
66 1
            $requests     = [];
67 1
            $batchRequest = [];
68
69 1
            foreach ($calls as $key => $call) {
70 1
                $transformedCall                  = $this->transformCall($call);
71 1
                $requests[spl_object_hash($call)] = new RequestTransformation($call, $transformedCall);
72 1
                $batchRequest[]                   = $transformedCall;
73 1
            }
74
75 1
            $this->client->restart();
76
77 1
            $httpRequest = $this->createHttpRequest($batchRequest);
78
79 1
            $this->client->request(
80 1
                $httpRequest->getMethod(),
81 1
                $httpRequest->getUri(),
82 1
                $httpRequest->getParameters(),
83 1
                [],
84 1
                $httpRequest->getServer(),
85 1
                $httpRequest->getContent()
86 1
            );
87
88 1
            return new JsonRpcResponseCollection(
89 1
                $this->client->getResponse(),
0 ignored issues
show
$this->client->getResponse() is of type object|null, but the function expects a object<Symfony\Component\BrowserKit\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
                $requests
91 1
            );
92
        } catch (\Exception $exception) {
93
            throw new RemoteCallFailedException($exception->getMessage(), 0, $exception);
94
        }
95
    }
96
97
    /**
98
     * @param $requestBody
99
     *
100
     * @return Request
101
     */
102 1
    private function createHttpRequest($requestBody)
103
    {
104 1
        return new Request(
105 1
            $this->uri,
106 1
            'POST',
107 1
            [],
108 1
            [],
109 1
            [],
110
            [
111 1
                'HTTP_CONTENT_TYPE' => 'application/json',
112 1
                'HTTP_ACCEPT'       => 'application/json',
113 1
            ],
114 1
            json_encode($requestBody, JSON_PRETTY_PRINT)
115 1
        );
116
    }
117
118
    /**
119
     * @param RpcRequestInterface $call
120
     *
121
     * @return JsonRpcRequest
122
     */
123 1
    private function transformCall(RpcRequestInterface $call)
124
    {
125 1
        $transformedCall = $call;
126 1
        if ($call instanceof RpcRequestInterface && !($call instanceof JsonRpcRequestInterface)) {
127
            $transformedCall = JsonRpcRequest::fromRpcRequest($call, $this->idGenerator->getRequestIdentifier($call));
128
        }
129
130 1
        return $transformedCall;
131
    }
132
}
133