Completed
Push — di ( f3b698...546a0f )
by Tim
03:40
created

ResultDescriptor::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 2
cp 0.5
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.125
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Description\ResultDescriptor
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\Lang\Reflection\AnnotationInterface;
24
use AppserverIo\Configuration\Interfaces\NodeInterface;
25
use AppserverIo\Description\DescriptorReferencesTrait;
26
use AppserverIo\Description\AbstractNameAwareDescriptor;
27
use AppserverIo\Lang\Reflection\ClassInterface;
28
use AppserverIo\Routlt\Annotations\Result;
29
30
/**
31
 * Descriptor implementation for a action result.
32
 *
33
 * @author     Tim Wagner <[email protected]>
34
 * @copyright  2015 TechDivision GmbH <[email protected]>
35
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link       http://github.com/appserver-io/routlt
37
 * @link       http://www.appserver.io
38
 */
39
class ResultDescriptor extends AbstractNameAwareDescriptor implements ResultDescriptorInterface
40
{
41
42
    /**
43
     * The trait with the references descriptors.
44
     *
45
     * @var AppserverIo\Description\DescriptorReferencesTrait
46
     */
47
    use DescriptorReferencesTrait;
48
49
    /**
50
     * The beans class name.
51
     *
52
     * @var string
53
     */
54
    protected $className;
55
56
    /**
57
     * The action result type.
58
     *
59
     * @var string
60
     */
61
    protected $type;
62
63
    /**
64
     * The action result value.
65
     *
66
     * @var string
67 6
     */
68
    protected $result;
69 6
70 6
    /**
71
     * The HTTP response code that has to be send.
72
     *
73
     * @var string
74
     */
75
    protected $code = 200;
76
77 1
    /**
78
     * Sets the beans class name.
79 1
     *
80
     * @param string $className The beans class name
81
     *
82
     * @return void
83
     */
84
    public function setClassName($className)
85
    {
86
        $this->className = $className;
87
    }
88
89 6
    /**
90
     * Returns the beans class name.
91 6
     *
92 6
     * @return string The beans class name
93
     */
94
    public function getClassName()
95
    {
96
        return $this->className;
97
    }
98
99 1
    /**
100
     * Sets the action result type.
101 1
     *
102
     * @param string $type The action result type
103
     *
104
     * @return void
105
     */
106
    public function setType($type)
107
    {
108
        $this->type = $type;
109
    }
110
111 1
    /**
112
     * Returns the action result type.
113 1
     *
114 1
     * @return string The action result type
115
     */
116
    public function getType()
117
    {
118
        return $this->type;
119
    }
120
121 1
    /**
122
     * Sets the action result value.
123 1
     *
124
     * @param string $result The action result value
125
     *
126
     * @return void
127
     */
128
    public function setResult($result)
129
    {
130
        $this->result = $result;
131 3
    }
132
133 3
    /**
134
     * Returns the action result value.
135
     *
136
     * @return string The action result value
137
     */
138
    public function getResult()
139
    {
140
        return $this->result;
141
    }
142
143 6
    /**
144
     * Sets the HTTP response code that has to be send.
145
     *
146
     * @param integer $code The HTTP response code
147 6
     *
148 6
     * @return void
149 6
     */
150 6
    public function setCode($code)
151
    {
152
        $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...
153 6
    }
154 6
155 6
    /**
156
     * Returns the HTTP response code that has to be send.
157
     *
158 6
     * @return integer The HTTP response code
159
     */
160
    public function getCode()
161
    {
162
        return $this->code;
163 6
    }
164
165
    /**
166
     * Returns a new descriptor instance.
167
     *
168
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The descriptor instance
169
     */
170
    public static function newDescriptorInstance()
171
    {
172
        return new ResultDescriptor();
173 1
    }
174
175 1
    /**
176
     * Returns a new annotation instance for the passed reflection class.
177
     *
178
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
179
     *
180
     * @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation
181
     */
182
    protected function newAnnotationInstance(ClassInterface $reflectionClass)
183
    {
184
        return $reflectionClass->getAnnotation(Result::ANNOTATION);
185
    }
186
187
    /**
188
     * Initializes the bean configuration instance from the passed reflection class instance.
189
     *
190
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
191
     *
192
     * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor
193
     */
194
    public function fromReflectionClass(ClassInterface $reflectionClass)
195
    {
196
197 2
        // add the annotation alias to the reflection class
198
        $reflectionClass->addAnnotationAlias(Result::ANNOTATION, Result::__getClass());
199
200
        // query if we've an action
201 2
        if ($reflectionClass->implementsInterface('AppserverIo\Routlt\Results\ResultInterface') === false &&
202 1
            $reflectionClass->toPhpReflectionClass()->isAbstract() === false) {
203 1
            // if not, do nothing
204 1
            return;
205
        }
206
207
        // query if we've a servlet with a @Path annotation
208 1
        if ($reflectionClass->hasAnnotation(Result::ANNOTATION) === false) {
209 1
            // if not, do nothing
210 1
            return;
211
        }
212
213 1
        // create a new annotation instance
214 1
        $reflectionAnnotation = $this->newAnnotationInstance($reflectionClass);
215 1
216
        // load class name
217
        $this->setClassName($reflectionClass->getName());
218 1
219 1
        // initialize the annotation instance
220 1
        $annotationInstance = $reflectionAnnotation->newInstance(
221 1
            $reflectionAnnotation->getAnnotationName(),
222
            $reflectionAnnotation->getValues()
223
        );
224
225
        // load the default name to register in naming directory
226
        if ($nameAttribute = $annotationInstance->getName()) {
227
            $name = $nameAttribute;
228
        } else {
229
            // if @Annotation(name=****) is NOT set, we use the class name by default
230
            $name = $reflectionClass->getShortName();
231
        }
232
233
        // prepare and set the name
234
        $this->setName($name);
235
236
        // initialize the descriptor properties from the annotation values
237
        $this->setType($annotationInstance->getType());
238
        $this->setResult($annotationInstance->getResult());
239
240
        // set the HTTP response code if given
241
        if ($code = $annotationInstance->getCode()) {
242
            $this->setCode($code);
243
        }
244
245
        // initialize the shared flag @Result(shared=true)
246
        $this->setShared($annotationInstance->getShared());
0 ignored issues
show
Bug introduced by
The method setShared() does not seem to exist on object<AppserverIo\Routl...ption\ResultDescriptor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
248
        // initialize references from the passed reflection class
249
        $this->referencesFromReflectionClass($reflectionClass);
250
251
        // return the instance
252
        return $this;
253
    }
254
255
    /**
256
     * Initializes the result configuration instance from the passed reflection annotation instance.
257
     *
258
     * @param \AppserverIo\Lang\Reflection\AnnotationInterface $reflectionAnnotation The reflection annotation with the result configuration
259
     *
260
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The initialized descriptor
261
     */
262
    public function fromReflectionAnnotation(AnnotationInterface $reflectionAnnotation)
263
    {
264
265
        // initialize the annotation instance
266
        $annotationInstance = $reflectionAnnotation->newInstance(
267
            $reflectionAnnotation->getAnnotationName(),
268
            $reflectionAnnotation->getValues()
269
        );
270
271
        // initialize the descriptor properties from the annotation values
272
        $this->setName($annotationInstance->getName());
273
        $this->setType($annotationInstance->getType());
274
        $this->setResult($annotationInstance->getResult());
275
276
        // set the HTTP response code if given
277
        if ($code = $annotationInstance->getCode()) {
278
            $this->setCode($code);
279
        }
280
281
        // return the instance
282
        return $this;
283
    }
284
285
    /**
286
     * Initializes a action configuration instance from the passed deployment descriptor node.
287
     *
288
     * @param \SimpleXmlElement $node The deployment node with the action configuration
289
     *
290
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
291
     */
292
    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...
293
    {
294
    }
295
296
    /**
297
     * Initializes a action configuration instance from the passed configuration node.
298
     *
299
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration
300
     *
301
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
302
     */
303
    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...
304
    {
305
    }
306
307
    /**
308
     * Merges the passed configuration into this one. Configuration values
309
     * of the passed configuration will overwrite the this one.
310
     *
311
     * @param \AppserverIo\Routlt\Description\ResultDescriptorInterface $resultDescriptor The configuration to merge
312
     *
313
     * @return void
314
     * @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name
315
     */
316
    public function merge(ResultDescriptorInterface $resultDescriptor)
317
    {
318
319
        // check if the classes are equal
320
        if ($this->getName() !== $resultDescriptor->getName()) {
321
            throw new DescriptorException(
322
                sprintf('You try to merge a result configuration for % with %s', $resultDescriptor->getName(), $this->getName())
323
            );
324
        }
325
326
        // merge the class name
327
        if ($className = $resultDescriptor->getClassName()) {
328
            $this->setClassName($className);
329
        }
330
331
        // merge the type
332
        if ($type = $resultDescriptor->getType()) {
333
            $this->setType($type);
334
        }
335
336
        // merge the result
337
        if ($result = $resultDescriptor->getResult()) {
338
            $this->setResult($result);
339
        }
340
341
        // merge the code
342
        if ($code = $resultDescriptor->getCode()) {
343
            $this->setCode($code);
344
        }
345
346
        // merge the shared flag
347
        $this->setShared($resultDescriptor->isShared());
0 ignored issues
show
Bug introduced by
The method isShared() does not seem to exist on object<AppserverIo\Routl...ultDescriptorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setShared() does not seem to exist on object<AppserverIo\Routl...ption\ResultDescriptor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
348
    }
349
}
350