Passed
Pull Request — master (#11)
by Pavel
06:35
created

JsonRpcRequest::fromStdClass()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.457

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 19
cp 0.7895
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 18
nop 1
crap 7.457
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Specification;
4
5
use Bankiru\Api\JsonRpc\BankiruJsonRpcServerBundle;
6
use Bankiru\Api\JsonRpc\Exception\InvalidRequestException;
7
use Bankiru\Api\JsonRpc\Exception\JsonRpcException;
8
use ScayTrase\Api\JsonRpc\JsonRpcRequestInterface;
9
10
/** @internal */
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
    /**
21
     * @param \stdClass $source
22
     *
23
     * @return JsonRpcRequestInterface
24
     * @throws JsonRpcException
25
     */
26 19
    public static function fromStdClass(\stdClass $source)
27
    {
28 19
        $request = new static;
29
30 19
        $missing = [];
31
32 19
        foreach (['method', 'jsonrpc'] as $field) {
33 19
            if (!isset($source->$field)) {
34 6
                $missing[] = $field;
35 6
            }
36 19
        }
37 19
        if (count($missing) > 0) {
38 6
            throw InvalidRequestException::missingFields($missing);
39
        }
40
41 13
        if (BankiruJsonRpcServerBundle::JSONRPC_VERSION !== $source->jsonrpc) {
42
            throw InvalidRequestException::invalidVersion(
43
                BankiruJsonRpcServerBundle::JSONRPC_VERSION,
44
                $source->jsonrpc
45
            );
46
        }
47
48 13
        $request->id         = isset($source->id) ? $source->id : null;
49 13
        $request->method     = $source->method;
50 13
        $request->parameters = isset($source->params) ? json_decode(json_encode($source->params), true) : null;
51
52 13
        return $request;
53
    }
54
55
    /** {@inheritdoc} */
56 13
    public function isNotification()
57
    {
58 13
        return null === $this->id;
59
    }
60
61
    /** {@inheritdoc} */
62 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...
63
    {
64
        return [
65
            self::VERSION_FIELD    => $this->getVersion(),
66
            self::METHOD_FIELD     => $this->getMethod(),
67
            self::PARAMETERS_FIELD => $this->getParameters(),
68
            self::ID_FIELD         => $this->getId(),
69
        ];
70
    }
71
72
    /** {@inheritdoc} */
73
    public function getVersion()
74
    {
75
        return BankiruJsonRpcServerBundle::JSONRPC_VERSION;
76
    }
77
78
    /** @return string */
79 13
    public function getMethod()
80
    {
81 13
        return $this->method;
82
    }
83
84
    /** {@inheritdoc} */
85 6
    public function getParameters()
86
    {
87 6
        return $this->parameters;
88
    }
89
90
    /** {@inheritdoc} */
91 13
    public function getId()
92
    {
93 13
        return $this->id;
94
    }
95
}
96