|
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\JsonRpcClientException; |
|
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
|
|
|
/** @var RequestObjectFactory */ |
|
29
|
|
|
private $requestObjectFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** @var Caller */ |
|
32
|
|
|
private $caller; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->requestObjectFactory = $requestObjectFactory; |
|
37
|
|
|
$this->caller = $caller; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function batch(): BatchRequestInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return new ProcessingBatchRequester($this->requestObjectFactory, $this->caller); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array|object|null $params |
|
47
|
|
|
* |
|
48
|
|
|
* @throws JsonRpcClientException |
|
49
|
|
|
* |
|
50
|
|
|
* @return array|object|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public function call(string $method, $params = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$result = null; |
|
55
|
|
|
$requestObject = $this->requestObjectFactory->createRequest($method, $params); |
|
56
|
|
|
$responseObject = $this->caller->call($requestObject); |
|
57
|
|
|
|
|
58
|
|
|
if (null !== $responseObject) { |
|
59
|
|
|
$result = $responseObject->getResult(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array|object|null $params |
|
67
|
|
|
* |
|
68
|
|
|
* @throws JsonRpcClientException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function notify(string $method, $params = null): void |
|
71
|
|
|
{ |
|
72
|
|
|
$notificationObject = $this->requestObjectFactory->createNotification($method, $params); |
|
73
|
|
|
|
|
74
|
|
|
$this->caller->call($notificationObject); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|