|
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(), |
|
|
|
|
|
|
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(), |
|
|
|
|
|
|
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 null|Response |
|
137
|
|
|
* @throws RemoteCallFailedException |
|
138
|
|
|
*/ |
|
139
|
1 |
|
private function checkResponse() |
|
140
|
|
|
{ |
|
141
|
1 |
|
$response = $this->client->getResponse(); |
|
142
|
|
|
|
|
143
|
1 |
|
if (!$response instanceof Response) { |
|
144
|
|
|
throw new RemoteCallFailedException( |
|
145
|
|
|
'Your browser-kit client implementation does not returns ' . Response::class . ' instance' |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
return $response; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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: