Passed
Pull Request — master (#7)
by Igor
04:31
created

ProcessingClient::batch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 37
    public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller)
35
    {
36 37
        $this->requestObjectFactory = $requestObjectFactory;
37 37
        $this->caller = $caller;
38 37
    }
39
40 7
    public function batch(): BatchRequestInterface
41
    {
42 7
        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 21
    public function call(string $method, $params = null)
53
    {
54 21
        $result = null;
55 21
        $requestObject = $this->requestObjectFactory->createRequest($method, $params);
56 21
        $responseObject = $this->caller->call($requestObject);
57
58 12
        if (null !== $responseObject) {
59 11
            $result = $responseObject->getResult();
60
        }
61
62 12
        return $result;
63
    }
64
65
    /**
66
     * @param array|object|null $params
67
     *
68
     * @throws JsonRpcClientException
69
     */
70 2
    public function notify(string $method, $params = null): void
71
    {
72 2
        $notificationObject = $this->requestObjectFactory->createNotification($method, $params);
73
74 2
        $this->caller->call($notificationObject);
75 2
    }
76
}
77