Passed
Pull Request — master (#17)
by Pavel
04:41
created

JsonRpcRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 86
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isNotification() 0 4 1
A getId() 0 4 1
A getMethod() 0 4 1
A getParameters() 0 4 1
A jsonSerialize() 0 14 2
A getVersion() 0 4 1
A fromRpcRequest() 0 4 1
1
<?php
2
3
namespace ScayTrase\Api\JsonRpc;
4
5
use ScayTrase\Api\Rpc\RpcRequestInterface;
6
7
final class JsonRpcRequest implements JsonRpcRequestInterface
8
{
9
    /** @var  string */
10
    private $id;
11
    /** @var  string */
12
    private $method;
13
    /** @var  \stdClass|array|null */
14
    private $parameters;
15
16
    /**
17
     * JsonRpcRequest constructor.
18
     * @param string $method
19
     * @param \stdClass|array|null $parameters
20
     * @param string $id
21
     */
22 16
    public function __construct($method, $parameters = null, $id = null)
23
    {
24 16
        $this->method = $method;
25 16
        $this->parameters = $parameters;
26 16
        $this->id = $id;
27 16
    }
28
29
    /**
30
     * @param RpcRequestInterface $request
31
     * @param string $id
32
     * @return static
33
     */
34 1
    public static function fromRpcRequest(RpcRequestInterface $request, $id)
35
    {
36 1
        return new static($request->getMethod(), $request->getParameters(), $id);
37
    }
38
39
    /** {@inheritdoc} */
40 16
    public function isNotification()
41
    {
42 16
        return false;
43
    }
44
45
    /** {@inheritdoc} */
46 16
    public function getId()
47
    {
48 16
        return $this->id;
49
    }
50
51
    /** {@inheritdoc} */
52 16
    public function getMethod()
53
    {
54 16
        return $this->method;
55
    }
56
57
    /** {@inheritdoc} */
58 16
    public function getParameters()
59
    {
60 16
        return $this->parameters;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 16
    public function jsonSerialize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $result = [
69 16
            self::VERSION_FIELD => JsonRpcClient::VERSION,
70 16
            self::METHOD_FIELD => $this->getMethod(),
71 16
            self::PARAMETERS_FIELD => $this->getParameters(),
72 16
        ];
73
74 16
        if (!$this->isNotification()) {
75 16
            $result[self::ID_FIELD] = $this->getId();
76 16
        }
77
78 16
        return $result;
79
    }
80
81
    /**
82
     * Returns version of the JSON-RPC request
83
     *
84
     * A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
85
     *
86
     * @return string JSON-RPC version
87
     */
88
    public function getVersion()
89
    {
90
        return JsonRpcClient::VERSION;
91
    }
92
}
93