Completed
Push — master ( a7ee47...bc94bf )
by Pavel
04:48
created

JsonRpcRequest::fromStdClass()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 18
nop 1
crap 7.0119
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Specification;
4
5
use Bankiru\Api\JsonRpc\Exception\InvalidRequestException;
6
use Bankiru\Api\JsonRpc\Exception\JsonRpcException;
7
use Bankiru\Api\JsonRpc\JsonRpcBundle;
8
use ScayTrase\Api\JsonRpc\JsonRpcError;
9
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
10
11
final class JsonRpcRequest implements JsonRpcRequestInterface
12
{
13
    /** @var  string|null */
14
    private $id;
15
    /** @var  string */
16
    private $method;
17
    /** @var  mixed|\stdClass */
18
    private $parameters;
19
20
    /** @return bool True if request should not receive response from the server */
21 7
    public function isNotification()
22
    {
23 7
        return null === $this->id;
24
    }
25
26
    /**
27
     * @param \stdClass $source
28
     *
29
     * @return JsonRpcRequestInterface
30
     * @throws JsonRpcException
31
     */
32 10
    public static function fromStdClass(\stdClass $source)
33
    {
34 10
        $request = new static;
35
36 10
        $missing = [];
37
38 10
        foreach (['method', 'jsonrpc'] as $field) {
39 10
            if (!isset($source->$field)) {
40 3
                $missing[] = $field;
41 3
            }
42 10
        }
43 10
        if (count($missing) > 0) {
44 3
            throw InvalidRequestException::missingFields($missing);
45
        }
46
47 7
        if (JsonRpcBundle::VERSION !== $source->jsonrpc) {
48
            throw InvalidRequestException::invalidVersion(JsonRpcBundle::VERSION, $source->jsonrpc);
49
        }
50
51 7
        $request->id         = isset($source->id) ? $source->id : null;
52 7
        $request->method     = $source->method;
53 7
        $request->parameters = isset($source->params) ? json_decode(json_encode($source->params), true) : null;
54
55 7
        return $request;
56
    }
57
58
    /** {@inheritdoc} */
59 View Code Duplication
    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...
60
    {
61
        return [
62
            self::VERSION_FIELD    => $this->getVersion(),
63
            self::METHOD_FIELD     => $this->getMethod(),
64
            self::PARAMETERS_FIELD => $this->getParameters(),
65
            self::ID_FIELD         => $this->getId(),
66
        ];
67
    }
68
69
    /** {@inheritdoc} */
70
    public function getVersion()
71
    {
72
        return JsonRpcBundle::VERSION;
73
    }
74
75
    /** @return string */
76 7
    public function getMethod()
77
    {
78 7
        return $this->method;
79
    }
80
81
    /** @return array */
82 6
    public function getParameters()
83
    {
84 6
        return $this->parameters;
85
    }
86
87
    /** @return int|null Id. if not a notification and id is not set - id should be automatically generated */
88 7
    public function getId()
89
    {
90 7
        return $this->id;
91
    }
92
}
93