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

RpcResponse   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 125
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 getVersion() 0 4 1
A getError() 0 4 1
A setError() 0 4 1
A get() 0 4 1
A toArray() 0 15 2
A fromArray() 0 21 4
A validate() 0 10 4
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Response;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcGenericErrorException;
6
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcInvalidResponseException;
7
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8
9
class RpcResponse implements RpcResponseInterface
10
{
11
    const VERSION = '2.0';
12
13
    public $id;
14
    public $attributes;
15
    public $error;
16
17
    public function __construct($id = null, array $attributes = [], RpcGenericErrorException $error = null)
18
    {
19
        $this->id = $id;
20
        $this->error = $error;
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
     * @return string
42
     */
43
    public function getVersion()
44
    {
45
        return self::VERSION;
46
    }
47
48
    /**
49
     * @return null|array
50
     */
51
    public function getError()
52
    {
53
        return $this->error;
54
    }
55
56
    /**
57
     * @param RpcGenericErrorException $error
58
     */
59
    public function setError(RpcGenericErrorException $error)
60
    {
61
        $this->error = $error;
62
    }
63
64
    /**
65
     * @param $key
66
     * @return mixed
67
     */
68
    public function get($key)
69
    {
70
        return $this->attributes->get($key);
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function toArray()
77
    {
78
        $rpc = [
79
            'id' => $this->id,
80
            'jsonrpc' => self::VERSION,
81
            'result' => $this->attributes->all()
82
        ];
83
84
        if ($this->error instanceof RpcGenericErrorException) {
85
            $rpc['error'] = $this->error;
86
            unset($rpc['result']);
87
        }
88
89
        return $rpc;
90
    }
91
92
    /**
93
     * @param array $response
94
     * @return $this
95
     * @throws RpcInvalidResponseException
96
     */
97
    public function fromArray(array $response)
98
    {
99
        $this->validate($response);
100
        $this->id = $response['id'];
101
102
        if (isset($response['error'])) {
103
            $this->error = $response['error'];
104
        }
105
106
        if (isset($response['result'])) {
107
108
            $result = $response['result'];
109
110
            if (!is_array($result)) {
111
                $result = [$result];
112
            }
113
            $this->attributes = new ParameterBag($result);
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param array $response
121
     * @throws RpcInvalidResponseException
122
     */
123
    public function validate(array $response)
124
    {
125
        if (
126
            !array_key_exists(['id', 'jsonrpc'], $response)
127
            || (!isset($response['result'])
128
                && !isset($response['error']))
129
        ) {
130
            throw new RpcInvalidResponseException();
131
        }
132
    }
133
}