1111use GuzzleHttp \Exception \GuzzleException ;
1212use GuzzleHttp \Psr7 \Request ;
1313use Psr \Http \Message \UriInterface ;
14+ use Psr \Log \LoggerInterface ;
15+ use Psr \Log \NullLogger ;
1416use ScayTrase \Api \IdGenerator \IdGeneratorInterface ;
1517use ScayTrase \Api \IdGenerator \UuidGenerator ;
1618use ScayTrase \Api \Rpc \Exception \RemoteCallFailedException ;
@@ -29,44 +31,76 @@ final class JsonRpcClient implements RpcClientInterface
2931 private $ uri ;
3032 /** @var IdGeneratorInterface */
3133 private $ idGenerator ;
34+ /** @var LoggerInterface */
35+ private $ logger ;
3236
3337 /**
3438 * JsonRpcClient constructor.
3539 * @param ClientInterface $client
3640 * @param UriInterface $endpoint
3741 * @param IdGeneratorInterface|null $idGenerator
42+ * @param LoggerInterface $logger
3843 */
39- public function __construct (ClientInterface $ client , UriInterface $ endpoint , IdGeneratorInterface $ idGenerator = null )
44+ public function __construct (
45+ ClientInterface $ client ,
46+ UriInterface $ endpoint ,
47+ IdGeneratorInterface $ idGenerator = null ,
48+ LoggerInterface $ logger = null
49+ )
4050 {
4151 $ this ->client = $ client ;
4252 $ this ->uri = $ endpoint ;
4353 $ this ->idGenerator = $ idGenerator ;
54+ $ this ->logger = $ logger ;
4455
4556 if (null === $ this ->idGenerator ) {
4657 $ this ->idGenerator = new UuidGenerator ();
4758 }
59+
60+ if (null === $ this ->logger ) {
61+ $ this ->logger = new NullLogger ();
62+ }
4863 }
4964
5065 /**
5166 * {@inheritdoc}
5267 */
5368 public function invoke ($ calls )
5469 {
55- if (!is_array ($ calls ) && ($ calls instanceof RpcRequestInterface)) {
56- $ calls = [$ calls ];
57- }
70+ try {
71+ if (!is_array ($ calls ) && ($ calls instanceof RpcRequestInterface)) {
72+ $ transformedCall = $ this ->transformCall ($ calls );
73+ return new JsonRpcResponseCollection (
74+ $ this ->client ->sendAsync (
75+ $ this ->createHttpRequest ($ transformedCall )
76+ ),
77+ [new RequestTransformation ($ calls , $ transformedCall )]
78+ );
79+ }
80+
81+ $ requests = [];
82+ $ batchRequest = [];
5883
59- $ requests = [];
60- $ requestBody = [];
84+ foreach ($ calls as $ key => $ call ) {
85+ $ transformedCall = $ this ->transformCall ($ call );
86+ $ requests [spl_object_hash ($ call )] = new RequestTransformation ($ call , $ transformedCall );
87+ $ batchRequest [] = $ transformedCall ;
88+ }
6189
62- foreach ($ calls as $ key => $ call ) {
63- $ transformedCall = $ this ->transformCall ($ call );
64- $ requests [spl_object_hash ($ call )] = new RequestTransformation ($ call , $ transformedCall );
65- $ requestBody [] = $ this ->formatJsonRpcCall ($ transformedCall );
90+ return new JsonRpcResponseCollection ($ this ->client ->sendAsync ($ this ->createHttpRequest ($ batchRequest )), $ requests );
91+ } catch (GuzzleException $ exception ) {
92+ throw new RemoteCallFailedException ($ exception ->getMessage (), 0 , $ exception );
6693 }
94+ }
6795
96+ /**
97+ * @param $requestBody
98+ * @return Request
99+ */
100+ private function createHttpRequest ($ requestBody )
101+ {
68102 /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
69- $ request = new Request (
103+ return new Request (
70104 'POST ' ,
71105 $ this ->uri ,
72106 [
@@ -75,12 +109,6 @@ public function invoke($calls)
75109 ],
76110 json_encode ($ requestBody , JSON_PRETTY_PRINT )
77111 );
78-
79- try {
80- return new JsonRpcResponseCollection ($ this ->client ->sendAsync ($ request ), $ requests );
81- } catch (GuzzleException $ exception ) {
82- throw new RemoteCallFailedException ($ exception ->getMessage (), 0 , $ exception );
83- }
84112 }
85113
86114 /**
@@ -96,23 +124,4 @@ private function transformCall(RpcRequestInterface $call)
96124 }
97125 return $ transformedCall ;
98126 }
99-
100- /**
101- * @param JsonRpcRequestInterface|RpcRequestInterface $request
102- * @return array
103- */
104- private function formatJsonRpcCall (JsonRpcRequestInterface $ request )
105- {
106- $ result = [
107- 'jsonrpc ' => static ::VERSION ,
108- 'method ' => $ request ->getMethod (),
109- 'params ' => $ request ->getParameters (),
110- ];
111-
112- if (!$ request ->isNotification ()) {
113- $ result ['id ' ] = $ request ->getId ();
114- }
115-
116- return $ result ;
117- }
118127}
0 commit comments