Completed
Push — master ( b534db...688c46 )
by Tim
40s
created

fromReflectionAnnotation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Description\ResultConfigurationDescriptor
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author     Tim Wagner <[email protected]>
15
 * @copyright  2015 TechDivision GmbH <[email protected]>
16
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link       http://github.com/appserver-io/routlt
18
 * @link       http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Routlt\Description;
22
23
use AppserverIo\Routlt\Annotations\Result;
24
use AppserverIo\Lang\Reflection\ClassInterface;
25
use AppserverIo\Lang\Reflection\AnnotationInterface;
26
use AppserverIo\Configuration\Interfaces\NodeInterface;
27
use AppserverIo\Description\AbstractNameAwareDescriptor;
28
29
/**
30
 * Descriptor implementation for a result configuration.
31
 *
32
 * @author     Tim Wagner <[email protected]>
33
 * @copyright  2015 TechDivision GmbH <[email protected]>
34
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link       http://github.com/appserver-io/routlt
36
 * @link       http://www.appserver.io
37
 */
38
class ResultConfigurationDescriptor extends AbstractNameAwareDescriptor implements ResultConfigurationDescriptorInterface
39
{
40
41
    /**
42
     * The action result type.
43
     *
44
     * @var string
45
     */
46
    protected $type;
47
48
    /**
49
     * The action result value.
50
     *
51
     * @var string
52
     */
53
    protected $result;
54
55
    /**
56
     * The HTTP response code that has to be send.
57
     *
58
     * @var string
59
     */
60
    protected $code = 200;
61
62
    /**
63
     * Sets the action result type.
64
     *
65
     * @param string $type The action result type
66
     *
67
     * @return void
68
     */
69
    public function setType($type)
70
    {
71
        $this->type = $type;
72
    }
73
74
    /**
75
     * Returns the action result type.
76
     *
77
     * @return string The action result type
78
     */
79
    public function getType()
80
    {
81
        return $this->type;
82
    }
83
84
    /**
85
     * Sets the action result value.
86
     *
87
     * @param string $result The action result value
88
     *
89
     * @return void
90
     */
91
    public function setResult($result)
92
    {
93
        $this->result = $result;
94
    }
95
96
    /**
97
     * Returns the action result value.
98
     *
99
     * @return string The action result value
100
     */
101
    public function getResult()
102
    {
103
        return $this->result;
104
    }
105
106
    /**
107
     * Sets the HTTP response code that has to be send.
108
     *
109
     * @param integer $code The HTTP response code
110
     *
111
     * @return void
112
     */
113
    public function setCode($code)
114
    {
115
        $this->code = $code;
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $code is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
116
    }
117
118
    /**
119
     * Returns the HTTP response code that has to be send.
120
     *
121
     * @return integer The HTTP response code
122
     */
123
    public function getCode()
124
    {
125
        return $this->code;
126
    }
127
128
    /**
129
     * Returns a new descriptor instance.
130
     *
131
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The descriptor instance
132
     */
133
    public static function newDescriptorInstance()
134
    {
135
        return new ResultConfigurationDescriptor();
136
    }
137
138
    /**
139
     * Returns a new annotation instance for the passed reflection class.
140
     *
141
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
142
     *
143
     * @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation
144
     */
145
    protected function newAnnotationInstance(ClassInterface $reflectionClass)
146
    {
147
        return $reflectionClass->getAnnotation(Result::ANNOTATION);
148
    }
149
150
    /**
151
     * Initializes the bean configuration instance from the passed reflection class instance.
152
     *
153
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
154
     *
155
     * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor
156
     */
157
    public function fromReflectionClass(ClassInterface $reflectionClass)
0 ignored issues
show
Unused Code introduced by
The parameter $reflectionClass 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...
158
    {
159
    }
160
161
    /**
162
     * Initializes the result configuration instance from the passed reflection annotation instance.
163
     *
164
     * @param \AppserverIo\Lang\Reflection\AnnotationInterface $reflectionAnnotation The reflection annotation with the result configuration
165
     *
166
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The initialized descriptor
167
     */
168
    public function fromReflectionAnnotation(AnnotationInterface $reflectionAnnotation)
169
    {
170
171
        // initialize the annotation instance
172
        $annotationInstance = $reflectionAnnotation->newInstance(
173
            $reflectionAnnotation->getAnnotationName(),
174
            $reflectionAnnotation->getValues()
175
        );
176
177
        // initialize the descriptor properties from the annotation values
178
        $this->setName($annotationInstance->getName());
179
        $this->setType($annotationInstance->getType());
180
        $this->setResult($annotationInstance->getResult());
181
182
        // set the HTTP response code if given
183
        if ($code = $annotationInstance->getCode()) {
184
            $this->setCode($code);
185
        }
186
187
        // return the instance
188
        return $this;
189
    }
190
191
    /**
192
     * Initializes a action configuration instance from the passed deployment descriptor node.
193
     *
194
     * @param \SimpleXmlElement $node The deployment node with the action configuration
195
     *
196
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
197
     */
198
    public function fromDeploymentDescriptor(\SimpleXmlElement $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
199
    {
200
    }
201
202
    /**
203
     * Initializes a action configuration instance from the passed configuration node.
204
     *
205
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration
206
     *
207
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
208
     */
209
    public function fromConfiguration(NodeInterface $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node 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...
210
    {
211
    }
212
213
    /**
214
     * Merges the passed configuration into this one. Configuration values
215
     * of the passed configuration will overwrite the this one.
216
     *
217
     * @param \AppserverIo\Routlt\Description\ResultConfigurationDescriptorInterface $resultConfigurationDescriptor The configuration to merge
218
     *
219
     * @return void
220
     * @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name
221
     */
222
    public function merge(ResultConfigurationDescriptorInterface $resultConfigurationDescriptor)
223
    {
224
225
        // check if the classes are equal
226
        if ($this->getName() !== $resultConfigurationDescriptor->getName()) {
227
            throw new DescriptorException(
228
                sprintf('You try to merge a path result configuration for % with %s', $resultConfigurationDescriptor->getName(), $this->getName())
229
            );
230
        }
231
232
        // merge the type
233
        if ($type = $resultConfigurationDescriptor->getType()) {
234
            $this->setType($type);
235
        }
236
237
        // merge the result
238
        if ($result = $resultConfigurationDescriptor->getResult()) {
239
            $this->setResult($result);
240
        }
241
242
        // merge the code
243
        if ($code = $resultConfigurationDescriptor->getCode()) {
244
            $this->setCode($code);
245
        }
246
    }
247
}
248