Completed
Push — 4.0 ( 4d6b64...872671 )
by Marco
03:00
created

Route   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 236
Duplicated Lines 5.93 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 54.55%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 34
lcom 3
cbo 1
dl 14
loc 236
ccs 48
cts 88
cp 0.5455
rs 9.2
c 3
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 5 1
A setType() 0 7 1
A getService() 0 5 1
A getServiceName() 0 5 2
A setService() 0 7 1
A addService() 0 7 1
A getParameter() 0 7 2
A getParameters() 0 5 1
A setParameter() 0 7 1
A setParameters() 0 7 1
A getRequestParameter() 0 7 2
A getRequestParameters() 0 5 1
A setRequestParameter() 0 7 1
A setRequestParameters() 0 7 1
A setQuery() 0 12 1
A isQueryRequired() 7 7 2
A getQueryRegex() 7 7 2
A getQueries() 0 5 1
A setQueries() 0 7 1
A getClassName() 0 5 1
A setClassName() 0 7 1
B path() 0 29 6
A serialize() 0 12 1
A unserialize() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Foundation\DataAccess\Model as FoundationModel;
4
use \Comodojo\Foundation\DataAccess\SerializationTrait;
5
use \Comodojo\Exception\DispatcherException;
6
use \Serializable;
7
use \Exception;
8
9
/**
10
 * @package     Comodojo Dispatcher
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @author      Marco Castiello <[email protected]>
13
 * @license     GPL-3.0+
14
 *
15
 * LICENSE:
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 */
30
31
class Route implements Serializable {
32
33
    protected $classname;
34
    protected $type;
35
    protected $service = [];
36
    protected $parameters = [];
37
    protected $request = [];
38
    protected $query = [];
39
40 2
    public function getType() {
41
42 2
        return $this->type;
43
44
    }
45
46 2
    public function setType($type) {
47
48 2
        $this->type = $type;
49
50 2
        return $this;
51
52
    }
53
54
    public function getService() {
55
56
        return $this->service;
57
58
    }
59
60 1
    public function getServiceName() {
61
62 1
        return empty($this->service) ? "default" : implode('.', $this->service);
63
64
    }
65
66
    public function setService($service) {
67
68
        $this->service = $service;
69
70
        return $this;
71
72
    }
73
74 1
    public function addService($service) {
75
76 1
        $this->service = array_merge($this->service, array($service));
77
78 1
        return $this;
79
80
    }
81
82
    public function getParameter($key) {
83
84
        $parameters = $this->parameters;
85
86
        return isset($parameters[$key]) ? $parameters[$key] : null;
87
88
    }
89
90
    public function getParameters() {
91
92
        return $this->parameters;
93
94
    }
95
96
    public function setParameter($key, $value) {
97
98
        $this->parameters = array_merge($this->parameters, array($key => $value));
99
100
        return $this;
101
102
    }
103
104 1
    public function setParameters($parameters) {
105
106 1
        $this->parameters = $parameters;
107
108 1
        return $this;
109
110
    }
111
112 1
    public function getRequestParameter($key) {
113
114 1
        $parameters = $this->request;
115
116 1
        return isset($parameters[$key]) ? $parameters[$key] : null;
117
118
    }
119
120 1
    public function getRequestParameters() {
121
122 1
        return $this->request;
123
124
    }
125
126 1
    public function setRequestParameter($key, $value) {
127
128 1
        $this->request = array_merge($this->request, array($key => $value));
129
130 1
        return $this;
131
132
    }
133
134
    public function setRequestParameters($parameters) {
135
136
        $this->request = $parameters;
137
138
        return $this;
139
140
    }
141
142 1
    public function setQuery($key, $regex, $required = false) {
143
144 1
        $this->query = array_merge($this->query, [
145
            $key => [
146 1
                "regex" => $regex,
147
                "required" => $required
148 1
            ]
149 1
        ]);
150
151 1
        return $this;
152
153
    }
154
155 1 View Code Duplication
    public function isQueryRequired($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
157 1
        $query = $this->query;
158
159 1
        return isset($query[$key]) ? $query[$key]["required"] : false;
160
161
    }
162
163 1 View Code Duplication
    public function getQueryRegex($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
165 1
        $query = $this->query;
166
167 1
        return isset($query[$key]) ? $query[$key]["regex"] : null;
168
169
    }
170
171
    public function getQueries() {
172
173
        return $this->query;
174
175
    }
176
177
    public function setQueries($query) {
178
179
        $this->query = $query;
180
181
        return $this;
182
183
    }
184
185 2
    public function getClassName() {
186
187 2
        return $this->classname;
188
189
    }
190
191 2
    public function setClassName($class) {
192
193 2
        $this->classname = $class;
194
195 2
        return $this;
196
197
    }
198
199 1
    public function path($path) {
200
201
        // Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key
202 1
        foreach ($this->query as $key => $value) {
203
204 1
            if ( isset($path[$key]) ) {
205
                /* if it's available a bit associated with the parameter name, it is compared against
206
                 * it's regular expression in order to extrect backreferences
207
                 */
208 1
                if ( preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches) ) {
209
210 1
                    if ( count($matches) == 1 ) $matches = $matches[0]; // This is the case where no backreferences are present or available.
211
212
                    // The extracted value (with any backreference available) is added to the query parameters.
213 1
                    $this->setRequestParameter($key, $matches);
214
215 1
                }
216
217 1
            } elseif ($value['required']) {
218
219
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500);
220
221
            }
222
223 1
        }
224
225 1
        return $this;
226
227
    }
228
229
    /**
230
     * Return the serialized data
231
     *
232
     * @return string
233
     */
234
    public function serialize() {
235
236
        return serialize( (object) [
237
            'classname' => $this->classname,
238
            'type' => $this->type,
239
            'service' => $this->service,
240
            'parameters' => $this->parameters,
241
            'request' => $this->request,
242
            'query' => $this->query
243
        ]);
244
245
    }
246
247
    /**
248
     * Return the unserialized object
249
     *
250
     * @param string $data Serialized data
251
     *
252
     */
253
    public function unserialize($data) {
254
255
        $parts = unserialize($data);
256
257
        $this->classname = $parts->classname;
258
        $this->type = $parts->type;
259
        $this->service = $parts->service;
260
        $this->parameters = $parts->parameters;
261
        $this->request = $parts->request;
262
        $this->query = $parts->query;
263
264
    }
265
266
}
267