Completed
Push — 4.0 ( 25fc97...6a2270 )
by Marco
16:06
created

Table   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 185
Duplicated Lines 25.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 4
dl 47
loc 185
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A put() 13 13 2
A set() 13 13 2
A add() 0 15 2
A get() 12 12 2
A remove() 9 9 2
A routes() 0 15 2
A defaultRoute() 0 5 1
A load() 0 17 3
A readCache() 0 13 2
A dumpCache() 0 11 1
B register() 0 25 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\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Router\Parser;
5
use \Monolog\Logger;
6
use \Comodojo\Dispatcher\Components\Configuration;
7
use \Comodojo\Cache\CacheManager;
8
use \Comodojo\Exception\DispatcherException;
9
use \Exception;
10
11
/**
12
 * @package     Comodojo Dispatcher
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 Table extends DispatcherClassModel {
34
35
    private $routes = array();
36
    
37
    private $parser;
38
    
39
    private $cache;
40
41
    public function __construct(
42
        CacheManager $cache,
43
        Configuration $configuration,
44
        Logger $logger
45
    ) {
46
47
        parent::__construct($configuration, $logger);
48
49
        $this->parser = new Parser($configuration, $logger);
50
        
51
        $this->cache = $cache;
52
        
53
        $this->readCache();
54
55
    }
56
57 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...
58
59
        $folders = explode("/", $route);
60
61
        $regex = $this->parser->read($folders);
62
63
        if (!isset($this->routes[$regex])) {
64
65
            $this->register($folders, $type, $class, $parameters);
66
67
        }
68
69
    }
70
71 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...
72
73
        $folders = explode("/", $route);
74
75
        $regex = $this->parser->read($folders);
76
77
        if (isset($this->routes[$regex])) {
78
79
            $this->register($folders, $type, $class, $parameters);
80
81
        }
82
83
    }
84
85
    public function add($route, $type, $class, $parameters = array()) {
86
87
        $routeData = $this->get($route);
88
89
        if (is_null($routeData)) {
90
91
            $this->put($route, $type, $class, $parameters);
92
93
        } else {
94
95
            $this->set($route, $type, $class, $parameters);
96
97
        }
98
99
    }
100
101 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...
102
103
        $folders = explode("/", $route);
104
105
        $regex = $this->parser->read($folders);
106
107
        if (isset($this->routes[$regex]))
108
            return $this->routes[$regex];
109
        else
110
            return null;
111
112
    }
113
114 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...
115
116
        $folders = explode("/", $route);
117
118
        $regex = $this->parser->read($folders);
119
120
        if (isset($this->routes[$regex])) unset($this->routes[$regex]);
121
122
    }
123
124
    public function routes($routes = null) {
125
126
        if (is_null($routes)) {
127
            
128
            return $this->routes;
129
            
130
        } else {
131
            
132
            $this->routes = $routes;
133
            
134
            return $this;
135
            
136
        }
137
138
    }
139
140
    public function defaultRoute() {
141
142
        return $this->get('/');
143
144
    }
145
146
    public function load($routes) {
147
148
        if ( !empty($routes) ) {
149
150
            foreach( $routes as $name => $route ) {
151
152
                $this->add($route['route'], $route['type'], $route['class'], $route['parameters']);
153
154
            }
155
156
        }
157
        
158
        $this->logger->debug("Routing table loaded");
159
160
        return $this->dumpCache();
161
162
    }
163
    
164
    private function readCache() {
165
        
166
        $routes = $this->cache->get("dispatcher_routes");
167
        
168
        if (is_null($routes)) return null;
169
        
170
        $this->routes($routes);
171
        
172
        $this->logger->debug("Routing table loaded from cache");
173
        
174
        return $this;
175
        
176
    }
177
    
178
    private function dumpCache() {
179
        
180
        $routes = $this->routes();
181
        
182
        $this->cache->set("dispatcher_routes", $routes, 24 * 60 * 60);
183
        
184
        $this->logger->debug("Routing table saved to cache");
185
        
186
        return $this;
187
        
188
    }
189
190
    // This method add a route to the supported list
191
    private function register($folders, $type, $class, $parameters) {
192
193
        // The values associated with a route are as follows:
194
        $value   = array(
195
            "type"       => $type,       // Type of route
196
            "class"      => $class,      // Class to be invoked
197
            "service"    => array(),     // Service name (it can be a list of namespaces plus a final service name)
198
            "parameters" => $parameters, // Parameters passed via the composer.json configuration (cache, ttl, etc...)
199
            "query"      => array()      // List of parameters with their regular expression that must be added among the query parameters
200
        );
201
202
        $this->logger->debug("ROUTE: " . implode("/", $folders));
203
204
        //$this->logger->debug("PARAMETERS: " . var_export($value, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
205
206
        // This method generate a global regular expression which will be able to match all the URI supported by the route
207
        $regex = $this->parser->read($folders, $value);
208
209
        $this->logger->debug("ROUTE: " . $regex);
210
211
        //$this->logger->debug("PARAMETERS: " . var_export($value, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
212
213
        $this->routes[$regex] = $value;
214
215
    }
216
217
}
218