Completed
Push — master ( 358621...3c4e64 )
by Pavel
03:50
created

JsonRpcRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 10.98 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 62.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 1
dl 9
loc 82
ccs 20
cts 32
cp 0.625
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isNotification() 0 4 1
C fromStdClass() 0 25 7
A jsonSerialize() 9 9 1
A getVersion() 0 4 1
A getMethod() 0 4 1
A getParameters() 0 4 1
A getId() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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