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\ContextGenerator; |
21
|
|
|
use Strider2038\JsonRpcClient\Serialization\JsonObjectSerializer; |
22
|
|
|
use Strider2038\JsonRpcClient\Serialization\MessageSerializerInterface; |
23
|
|
|
use Strider2038\JsonRpcClient\Service\Caller; |
24
|
|
|
use Strider2038\JsonRpcClient\Service\ProcessingClient; |
25
|
|
|
use Strider2038\JsonRpcClient\Service\RawClient; |
26
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @experimental API may be changed |
30
|
|
|
* |
31
|
|
|
* @author Igor Lazarev <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class ClientBuilder |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* If enabled then ProcessingClient will be returned with response unpacking. |
37
|
|
|
* If disabled then RawClient will be returned, that will return ResponseObjectInterface |
38
|
|
|
* for each request. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $enableResponseProcessing = true; |
43
|
|
|
|
44
|
|
|
/** @var TransportInterface */ |
45
|
|
|
private $transport; |
46
|
|
|
|
47
|
|
|
/** @var MessageSerializerInterface */ |
48
|
|
|
private $serializer; |
49
|
|
|
|
50
|
|
|
/** @var IdGeneratorInterface */ |
51
|
|
|
private $idGenerator; |
52
|
|
|
|
53
|
|
|
/** @var string[] */ |
54
|
|
|
private $resultTypesByMethods = []; |
55
|
|
|
|
56
|
|
|
/** @var string|null */ |
57
|
|
|
private $defaultErrorType = null; |
58
|
|
|
|
59
|
|
|
/** @var string[] */ |
60
|
|
|
private $errorTypesByMethods = []; |
61
|
|
|
|
62
|
51 |
|
public function __construct(TransportInterface $transport) |
63
|
|
|
{ |
64
|
51 |
|
$this->transport = $transport; |
65
|
51 |
|
$this->serializer = new JsonObjectSerializer(); |
66
|
|
|
|
67
|
51 |
|
if (class_exists(Uuid::class)) { |
68
|
51 |
|
$this->idGenerator = new UuidGenerator(); |
69
|
|
|
} else { |
70
|
|
|
$this->idGenerator = new SequentialIntegerIdGenerator(); |
71
|
|
|
} |
72
|
51 |
|
} |
73
|
|
|
|
74
|
32 |
|
public function setSerializer(MessageSerializerInterface $serializer): self |
75
|
|
|
{ |
76
|
32 |
|
$this->serializer = $serializer; |
77
|
|
|
|
78
|
32 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
public function setIdGenerator(IdGeneratorInterface $idGenerator): self |
82
|
|
|
{ |
83
|
11 |
|
$this->idGenerator = $idGenerator; |
84
|
|
|
|
85
|
11 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* If response processing is enabled then ProcessingClient will be constructed. |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
26 |
|
public function enableResponseProcessing(): self |
94
|
|
|
{ |
95
|
26 |
|
$this->enableResponseProcessing = true; |
96
|
|
|
|
97
|
26 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* If response processing is disabled then RawClient will be constructed. |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
13 |
|
public function disableResponseProcessing(): self |
106
|
|
|
{ |
107
|
13 |
|
$this->enableResponseProcessing = false; |
108
|
|
|
|
109
|
13 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
31 |
|
public function setResultTypesByMethods(array $resultTypesByMethods): self |
113
|
|
|
{ |
114
|
31 |
|
$this->resultTypesByMethods = $resultTypesByMethods; |
115
|
|
|
|
116
|
31 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
31 |
|
public function setDefaultErrorType(?string $defaultErrorType): self |
120
|
|
|
{ |
121
|
31 |
|
$this->defaultErrorType = $defaultErrorType; |
122
|
|
|
|
123
|
31 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
public function setErrorTypesByMethods(array $errorTypesByMethods): void |
127
|
|
|
{ |
128
|
4 |
|
$this->errorTypesByMethods = $errorTypesByMethods; |
129
|
4 |
|
} |
130
|
|
|
|
131
|
51 |
|
public function getClient(): ClientInterface |
132
|
|
|
{ |
133
|
51 |
|
$requestObjectFactory = $this->createRequestObjectFactory(); |
134
|
51 |
|
$contextGenerator = new ContextGenerator($this->resultTypesByMethods, $this->defaultErrorType, $this->errorTypesByMethods); |
135
|
|
|
|
136
|
51 |
|
if ($this->enableResponseProcessing) { |
137
|
38 |
|
$caller = new Caller($this->serializer, $contextGenerator, $this->transport, new ExceptionalResponseValidator()); |
138
|
38 |
|
$client = new ProcessingClient($requestObjectFactory, $caller); |
139
|
|
|
} else { |
140
|
13 |
|
$caller = new Caller($this->serializer, $contextGenerator, $this->transport, new NullResponseValidator()); |
141
|
13 |
|
$client = new RawClient($requestObjectFactory, $caller); |
142
|
|
|
} |
143
|
|
|
|
144
|
51 |
|
return $client; |
145
|
|
|
} |
146
|
|
|
|
147
|
51 |
|
private function createRequestObjectFactory(): RequestObjectFactory |
148
|
|
|
{ |
149
|
51 |
|
return new RequestObjectFactory($this->idGenerator); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|