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
|
|
|
use Strider2038\JsonRpcClient\Response\ResponseObjectInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Can be used for low-level operations. Remote procedure results returned as response objects. |
21
|
|
|
* So client response data must be extracted from responses. Advantage of this type of client |
22
|
|
|
* is possibility to handle errors from server without catching exceptions. |
23
|
|
|
* |
24
|
|
|
* @author Igor Lazarev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class RawClient implements ClientInterface |
27
|
|
|
{ |
28
|
|
|
private RequestObjectFactory $requestObjectFactory; |
29
|
|
|
|
30
|
|
|
private Caller $caller; |
31
|
|
|
|
32
|
|
|
public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller) |
33
|
|
|
{ |
34
|
16 |
|
$this->requestObjectFactory = $requestObjectFactory; |
35
|
|
|
$this->caller = $caller; |
36
|
16 |
|
} |
37
|
16 |
|
|
38
|
16 |
|
public function batch(): BatchRequestInterface |
39
|
|
|
{ |
40
|
3 |
|
return new RawBatchRequester($this->requestObjectFactory, $this->caller); |
41
|
|
|
} |
42
|
3 |
|
|
43
|
|
|
/** |
44
|
|
|
* Calls remote procedure with given parameters. Server response is returned. |
45
|
|
|
* |
46
|
|
|
* @param array|object|null $params |
47
|
|
|
* |
48
|
|
|
* @throws JsonRpcClientExceptionInterface |
49
|
|
|
* |
50
|
|
|
* @return ResponseObjectInterface|ResponseObjectInterface[]|null |
51
|
|
|
*/ |
52
|
|
|
public function call(string $method, $params = null) |
53
|
|
|
{ |
54
|
8 |
|
$requestObject = $this->requestObjectFactory->createRequest($method, $params); |
55
|
|
|
|
56
|
8 |
|
return $this->caller->call($requestObject); |
57
|
|
|
} |
58
|
8 |
|
|
59
|
|
|
/** |
60
|
|
|
* @param array|object|null $params |
61
|
|
|
* |
62
|
|
|
* @throws JsonRpcClientExceptionInterface |
63
|
|
|
*/ |
64
|
|
|
public function notify(string $method, $params = null): void |
65
|
|
|
{ |
66
|
2 |
|
$notificationObject = $this->requestObjectFactory->createNotification($method, $params); |
67
|
|
|
|
68
|
2 |
|
$this->caller->call($notificationObject); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|