Completed
Pull Request — master (#7)
by Igor
07:53 queued 03:25
created

RawBatchRequester::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 4
nop 0
crap 4
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