ResponseObject   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProtocol() 0 3 1
A setError() 0 3 1
A getResult() 0 3 1
A __construct() 0 5 1
A hasError() 0 3 1
A getError() 0 9 2
A getId() 0 3 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\LogicException;
14
15
/**
16
 * @author Igor Lazarev <[email protected]>
17
 */
18
class ResponseObject implements ResponseObjectInterface
19
{
20
    private string $jsonrpc;
21
22
    /** @var mixed */
23
    private $result;
24
25
    private ?ErrorObject $error = null;
26
27
    /** @var string|int|null */
28
    private $id;
29
30
    public function __construct(string $protocol, $result, $id)
31
    {
32 57
        $this->jsonrpc = $protocol;
33
        $this->result = $result;
34 57
        $this->id = $id;
35 57
    }
36 57
37 57
    public function getProtocol(): string
38
    {
39 45
        return $this->jsonrpc;
40
    }
41 45
42
    public function getId()
43
    {
44 26
        return $this->id;
45
    }
46 26
47
    public function getResult()
48
    {
49 45
        return $this->result;
50
    }
51 45
52
    public function hasError(): bool
53
    {
54 50
        return null !== $this->error;
55
    }
56 50
57
    public function getError(): ErrorObject
58
    {
59 20
        if (null === $this->error) {
60
            throw new LogicException(
61 20
                'There is no error in response. Please, use hasError() method to check response for errors.'
62 1
            );
63 1
        }
64
65
        return $this->error;
66
    }
67 19
68
    public function setError(ErrorObject $error): void
69
    {
70 19
        $this->error = $error;
71
    }
72
}
73