Completed
Push — master ( f8a04a...bca97c )
by Daniel
03:18
created

Method::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Routing;
4
5
class Method implements \Serializable
6
{
7
8
    private $id;
9
    private $name;
10
    private $params;
11
    private $defaults = [];
12
    private  $options = [];
13
    private $compiled;
14
15
    public function __construct($id, $name, array $defaults = [], array $options = [], array $params = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17
        $this->id = $id;
18
        $this->name = $name;
19
        $this->setDefaults($defaults);
20
        $this->params = $params;
21
    }
22
23
24
    public function serialize()
25
    {
26
        return serialize([
27
           'id' => $this->id,
28
            'name' => $this->name,
29
            'params' => $this->params,
30
            'defaults' => $this->defaults,
31
            'compiled' => $this->compiled
32
        ]);
33
    }
34
35
    public function unserialize($serialized)
36
    {
37
        $data = unserialize($serialized);
38
        $this->setId($data['id']);
39
        $this->setName($data['name']);
40
        $this->setParams($data['params']);
41
        $this->setDefaults($data['defaults']);
42
43
        if (isset($data['compiled'])) {
44
            $this->compiled =$data['compiled'];
45
        }
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getId()
52
    {
53
        return $this->id;
54
    }
55
56
    /**
57
     * @param string $id
58
     */
59
    public function setId($id)
60
    {
61
        $this->id = $id;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getName()
68
    {
69
        return $this->name;
70
    }
71
72
    /**
73
     * @param string $name
74
     */
75
    public function setName($name)
76
    {
77
        $this->name = $name;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getParams()
84
    {
85
        return $this->params;
86
    }
87
88
    /**
89
     * @param array $params
90
     */
91
    public function setParams(array $params)
92
    {
93
        $this->params = $params;
94
    }
95
96
    public function getOptions()
97
    {
98
        return $this->options;
99
    }
100
101
    public function setOptions(array $options)
102
    {
103
        $this->options = array(
104
            'compiler_class' => 'Cmobi\\RabbitmqBundle\\Routing\\MethodCompiler',
105
        );
106
107
        return $this->addOptions($options);
108
    }
109
110
    public function addOptions(array $options)
111
    {
112
        foreach ($options as $name => $option) {
113
            $this->options[$name] = $option;
114
        }
115
        $this->compiled = null;
116
117
        return $this;
118
    }
119
120
    public function setOption($name, $value)
121
    {
122
        $this->options[$name] = $value;
123
        $this->compiled = null;
124
125
        return $this;
126
    }
127
128
    public function getOption($name)
129
    {
130
        if (isset($this->options[$name])) {
131
            return $this->options[$name];
132
        }
133
134
        return null;
135
    }
136
137
    public function hasOption($name)
138
    {
139
        return array_key_exists($name, $this->options);
140
    }
141
142
    public function getDefaults()
143
    {
144
        return $this->defaults;
145
    }
146
147
    public function setDefaults(array $defaults)
148
    {
149
        $this->defaults = array();
150
151
        return $this->addDefaults($defaults);
152
    }
153
154
    public function addDefaults(array $defaults)
155
    {
156
        foreach ($defaults as $name => $default) {
157
            $this->defaults[$name] = $default;
158
        }
159
        $this->compiled = null;
160
161
        return $this;
162
    }
163
164
    public function getDefault($name)
165
    {
166
        if (isset($this->defaults[$name])) {
167
            return $this->defaults[$name];
168
        }
169
170
        return null;
171
    }
172
173
    public function hasDefault($name)
174
    {
175
        return array_key_exists($name, $this->defaults);
176
    }
177
178
    public function setDefault($name, $default)
179
    {
180
        $this->defaults[$name] = $default;
181
        $this->compiled = null;
182
183
        return $this;
184
    }
185
186
    public function getAttribute($key)
187
    {
188
        $attributes = [
189
            'id' => $this->id,
190
            'name' => $this->name,
191
            'params' => $this->params,
192
            'defaults' => $this->defaults,
193
            'options' => $this->options
194
        ];
195
196
        if (!array_key_exists($key, $attributes)) {
197
            return false;
198
        }
199
        return $attributes[$key];
200
    }
201
202
    public function compile()
203
    {
204
        if (null !== $this->compiled) {
205
            return $this->compiled;
206
        }
207
208
        $class = $this->getOption('compiler_class');
209
210
        return $this->compiled = $class::compile($this);
211
    }
212
}