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

RawClient::notify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
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
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
    /** @var RequestObjectFactory */
29
    private $requestObjectFactory;
30
31
    /** @var Caller */
32
    private $caller;
33
34 14
    public function __construct(RequestObjectFactory $requestObjectFactory, Caller $caller)
35
    {
36 14
        $this->requestObjectFactory = $requestObjectFactory;
37 14
        $this->caller = $caller;
38 14
    }
39
40 3
    public function batch(): BatchRequestInterface
41
    {
42 3
        return new RawBatchRequester($this->requestObjectFactory, $this->caller);
43
    }
44
45
    /**
46
     * Calls remote procedure with given parameters. Server response is returned.
47
     *
48
     * @param array|object|null $params
49
     *
50
     * @throws JsonRpcClientException
51
     *
52
     * @return ResponseObjectInterface
53
     */
54 7
    public function call(string $method, $params = null)
55
    {
56 7
        $requestObject = $this->requestObjectFactory->createRequest($method, $params);
57
58 7
        return $this->caller->call($requestObject);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->caller->call($requestObject) also could return the type Strider2038\JsonRpcClien...sponseObjectInterface[] which is incompatible with the documented return type Strider2038\JsonRpcClien...ResponseObjectInterface.
Loading history...
59
    }
60
61
    /**
62
     * @param array|object|null $params
63
     *
64
     * @throws JsonRpcClientException
65
     */
66 2
    public function notify(string $method, $params = null): void
67
    {
68 2
        $notificationObject = $this->requestObjectFactory->createNotification($method, $params);
69
70 2
        $this->caller->call($notificationObject);
71 2
    }
72
}
73