Completed
Push — 4.0 ( ed2d0a...2bab8b )
by Marco
15:57
created

RoutingTable   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 100
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 0 25 5
A remove() 0 9 2
A routes() 0 5 1
A defaultRoute() 0 5 1
C readpath() 0 45 7
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
    private $def_route = array();
37
    
38
    public function add($route, $type, $class, $parameters = array()) {
39
        
40
        $folders = explode("/", $route);
41
        
42
        $value   = array(
43
            "type"       => $type,
44
            "class"      => $class,
45
            "parameters" => $parameters,
46
            "query"      => array()
47
        );
48
        
49
        if (count($folders) > 1 && empty($folders[0]))
50
            array_shift($folders);
51
        
52
        if (count($folders) == 1 && empty($folders[0])) {
53
            $this->def_route = $value;
54
            
55
            return $this;
56
        }
57
        
58
        $regex = $this->readpath($folders, $value);
59
        
60
        $this->routes[$regex] = $value;
61
        
62
    }
63
    
64
    public function remove($route) {
65
        
66
        $folders = explode("/", $route);
67
        
68
        $regex = $this->readpath($folders);
69
        
70
        if (isset($this->routes[$regex])) unset($this->routes[$regex]);
71
        
72
    }
73
    
74
    public function routes() {
75
        
76
        return $this->routes;
77
        
78
    }
79
    
80
    public function defaultRoute() {
81
        
82
        return $this->def_route;
83
        
84
    }
85
    
86
    private function readpath($folders, &$value = null, $regex = '\/') {
87
        
88
        if (empty($folders)) {
89
            
90
            return $regex;
91
            
92
        } else {
93
            
94
            $folder = array_shift($folders);
95
            
96
            $decoded = json_decode($folder, true);
97
            
98
            if (!is_null($decoded) && is_array($decoded)) {
99
                
100
                $key = array_keys($decoded);
101
                
102
                $key = $key[0];
103
                
104
                $required = isset($decoded['required'])?
105
                    filter_var($decoded['required'], FILTER_VALIDATE_BOOLEAN) :
106
                    false;
107
                    
108
                if (!empty($value)) {
109
                    $value['query'][$key] = $decoded[$key];
110
                }
111
                
112
                $this->readpath( 
113
                    $folders,
114
                    $value,
115
                    $regex.'('.$decoded[$key].'\/)'. (($required)?'{1}':'?')
116
                );
117
                
118
            } else {
119
                
120
                $this->readpath(
121
                    $folders,
122
                    $value,
123
                    $regex.$folder.'\/'
124
                );
125
                
126
            }
127
            
128
        }
129
        
130
    }
131
132
}
133