|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObjectInterface; |
|
14
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseObjectInterface; |
|
15
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseValidatorInterface; |
|
16
|
|
|
use Strider2038\JsonRpcClient\Serialization\ContextGenerator; |
|
17
|
|
|
use Strider2038\JsonRpcClient\Serialization\MessageSerializerInterface; |
|
18
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
* |
|
23
|
|
|
* @author Igor Lazarev <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Caller |
|
26
|
|
|
{ |
|
27
|
|
|
private MessageSerializerInterface $serializer; |
|
28
|
|
|
|
|
29
|
|
|
private ContextGenerator $contextGenerator; |
|
30
|
|
|
|
|
31
|
|
|
private TransportInterface $transport; |
|
32
|
|
|
|
|
33
|
|
|
private ResponseValidatorInterface $validator; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
MessageSerializerInterface $serializer, |
|
37
|
|
|
ContextGenerator $contextGenerator, |
|
38
|
|
|
TransportInterface $transport, |
|
39
|
52 |
|
ResponseValidatorInterface $validator |
|
40
|
|
|
) { |
|
41
|
|
|
$this->serializer = $serializer; |
|
42
|
|
|
$this->contextGenerator = $contextGenerator; |
|
43
|
|
|
$this->transport = $transport; |
|
44
|
|
|
$this->validator = $validator; |
|
45
|
52 |
|
} |
|
46
|
52 |
|
|
|
47
|
52 |
|
/** |
|
48
|
52 |
|
* @param RequestObjectInterface|RequestObjectInterface[] $request |
|
49
|
52 |
|
* |
|
50
|
|
|
* @return ResponseObjectInterface|ResponseObjectInterface[]|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public function call($request) |
|
53
|
|
|
{ |
|
54
|
|
|
$serializedRequest = $this->serializer->serialize($request); |
|
55
|
|
|
$serializedResponse = $this->transport->send($serializedRequest); |
|
56
|
42 |
|
$context = $this->contextGenerator->createSerializationContext($request); |
|
57
|
|
|
$response = $this->serializer->deserialize($serializedResponse, $context); |
|
58
|
42 |
|
$this->validator->validate($response); |
|
59
|
42 |
|
|
|
60
|
37 |
|
return $response; |
|
61
|
37 |
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|