OperationWrapper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 31
dl 0
loc 200
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 2
A getOperation() 0 3 1
A getMethod() 0 3 1
A match() 0 11 1
A getPathVariables() 0 3 1
A getParameters() 0 3 1
A getMatch() 0 3 1
A getMatches() 0 3 1
A produces() 0 3 1
A getLookupName() 0 3 1
A getMethodName() 0 3 1
1
<?php
2
3
/**
4
 * AppserverIo\RestApi\Wrappers\OA2\OperationWrapper
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 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/restapi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RestApi\Wrappers\OA2;
22
23
use Swagger\Annotations\Operation;
24
use AppserverIo\RestApi\Wrappers\ParameterWrapperInterface;
25
use AppserverIo\RestApi\Wrappers\OperationWrapperInterface;
26
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
27
use AppserverIo\Psr\EnterpriseBeans\Description\NameAwareDescriptorInterface;
28
29
/**
30
 * OpenApi 2.0 compatible operation wrapper.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2018 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/appserver-io/restapi
36
 * @link      http://www.appserver.io
37
 */
38
class OperationWrapper implements OperationWrapperInterface
39
{
40
41
    /**
42
     * The operation to wrap.
43
     *
44
     * @var \Swagger\Annotations\Operation
45
     */
46
    protected $operation;
47
48
    /**
49
     * The lookup name of the object the operation has to be invoked on.
50
     *
51
     * @var string
52
     */
53
    protected $lookupName;
54
55
    /**
56
     * The method name of the object that has to be invoked.
57
     *
58
     * @var string
59
     */
60
    protected $methodName;
61
62
    /**
63
     * The regular expression to match the operation's path to the request's path info.
64
     *
65
     * @var string
66
     */
67
    protected $pattern;
68
69
    /**
70
     * The operation's wrapped parameters.
71
     *
72
     * @var \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface[]
73
     */
74
    protected $parameters = array();
75
76
    /**
77
     * The array with the names of the parsed path variables as key and their position as value.
78
     *
79
     * @var array
80
     */
81
    protected $pathVariables = array();
82
83
    /**
84
     * The array with the values of the matched path variables.
85
     *
86
     * @var array
87
     */
88
    protected $matches = array();
89
90
    /**
91
     * Initializes the wrapper with the passed instances.
92
     *
93
     * @param \Swagger\Annotations\Operation                                            $operation        The operation that has to be wrapped
94
     * @param \AppserverIo\Psr\EnterpriseBeans\Description\NameAwareDescriptorInterface $objectDescriptor The object descriptor of the object the operation has to be invoked on
95
     * @param \ReflectionMethod                                                         $reflectionMethod The reflection method with the method name of the object that has to be invoked
96
     */
97
    public function __construct(Operation $operation, NameAwareDescriptorInterface $objectDescriptor, \ReflectionMethod $reflectionMethod)
98
    {
99
100
        // set the passed instances
101
        $this->operation = $operation;
102
        $this->lookupName = $objectDescriptor->getName();
103
        $this->methodName = $reflectionMethod->getName();
104
105
        // wrap the operation's parameters
106
        foreach ($operation->parameters as $parameter) {
107
            $this->parameters[] = new ParameterWrapper($parameter);
108
        }
109
110
        // initialize the array for the path variables
111
        $pathVariables = array();
112
113
        // extract the path variables from the operations path
114
        preg_match('/\\\{[a-zA-Z0-9\_\-]+\\\}/', preg_quote($operation->path), $pathVariables);
115
116
        // remove the surrounding braces
117
        array_walk($pathVariables, function (&$pathVariable) {
118
            $pathVariable = stripslashes(str_replace(array('{', '}'), null, $pathVariable));
119
        });
120
121
        // set the found path variables
122
        $this->pathVariables = array_flip($pathVariables);
123
124
        // initialize the regex to match the path info to the operation's path
125
        $this->pattern = "@^" . preg_replace('/\\\{[a-zA-Z0-9\_\-]+\\\}/', '([a-zA-Z0-9\-\_]+)', preg_quote($operation->path)) . "$@D";
126
    }
127
128
    /**
129
     * Returns the operation instance.
130
     *
131
     * @return \Swagger\Annotations\Operation The operation instance
132
     */
133
    protected function getOperation()
134
    {
135
        return $this->operation;
136
    }
137
138
    /**
139
     * Query whether or not the operation matches the passed servlet request.
140
     *
141
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The HTTP servlet request instance to match
142
     *
143
     * @return boolean Returns TRUE, if the operation matches the request, else FALSE
144
     */
145
    public function match(HttpServletRequestInterface $servletRequest)
146
    {
147
148
        // try to match the request instance
149
        $match = preg_match($this->pattern, $servletRequest->getPathInfo(), $this->matches);
150
151
        // remove the first match, because it's the path info
152
        array_shift($this->matches);
153
154
        // return TRUE, if the request matches, else FALSE
155
        return $match;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $match returns the type integer which is incompatible with the documented return type boolean.
Loading history...
156
    }
157
158
    /**
159
     * Returns a list of MIME types the operation can produce.
160
     *
161
     * @return array The list of MIME types
162
     */
163
    public function produces()
164
    {
165
        return $this->getOperation()->produces;
166
    }
167
168
    /**
169
     * Returns the operations parameters.
170
     *
171
     * @return \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface[] The array with the parameters
172
     */
173
    public function getParameters()
174
    {
175
        return $this->parameters;
176
    }
177
178
    /**
179
     * Returns the object lookup name that has to be invoked.
180
     *
181
     * @return string The lookup name
182
     */
183
    public function getLookupName()
184
    {
185
        return $this->lookupName;
186
    }
187
188
    /**
189
     * Returns the object method name that has to be invoked.
190
     *
191
     * @return string The method name
192
     */
193
    public function getMethodName()
194
    {
195
        return $this->methodName;
196
    }
197
198
    /**
199
     * Returns an array with the values of the matched path variables.
200
     *
201
     * @return array The array with the values
202
     */
203
    public function getMatches()
204
    {
205
        return $this->matches;
206
    }
207
208
    /**
209
     * Return's the variable value for the passed parameter.
210
     *
211
     * @param \AppserverIo\RestApi\Wrappers\ParameterWrapperInterface $parameter The parameter to return the match for
212
     *
213
     * @return mixed The variable value of the passed parameter
214
     */
215
    public function getMatch(ParameterWrapperInterface $parameter)
216
    {
217
        return $this->matches[$this->pathVariables[$parameter->getName()]];
218
    }
219
220
    /**
221
     * Returns an array with the names of the parsed path variables as key and their position as value.
222
     *
223
     * @return array The array with the path variables
224
     */
225
    public function getPathVariables()
226
    {
227
        return $this->pathVariables;
228
    }
229
230
    /**
231
     * Returns operation's request method.
232
     *
233
     * @return string The request method
234
     */
235
    public function getMethod()
236
    {
237
        return $this->getOperation()->method;
238
    }
239
}
240