Completed
Push — 4.0 ( 7cd9cd...61462a )
by Marco
12:15
created

RoutingTable::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 13
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 13
loc 13
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
1
<?php namespace Comodojo\Dispatcher\Routes;
2
3
use \Comodojo\Database\Database;
4
use \Comodojo\Base\Element;
5
use \Comodojo\Exception\DatabaseException;
6
use \Comodojo\Exception\ConfigurationException;
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 RoutingTable implements RoutingTableInterface {
32
33
    private $routes = array();
34
35 View Code Duplication
    public function put($route, $type, $class, $parameters = array()) {
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...
36
37
        $folders = explode("/", $route);
38
39
        $regex = $this->readpath($folders);
40
41
        if (!isset($this->routes[$regex])) {
42
43
            $this->add($folders, $type, $class, $parameters);
44
45
        }
46
47
    }
48
49 View Code Duplication
    public function set($route, $type, $class, $parameters = array()) {
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...
50
51
        $folders = explode("/", $route);
52
53
        $regex = $this->readpath($folders);
54
55
        if (isset($this->routes[$regex])) {
56
57
            $this->add($folders, $type, $class, $parameters);
58
59
        }
60
61
    }
62
63 View Code Duplication
    public function get($route) {
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...
64
65
        $folders = explode("/", $route);
66
67
        $regex = $this->readpath($folders);
68
69
        if (isset($this->routes[$regex]))
70
            return $this->routes[$regex];
71
        else
72
            return null;
73
74
    }
75
76 View Code Duplication
    public function remove($route) {
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...
77
78
        $folders = explode("/", $route);
79
80
        $regex = $this->readpath($folders);
81
82
        if (isset($this->routes[$regex])) unset($this->routes[$regex]);
83
84
    }
85
86
    public function routes() {
87
88
        return $this->routes;
89
90
    }
91
92
    public function defaultRoute() {
93
94
        return $this->get('/');
95
96
    }
97
98
    private function readpath($folders = array(), &$value = null, $regex = '') {
99
100
        if (!empty($folders) && empty($folders[0])) array_shift($folders);
101
102
        if (empty($folders)) {
103
104
            return '^'.$regex.'[\/]?$';
105
106
        } else {
107
108
            $folder  = array_shift($folders);
109
110
            $decoded = json_decode($folder, true);
111
112
            if (!is_null($decoded) && is_array($decoded)) {
113
114
                $keys = array_keys($decoded);
0 ignored issues
show
Unused Code introduced by
$keys is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
115
116
                $param_regex    = '';
117
118
                $param_required = false;
119
120
                foreach ($decoded as $key => $string) {
121
122
                    $param_regex .= $this->readparam($key, $string, $param_required);
123
124
                }
125
126
                $this->readpath(
127
                    $folders,
128
                    $value,
129
                    $regex.'(?:\/'.$param_regex.')'. (($param_required)?'{1}':'?')
130
                );
131
132
            } else {
133
134
                array_push($value['service'], $folder);
135
136
                $this->readpath(
137
                    $folders,
138
                    $value,
139
                    $regex.'\/'.$folder
140
                );
141
142
            }
143
144
        }
145
146
    }
147
148
    private function readparam($key, $string, &$param_required) {
149
150
        $field_required = false;
151
152
        if (preg_match('/^(.+)\*$/', $key, $bits)) {
153
154
            $key            = $bits[1];
155
            $field_required = true;
156
            $param_required = true;
157
158
        }
159
160
        if (!is_null($value)) {
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
161
162
            $value['query'][$key] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$value was never initialized. Although not strictly required by PHP, it is generally a good practice to add $value = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
163
                'regex'    => $string,
164
                'required' => $required
0 ignored issues
show
Bug introduced by
The variable $required does not exist. Did you mean $param_required?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
165
            );
166
167
        }
168
169
        $string = preg_replace('/(?<!\\)\((?!\?)/', '(?:', $string);
170
        $string = preg_replace('/\.([\*\+])(?!\?)/', '.\${1}?', $string);
171
172
        return '(?P<' . $key . '>' . $string . ')' . (($field_required)?'{1}':'?');
173
174
    }
175
176
    private function add($folders, $type, $class, $parameters) {
177
178
        $folders = explode("/", $route);
0 ignored issues
show
Bug introduced by
The variable $route does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
179
180
        $value   = array(
181
            "type"       => $type,
182
            "class"      => $class,
183
            "service"    => array(),
184
            "parameters" => $parameters,
185
            "query"      => array()
186
        );
187
188
        $regex = $this->readpath($folders, $value);
189
190
        $this->routes[$regex] = $value;
191
192
    }
193
194
}
195