Completed
Push — master ( 05aecd...a61eba )
by Daniel
03:35
created

RpcRequest::getParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @param array $params
59
     */
60
    public function setParams(array $params)
61
    {
62
        $this->attributes = new ParameterBag($params);
63
    }
64
65
    public function getParams()
66
    {
67
        return $this->attributes->all();
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getVersion()
74
    {
75
        return self::VERSION;
76
    }
77
78
    /**
79
     * @param $key
80
     * @return mixed
81
     */
82
    public function get($key)
83
    {
84
        return $this->attributes->get($key);
85
    }
86
87
    /**
88
     * @param $key
89
     * @param $value
90
     */
91
    public function add($key, $value)
92
    {
93
        $this->attributes->add([$key => $value]);
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function toArray()
100
    {
101
        $rpc = [
102
            'id' => $this->id,
103
            'jsonrpc' => self::VERSION,
104
            'method' => $this->method,
105
            'params' => $this->attributes->all()
106
        ];
107
108
        return $rpc;
109
    }
110
111
    /**
112
     * @param array $request
113
     * @return $this
114
     * @throws RpcInvalidRequestException
115
     */
116
    public function fromArray(array $request)
117
    {
118
        $this->validate($request);
119
        $this->id = $request['id'];
120
        $this->method = $request['method'];
121
        $this->attributes = new ParameterBag($request['params']);
122
123
        return $this;
124
    }
125
126
    public function validate(array $request)
127
    {
128
        if (
129
            (!isset($request['id']) || !isset($request['method']) || !isset($request['params']))
130
            || self::VERSION !== $request['jsonrpc']
131
            || !is_array($request['params'])
132
        ) {
133
            throw new RpcInvalidRequestException();
134
        }
135
    }
136
}