Completed
Push — 4.0 ( 1533b9...4d1c3b )
by Marco
13:08
created

RoutingTable::put()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 13
loc 13
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
1
<?php namespace Comodojo\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
 *
11
 *
12
 * @package     Comodojo Framework
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @author      Marco Castiello <[email protected]>
15
 * @license     GPL-3.0+
16
 *
17
 * LICENSE:
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
 */
32
33
class RoutingTable implements RoutingTableInterface {
34
35
    private $routes = array();
36
    
37 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...
38
        
39
        $folders = explode("/", $route);
40
        
41
        $regex = $this->readpath($folders);
42
        
43
        if (!isset($this->routes[$regex])) {
44
            
45
            $this->add($folders, $type, $class, $parameters);
46
            
47
        }
48
        
49
    }
50
    
51 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...
52
        
53
        $folders = explode("/", $route);
54
        
55
        $regex = $this->readpath($folders);
56
        
57
        if (isset($this->routes[$regex])) {
58
            
59
            $this->add($folders, $type, $class, $parameters);
60
            
61
        }
62
        
63
    }
64
    
65 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...
66
        
67
        $folders = explode("/", $route);
68
        
69
        $regex = $this->readpath($folders);
70
        
71
        if (isset($this->routes[$regex])) 
72
            return $this->routes[$regex];
73
        else 
74
            return null;
75
        
76
    }
77
    
78 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...
79
        
80
        $folders = explode("/", $route);
81
        
82
        $regex = $this->readpath($folders);
83
        
84
        if (isset($this->routes[$regex])) unset($this->routes[$regex]);
85
        
86
    }
87
    
88
    public function routes() {
89
        
90
        return $this->routes;
91
        
92
    }
93
    
94
    public function defaultRoute() {
95
        
96
        return $this->get('/');
97
        
98
    }
99
    
100
    private function readpath($folders = array(), &$value = null, $regex = '') {
101
        
102
        if (!empty($folders) && empty($folders[0])) array_shift($folders);
103
        
104
        if (empty($folders)) {
105
            
106
            return '^'.$regex.'[\/]?$';
107
            
108
        } else {
109
            
110
            $folder  = array_shift($folders);
111
            
112
            $decoded = json_decode($folder, true);
113
            
114
            if (!is_null($decoded) && is_array($decoded)) {
115
                
116
                $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...
117
                
118
                $param_regex    = '';
119
                
120
                $param_required = false;
121
                
122
                foreach ($decoded as $key => $string) {
123
                    
124
                    $param_regex .= $this->readparam($key, $string, $param_required);
125
                    
126
                }
127
                
128
                $this->readpath( 
129
                    $folders,
130
                    $value,
131
                    $regex.'(?:\/'.$param_regex.')'. (($param_required)?'{1}':'?')
132
                );
133
                
134
            } else {
135
                
136
                array_push($value['service'], $folder);
137
                
138
                $this->readpath(
139
                    $folders,
140
                    $value,
141
                    $regex.'\/'.$folder
142
                );
143
                
144
            }
145
            
146
        }
147
        
148
    }
149
    
150
    private function readparam($key, $string, &$param_required) {
151
        
152
        $field_required = false;
153
        
154
        if (preg_match('/^(.+)\*$/', $key, $bits)) {
155
            
156
            $key            = $bits[1];
157
            $field_required = true;
158
            $param_required = true;
159
            
160
        }
161
        
162
        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...
163
            
164
            $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...
165
                'regex'    => $string,
166
                '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...
167
            );
168
            
169
        }
170
        
171
        $string = preg_replace('/(?<!\\)\((?!\?)/', '(?:', $string);
172
        $string = preg_replace('/\.([\*\+])(?!\?)/', '.\${1}?', $string);
173
        
174
        return '(?P<' . $key . '>' . $string . ')' . (($field_required)?'{1}':'?');
175
        
176
    }
177
    
178
    private function add($folders, $type, $class, $parameters) {
179
        
180
        $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...
181
        
182
        $value   = array(
183
            "type"       => $type,
184
            "class"      => $class,
185
            "service"    => array(),
186
            "parameters" => $parameters,
187
            "query"      => array()
188
        );
189
        
190
        $regex = $this->readpath($folders, $value);
191
        
192
        $this->routes[$regex] = $value;
193
        
194
    }
195
196
}
197