Completed
Push — master ( 371f7d...1c3c02 )
by Daniel
03:19
created

RpcRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 106
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getId() 0 4 1
A setId() 0 4 1
A getMethod() 0 4 1
A setMethod() 0 4 1
A getVersion() 0 4 1
A get() 0 4 1
A toArray() 0 11 1
A fromArray() 0 9 1
A validate() 0 10 4
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Request;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcInvalidRequestException;
6
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
7
8
9
class RpcRequest implements RpcRequestInterface
10
{
11
    const VERSION = '2.0';
12
13
    public $id;
14
    public $method;
15
    public $attributes;
16
17
    public function __construct($id = null, $method = null, array $attributes = [])
18
    {
19
        $this->id = $id;
20
        $this->method = $method;
21
        $this->attributes = new ParameterBag($attributes);
22
    }
23
24
    /**
25
     * @return int|string
26
     */
27
    public function getId()
28
    {
29
        return $this->id;
30
    }
31
32
    /**
33
     * @param int|string $id
34
     */
35
    public function setId($id)
36
    {
37
        $this->id = $id;
38
    }
39
40
41
    /**
42
     * @return string
43
     */
44
    public function getMethod()
45
    {
46
        return $this->method;
47
    }
48
49
    /**
50
     * @param string $method
51
     */
52
    public function setMethod($method)
53
    {
54
        $this->method = $method;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getVersion()
61
    {
62
        return self::VERSION;
63
    }
64
65
    /**
66
     * @param $key
67
     * @return mixed
68
     */
69
    public function get($key)
70
    {
71
        return $this->attributes->get($key);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function toArray()
78
    {
79
        $rpc = [
80
            'id' => $this->id,
81
            'jsonrpc' => self::VERSION,
82
            'method' => $this->method,
83
            'params' => $this->attributes->all()
84
        ];
85
86
        return $rpc;
87
    }
88
89
    /**
90
     * @param array $request
91
     * @return $this
92
     * @throws RpcInvalidRequestException
93
     */
94
    public function fromArray(array $request)
95
    {
96
        $this->validate($request);
97
        $this->id = $request['id'];
98
        $this->method = $request['method'];
99
        $this->attributes = new ParameterBag($request['params']);
100
101
        return $this;
102
    }
103
104
    public function validate(array $request)
105
    {
106
        if (
107
            !array_key_exists(['id', 'jsonrpc', 'method', 'params'], $request)
108
            || self::VERSION !== $request['jsonrpc']
109
            || !is_array($request['params'])
110
        ) {
111
            throw new RpcInvalidRequestException();
112
        }
113
    }
114
}