|
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; |
|
12
|
|
|
|
|
13
|
|
|
use Ramsey\Uuid\Uuid; |
|
14
|
|
|
use Strider2038\JsonRpcClient\Request\IdGeneratorInterface; |
|
15
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObjectFactory; |
|
16
|
|
|
use Strider2038\JsonRpcClient\Request\SequentialIntegerIdGenerator; |
|
17
|
|
|
use Strider2038\JsonRpcClient\Request\UuidGenerator; |
|
18
|
|
|
use Strider2038\JsonRpcClient\Response\ExceptionalResponseValidator; |
|
19
|
|
|
use Strider2038\JsonRpcClient\Response\NullResponseValidator; |
|
20
|
|
|
use Strider2038\JsonRpcClient\Serialization\JsonObjectSerializer; |
|
21
|
|
|
use Strider2038\JsonRpcClient\Serialization\MessageSerializerInterface; |
|
22
|
|
|
use Strider2038\JsonRpcClient\Service\Caller; |
|
23
|
|
|
use Strider2038\JsonRpcClient\Service\HighLevelClient; |
|
24
|
|
|
use Strider2038\JsonRpcClient\Service\LowLevelClient; |
|
25
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @experimental API may be changed |
|
29
|
|
|
* |
|
30
|
|
|
* @author Igor Lazarev <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class ClientBuilder |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* If enabled then HighLevelClient will be returned with response unpacking. |
|
36
|
|
|
* If disabled then LowLevelClient will be returned, that will return ResponseObjectInterface |
|
37
|
|
|
* for each request. |
|
38
|
|
|
* |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
private $enableResponseProcessing = true; |
|
42
|
|
|
|
|
43
|
|
|
/** @var TransportInterface */ |
|
44
|
|
|
private $transport; |
|
45
|
|
|
|
|
46
|
|
|
/** @var MessageSerializerInterface */ |
|
47
|
|
|
private $serializer; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IdGeneratorInterface */ |
|
50
|
|
|
private $idGenerator; |
|
51
|
|
|
|
|
52
|
17 |
|
public function __construct(TransportInterface $transport) |
|
53
|
|
|
{ |
|
54
|
17 |
|
$this->transport = $transport; |
|
55
|
17 |
|
$this->serializer = new JsonObjectSerializer(); |
|
56
|
|
|
|
|
57
|
17 |
|
if (class_exists(Uuid::class)) { |
|
58
|
17 |
|
$this->idGenerator = new UuidGenerator(); |
|
59
|
|
|
} else { |
|
60
|
|
|
$this->idGenerator = new SequentialIntegerIdGenerator(); |
|
61
|
|
|
} |
|
62
|
17 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function setSerializer(MessageSerializerInterface $serializer): self |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->serializer = $serializer; |
|
67
|
|
|
|
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function setIdGenerator(IdGeneratorInterface $idGenerator): self |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->idGenerator = $idGenerator; |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* If response processing is disabled then LowLevelClient will be constructed. |
|
80
|
|
|
* |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function disableResponseProcessing(): self |
|
84
|
|
|
{ |
|
85
|
2 |
|
$this->enableResponseProcessing = false; |
|
86
|
|
|
|
|
87
|
2 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
17 |
|
public function getClient(): ClientInterface |
|
91
|
|
|
{ |
|
92
|
17 |
|
$requestObjectFactory = $this->createRequestObjectFactory(); |
|
93
|
|
|
|
|
94
|
17 |
|
if ($this->enableResponseProcessing) { |
|
95
|
15 |
|
$caller = new Caller($this->serializer, $this->transport, new ExceptionalResponseValidator()); |
|
96
|
15 |
|
$client = new HighLevelClient($requestObjectFactory, $caller); |
|
97
|
|
|
} else { |
|
98
|
2 |
|
$caller = new Caller($this->serializer, $this->transport, new NullResponseValidator()); |
|
99
|
2 |
|
$client = new LowLevelClient($requestObjectFactory, $caller); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
17 |
|
return $client; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
17 |
|
private function createRequestObjectFactory(): RequestObjectFactory |
|
106
|
|
|
{ |
|
107
|
17 |
|
return new RequestObjectFactory($this->idGenerator); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|