Completed
Push — master ( c82f1b...c3956a )
by Tim
12s
created

ResultConfigurationDescriptor::merge()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 5
nc 9
nop 1
crap 5
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\Configuration\Interfaces\NodeInterface;
26
use AppserverIo\Description\AbstractNameAwareDescriptor;
27
28
/**
29
 * Descriptor implementation for a result configuration.
30
 *
31
 * @author     Tim Wagner <[email protected]>
32
 * @copyright  2015 TechDivision GmbH <[email protected]>
33
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link       http://github.com/appserver-io/routlt
35
 * @link       http://www.appserver.io
36
 */
37
class ResultConfigurationDescriptor extends AbstractNameAwareDescriptor implements ResultConfigurationDescriptorInterface
38
{
39
40
    /**
41
     * The action result type.
42
     *
43
     * @var string
44
     */
45
    protected $type;
46
47
    /**
48
     * The action result value.
49
     *
50
     * @var string
51
     */
52
    protected $result;
53
54
    /**
55
     * The HTTP response code that has to be send.
56
     *
57
     * @var string
58
     */
59
    protected $code = 200;
60
61
    /**
62
     * Sets the action result type.
63
     *
64
     * @param string $type The action result type
65
     *
66
     * @return void
67
     */
68 6
    public function setType($type)
69
    {
70 6
        $this->type = $type;
71 6
    }
72
73
    /**
74
     * Returns the action result type.
75
     *
76
     * @return string The action result type
77
     */
78 1
    public function getType()
79
    {
80 1
        return $this->type;
81
    }
82
83
    /**
84
     * Sets the action result value.
85
     *
86
     * @param string $result The action result value
87
     *
88
     * @return void
89
     */
90 6
    public function setResult($result)
91
    {
92 6
        $this->result = $result;
93 6
    }
94
95
    /**
96
     * Returns the action result value.
97
     *
98
     * @return string The action result value
99
     */
100 1
    public function getResult()
101
    {
102 1
        return $this->result;
103
    }
104
105
    /**
106
     * Sets the HTTP response code that has to be send.
107
     *
108
     * @param integer $code The HTTP response code
109
     *
110
     * @return void
111
     */
112 1
    public function setCode($code)
113
    {
114 1
        $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...
115 1
    }
116
117
    /**
118
     * Returns the HTTP response code that has to be send.
119
     *
120
     * @return integer The HTTP response code
121
     */
122 1
    public function getCode()
123
    {
124 1
        return $this->code;
125
    }
126
127
    /**
128
     * Returns a new descriptor instance.
129
     *
130
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The descriptor instance
131
     */
132 3
    public static function newDescriptorInstance()
133
    {
134 3
        return new ResultConfigurationDescriptor();
135
    }
136
137
    /**
138
     * Returns a new annotation instance for the passed reflection class.
139
     *
140
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
141
     *
142
     * @return \AppserverIo\Lang\Reflection\AnnotationInterface The reflection annotation
143
     */
144
    protected function newAnnotationInstance(ClassInterface $reflectionClass)
145
    {
146
        return $reflectionClass->getAnnotation(Result::ANNOTATION);
147
    }
148
149
    /**
150
     * Initializes the bean configuration instance from the passed reflection class instance.
151
     *
152
     * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration
153
     *
154
     * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor
155
     */
156
    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...
157
    {
158
    }
159
160
    /**
161
     * Initializes the result configuration instance from the passed result annotation instance.
162
     *
163
     * @param \AppserverIo\Routlt\Annotations\Result $annotation The result annotation with the result configuration
164
     *
165
     * @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The initialized descriptor
166
     */
167 6
    public function fromAnnotation(Result $annotation)
168
    {
169
170
        // initialize the descriptor properties from the annotation values
171 6
        $this->setName($annotation->getName());
172 6
        $this->setType($annotation->getType());
173 6
        $this->setResult($annotation->getResult());
174
175
        // set the HTTP response code if given
176 6
        if ($code = $annotation->getCode()) {
177
            $this->setCode($code);
178
        }
179
180
        // return the instance
181 6
        return $this;
182
    }
183
184
    /**
185
     * Initializes a action configuration instance from the passed deployment descriptor node.
186
     *
187
     * @param \SimpleXmlElement $node The deployment node with the action configuration
188
     *
189
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
190
     */
191 1
    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...
192
    {
193 1
    }
194
195
    /**
196
     * Initializes a action configuration instance from the passed configuration node.
197
     *
198
     * @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration
199
     *
200
     * @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor
201
     */
202
    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...
203
    {
204
    }
205
206
    /**
207
     * Merges the passed configuration into this one. Configuration values
208
     * of the passed configuration will overwrite the this one.
209
     *
210
     * @param \AppserverIo\Routlt\Description\ResultConfigurationDescriptorInterface $resultConfigurationDescriptor The configuration to merge
211
     *
212
     * @return void
213
     * @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name
214
     */
215 2
    public function merge(ResultConfigurationDescriptorInterface $resultConfigurationDescriptor)
216
    {
217
218
        // check if the classes are equal
219 2
        if ($this->getName() !== $resultConfigurationDescriptor->getName()) {
220 1
            throw new DescriptorException(
221 1
                sprintf('You try to merge a path result configuration for % with %s', $resultConfigurationDescriptor->getName(), $this->getName())
222 1
            );
223
        }
224
225
        // merge the type
226 1
        if ($type = $resultConfigurationDescriptor->getType()) {
227 1
            $this->setType($type);
228 1
        }
229
230
        // merge the result
231 1
        if ($result = $resultConfigurationDescriptor->getResult()) {
232 1
            $this->setResult($result);
233 1
        }
234
235
        // merge the code
236 1
        if ($code = $resultConfigurationDescriptor->getCode()) {
237 1
            $this->setCode($code);
238 1
        }
239 1
    }
240
}
241