Completed
Push — di ( 256938...5799b2 )
by Tim
06:52
created

ResultDescriptor::merge()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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