Completed
Push — master ( 339dcc...fa58da )
by Daniel
13:39
created

RpcResponse::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Response;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcGenericErrorException;
6
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
7
8
class RpcResponse implements RpcResponseInterface
9
{
10
    const VERSION = '2.0';
11
12
    public $id;
13
    public $method;
14
    public $attributes;
15
    public $error;
16
17
    public function __construct($id = null, $method = null, array $attributes = [], RpcGenericErrorException $error = null)
18
    {
19
        $this->id = $id;
20
        $this->method = $method;
21
        $this->error = $error;
22
        $this->attributes = new ParameterBag($attributes);
23
    }
24
25
    /**
26
     * @return int|string
27
     */
28
    public function getId()
29
    {
30
        return $this->id;
31
    }
32
33
    /**
34
     * @param int|string $id
35
     */
36
    public function setId($id)
37
    {
38
        $this->id = $id;
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
     * @return null|array
67
     */
68
    public function getError()
69
    {
70
        return $this->error;
71
    }
72
73
    /**
74
     * @param RpcGenericErrorException $error
75
     */
76
    public function setError(RpcGenericErrorException $error)
77
    {
78
        $this->error = $error;
79
    }
80
81
    /**
82
     * @param $key
83
     * @return mixed
84
     */
85
    public function get($key)
86
    {
87
        return $this->attributes->get($key);
88
    }
89
90
    public function export()
91
    {
92
        $rpc = [
93
            'id' => $this->id,
94
            'jsonrpc' => self::VERSION,
95
            'result' => $this->attributes->all()
96
        ];
97
98
        if ($this->error instanceof RpcGenericErrorException) {
99
            $rpc['error'] = $this->error;
100
            unset($rpc['result']);
101
        }
102
103
        return $rpc;
104
    }
105
}