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