forked from bankiru/browserkit-jsonrpc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcClient.php
More file actions
141 lines (125 loc) · 4.01 KB
/
JsonRpcClient.php
File metadata and controls
141 lines (125 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Bankiru\Api\BrowserKit;
use ScayTrase\Api\IdGenerator\IdGeneratorInterface;
use ScayTrase\Api\JsonRpc\JsonRpcRequest;
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
use ScayTrase\Api\JsonRpc\RequestTransformation;
use ScayTrase\Api\Rpc\Exception\RemoteCallFailedException;
use ScayTrase\Api\Rpc\RpcClientInterface;
use ScayTrase\Api\Rpc\RpcRequestInterface;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Request;
use Symfony\Component\BrowserKit\Response;
final class JsonRpcClient implements RpcClientInterface
{
/** @var Client */
private $client;
/**
* @var string
*/
private $uri;
/**
* @var IdGeneratorInterface
*/
private $idGenerator;
/**
* JsonRpcClient constructor.
*
* @param Client $client
* @param string $uri
* @param IdGeneratorInterface $idGenerator
*/
public function __construct(Client $client, $uri, IdGeneratorInterface $idGenerator)
{
$this->client = $client;
$this->uri = $uri;
$this->idGenerator = $idGenerator;
}
/**
* {@inheritdoc}
*/
public function invoke($calls)
{
try {
if (!is_array($calls) && ($calls instanceof RpcRequestInterface)) {
$transformedCall = $this->transformCall($calls);
$httpRequest = $this->createHttpRequest($transformedCall);
$this->client->request(
$httpRequest->getMethod(),
$httpRequest->getUri(),
[],
[],
$httpRequest->getServer(),
$httpRequest->getContent()
);
return new JsonRpcResponseCollection(
$this->checkResponse(),
[new RequestTransformation($calls, $transformedCall)]
);
}
$requests = [];
$batchRequest = [];
foreach ((array)$calls as $key => $call) {
$transformedCall = $this->transformCall($call);
$requests[spl_object_hash($call)] = new RequestTransformation($call, $transformedCall);
$batchRequest[] = $transformedCall;
}
$this->client->restart();
$httpRequest = $this->createHttpRequest($batchRequest);
$this->client->request(
$httpRequest->getMethod(),
$httpRequest->getUri(),
$httpRequest->getParameters(),
[],
$httpRequest->getServer(),
$httpRequest->getContent()
);
return new JsonRpcResponseCollection(
$this->checkResponse(),
$requests
);
} catch (\Exception $exception) {
throw new RemoteCallFailedException($exception->getMessage(), 0, $exception);
}
}
/**
* @param $requestBody
*
* @return Request
*/
private function createHttpRequest($requestBody)
{
return new Request(
$this->uri,
'POST',
[],
[],
[],
[
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_ACCEPT' => 'application/json',
],
json_encode($requestBody, JSON_PRETTY_PRINT)
);
}
/**
* @param RpcRequestInterface $call
*
* @return JsonRpcRequest
*/
private function transformCall(RpcRequestInterface $call)
{
$transformedCall = $call;
if ($call instanceof RpcRequestInterface && !($call instanceof JsonRpcRequestInterface)) {
$transformedCall = JsonRpcRequest::fromRpcRequest($call, $this->idGenerator->getRequestIdentifier($call));
}
return $transformedCall;
}
/**
* @return Response
*/
private function checkResponse()
{
return $this->client->getInternalResponse();
}
}