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\Request\RequestObjectFactory; |
15
|
|
|
use Strider2038\JsonRpcClient\Request\RequestObjectInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Igor Lazarev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class RawBatchRequester implements BatchRequestInterface |
21
|
|
|
{ |
22
|
|
|
private RequestObjectFactory $requestObjectFactory; |
23
|
|
|
|
24
|
|
|
private Caller $caller; |
25
|
|
|
|
26
|
|
|
/** @var RequestObjectInterface[] */ |
27
|
|
|
private array $queue = []; |
28
|
|
|
|
29
|
|
|
public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller) |
30
|
|
|
{ |
31
|
18 |
|
$this->requestObjectFactory = $requestObjectFactory; |
32
|
|
|
$this->caller = $caller; |
33
|
18 |
|
} |
34
|
18 |
|
|
35
|
18 |
|
public function call(string $method, $params = null): BatchRequestInterface |
36
|
|
|
{ |
37
|
12 |
|
$this->queue[] = $this->requestObjectFactory->createRequest($method, $params); |
38
|
|
|
|
39
|
12 |
|
return $this; |
40
|
|
|
} |
41
|
12 |
|
|
42
|
|
|
public function notify(string $method, $params = null): BatchRequestInterface |
43
|
|
|
{ |
44
|
10 |
|
$this->queue[] = $this->requestObjectFactory->createNotification($method, $params); |
45
|
|
|
|
46
|
10 |
|
return $this; |
47
|
|
|
} |
48
|
10 |
|
|
49
|
|
|
public function send(): array |
50
|
|
|
{ |
51
|
16 |
|
$responses = []; |
52
|
|
|
|
53
|
16 |
|
if (count($this->queue) > 0) { |
54
|
|
|
$responses = $this->caller->call($this->queue); |
55
|
16 |
|
|
56
|
15 |
|
if (null === $responses) { |
57
|
|
|
$responses = []; |
58
|
15 |
|
} elseif (!is_array($responses)) { |
59
|
3 |
|
$responses = [$responses]; |
60
|
12 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
return $responses; |
64
|
|
|
} |
65
|
16 |
|
|
66
|
|
|
public function getQueue(): array |
67
|
|
|
{ |
68
|
10 |
|
return $this->queue; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|