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\BatchRequestInterface; |
14
|
|
|
use Strider2038\JsonRpcClient\ClientInterface; |
15
|
|
|
use Strider2038\JsonRpcClient\Exception\JsonRpcClientExceptionInterface; |
16
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObjectFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Can be used for high-level operations. Remote procedure results returned as response data. On server |
20
|
|
|
* errors it throws exceptions. It is recommended for use as reliable channel between client and server |
21
|
|
|
* when errors from server are not expected as normal behaviour. Advantage of this type of client over |
22
|
|
|
* raw client is automatic ordering of responses for batch requests. |
23
|
|
|
* |
24
|
|
|
* @author Igor Lazarev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ProcessingClient implements ClientInterface |
27
|
|
|
{ |
28
|
|
|
private RequestObjectFactory $requestObjectFactory; |
29
|
|
|
|
30
|
|
|
private Caller $caller; |
31
|
|
|
|
32
|
|
|
public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller) |
33
|
|
|
{ |
34
|
42 |
|
$this->requestObjectFactory = $requestObjectFactory; |
35
|
|
|
$this->caller = $caller; |
36
|
42 |
|
} |
37
|
42 |
|
|
38
|
42 |
|
public function batch(): BatchRequestInterface |
39
|
|
|
{ |
40
|
8 |
|
return new ProcessingBatchRequester($this->requestObjectFactory, $this->caller); |
41
|
|
|
} |
42
|
8 |
|
|
43
|
|
|
/** |
44
|
|
|
* @param array|object|null $params |
45
|
|
|
* |
46
|
|
|
* @throws JsonRpcClientExceptionInterface |
47
|
|
|
* |
48
|
|
|
* @return array|object|null |
49
|
|
|
*/ |
50
|
|
|
public function call(string $method, $params = null) |
51
|
|
|
{ |
52
|
25 |
|
$result = null; |
53
|
|
|
$requestObject = $this->requestObjectFactory->createRequest($method, $params); |
54
|
25 |
|
$responseObject = $this->caller->call($requestObject); |
55
|
25 |
|
|
56
|
25 |
|
if (null !== $responseObject) { |
57
|
|
|
$result = $responseObject->getResult(); |
58
|
14 |
|
} |
59
|
13 |
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
14 |
|
|
63
|
|
|
/** |
64
|
|
|
* @param array|object|null $params |
65
|
|
|
* |
66
|
|
|
* @throws JsonRpcClientExceptionInterface |
67
|
|
|
*/ |
68
|
|
|
public function notify(string $method, $params = null): void |
69
|
|
|
{ |
70
|
2 |
|
$notificationObject = $this->requestObjectFactory->createNotification($method, $params); |
71
|
|
|
|
72
|
2 |
|
$this->caller->call($notificationObject); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|