Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
01:57
created

JsonRpcResponse::setResult()   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
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServer\Domain\Model;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface;
5
6
/**
7
 * Class JsonRpcResponse
8
 */
9
class JsonRpcResponse
10
{
11
    const DEFAULT_VERSION = '2.0';
12
13
    /** @var string */
14
    private $jsonRpc;
15
    /** @var mixed */
16
    private $result = null;
17
    /** @var null|JsonRpcExceptionInterface */
18
    private $error = null;
19
    /** @var mixed */
20
    private $id = null;
21
    /** @var bool */
22
    private $isNotification = false;
23
24
    /**
25
     * @param string $jsonRpc
26
     */
27 11
    public function __construct(string $jsonRpc = self::DEFAULT_VERSION)
28
    {
29 11
        $this->jsonRpc = $jsonRpc;
30 11
    }
31
32
    /**
33
     * @param mixed $result
34
     *
35
     * @return JsonRpcResponse
36
     */
37 1
    public function setResult($result) : JsonRpcResponse
38
    {
39 1
        $this->result = $result;
40
41 1
        return $this;
42
    }
43
44
    /**
45
     * @param JsonRpcExceptionInterface $error
46
     *
47
     * @return JsonRpcResponse
48
     */
49 1
    public function setError(JsonRpcExceptionInterface $error) : JsonRpcResponse
50
    {
51 1
        $this->error = $error;
52
53 1
        return $this;
54
    }
55
56
    /**
57
     * @param mixed $id
58
     *
59
     * @return JsonRpcResponse
60
     */
61 7
    public function setId($id) : JsonRpcResponse
62
    {
63 7
        if (!is_string($id) && !is_int($id)) {
64 5
            throw new \InvalidArgumentException('Id must be either an int or a string');
65
        }
66
67 2
        $this->id = $id;
68
69 2
        return $this;
70
    }
71
72
    /**
73
     * @param bool $isNotification
74
     */
75 1
    public function setIsNotification(bool $isNotification) : JsonRpcResponse
76
    {
77 1
        $this->isNotification = $isNotification;
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * @return string
84
     */
85 1
    public function getJsonRpc() : string
86
    {
87 1
        return $this->jsonRpc;
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93 1
    public function getResult()
94
    {
95 1
        return $this->result;
96
    }
97
98
    /**
99
     * @return JsonRpcExceptionInterface|null
100
     */
101 1
    public function getError()
102
    {
103 1
        return $this->error;
104
    }
105
106
    /**
107
     * @return string|int|null
108
     */
109 2
    public function getId()
110
    {
111 2
        return $this->id;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117 1
    public function isNotification() : bool
118
    {
119 1
        return $this->isNotification;
120
    }
121
}
122