Completed
Pull Request — master (#7)
by Igor
04:25
created

ResponseObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 3
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\Response;
12
13
use Strider2038\JsonRpcClient\Exception\JsonRpcClientException;
14
15
/**
16
 * @author Igor Lazarev <[email protected]>
17
 */
18
class ResponseObject implements ResponseObjectInterface
19
{
20
    /** @var string */
21
    private $jsonrpc;
22
23
    /** @var mixed */
24
    private $result;
25
26
    /** @var ErrorObject|null */
27
    private $error;
28
29
    /** @var string|int|null */
30 16
    private $id;
31
32 16
    public function __construct(string $protocol, $result, $id)
33
    {
34
        $this->jsonrpc = $protocol;
35 6
        $this->result = $result;
36
        $this->id = $id;
37 6
    }
38
39
    public function getProtocol(): string
40 15
    {
41
        return $this->jsonrpc;
42 15
    }
43
44
    public function getId()
45 17
    {
46
        return $this->id;
47 17
    }
48
49
    public function getResult()
50 6
    {
51
        return $this->result;
52 6
    }
53
54
    public function hasError(): bool
55
    {
56
        return null !== $this->error;
57
    }
58
59
    public function getError(): ErrorObject
60
    {
61
        if ($this->error === null) {
62
            throw new JsonRpcClientException(
63
                'There is no error in response. Please, use hasError() method to check response for errors.'
64
            );
65
        }
66
67
        return $this->error;
68
    }
69
70
    public function setError(ErrorObject $error): void
71
    {
72
        $this->error = $error;
73
    }
74
}
75