Completed
Push — 4.0 ( bf6233...a1234e )
by Marco
13:59
created

Table::routes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 15
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.2559
1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Router\Parser;
5
use \Comodojo\Dispatcher\Router\Route;
6
use \Comodojo\Dispatcher\Router\Model as Router;
7
use \Comodojo\Dispatcher\Components\Configuration;
8
use \Comodojo\Cache\CacheManager;
9
use \Comodojo\Exception\DispatcherException;
10
use \Exception;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     GPL-3.0+
17
 *
18
 * LICENSE:
19
 *
20
 * This program is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License as
22
 * published by the Free Software Foundation, either version 3 of the
23
 * License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 */
33
34
class Table extends DispatcherClassModel {
35
36
    public function __construct(
37
        CacheManager $cache,
38
        Router $router
39
    ) {
40
41
        parent::__construct($router->configuration, $router->logger);
0 ignored issues
show
Bug introduced by
The property configuration cannot be accessed from this context as it is declared protected in class Comodojo\Dispatcher\Components\Model.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The property logger cannot be accessed from this context as it is declared protected in class Comodojo\Dispatcher\Components\Model.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
42
        
43
        $this->routes = array();
0 ignored issues
show
Bug introduced by
The property routes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44 1
45
        $this->router = $router;
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
47
        $this->parser = new Parser($router);
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
The call to Parser::__construct() has too many arguments starting with $router.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
48
49 1
        $this->cache = $cache;
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
51 1
        $this->readCache();
52
53 1
    }
54
55 1
    public function add($route, $type, $class, $parameters = array()) {
56
57 1
        $routeData = $this->get($route);
58
59 1
        if (!is_null($routeData)) {
60
61 1
            $routeData->setType($type)
62
                ->setClassName($class)
63 1
                ->setParameters($parameters);
64
65 1
        } else {
66
67
            $folders = explode("/", $route);
68
69
            $this->register($folders, $type, $class, $parameters);
70
71
        }
72
73 1
        return $this;
74
75 1
    }
76
77 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...
78
79 1
        $regex = $this->regex($route);
80
81
        if (isset($this->routes[$regex]))
82
            return $this->routes[$regex];
83 1
        else
84
            return null;
85 1
86
    }
87 1
88 1
    public function regex($route) {
89
90 1
        $folders = explode("/", $route);
91
92
        return $this->parser->read($folders);
93
94 1
    }
95
96 1 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...
97
98 1
        $regex = $this->regex($route);
99
100
        if (isset($this->routes[$regex])) {
101
102 1
            unset($this->routes[$regex]);
103
104 1
            return true;
105
106 1
        }
107
108 1
        return false;
109
110 1
    }
111
112
    public function defaultRoute() {
113
114
        return $this->get('/');
115
116
    }
117
118 1
    public function load($routes) {
119
120 1
        if ( !empty($routes) ) {
121
122 1
            foreach( $routes as $name => $route ) {
123
124
                $this->add($route['route'], $route['type'], $route['class'], $route['parameters']);
125
126
            }
127
128
        }
129
130
        $this->logger->debug("Routing table loaded");
131
132
        $this->dumpCache();
133
134
    }
135
136
    private function readCache() {
137
138
        if ( $this->configuration()->get('routing-table-cache') !== true ) return;
139
140
        $this->routes = $this->cache->setNamespace('dispatcherinternals')->get("dispatcher-routes");
141
142
        if (is_null($routes)) return;
0 ignored issues
show
Bug introduced by
The variable $routes 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...
143
144
        $this->logger->debug("Routing table loaded from cache");
145
146
    }
147
148
    private function dumpCache() {
149
150
        if ( $this->configuration()->get('routing-table-cache') !== true ) return;
151
152
        $ttl = $this->configuration()->get('routing-table-ttl');
153
154
        $this->cache->setNamespace('dispatcherinternals')->set("dispatcher-routes", $this->routes, $ttl == null ? 86400 : intval($ttl));
155
156
        $this->logger->debug("Routing table saved to cache");
157
158 1
    }
159
160 1
    // This method add a route to the supported list
161
    private function register($folders, $type, $class, $parameters) {
162
163
        // The values associated with a route are as follows:
164
        $route = new Route($this->router);
165
        $route->setType($type) // Type of route
166
            ->setClassName($class) // Class to be invoked
167
            ->setParameters($parameters); // Parameters passed via the composer.json configuration (cache, ttl, etc...)
168
169
        $this->logger->debug("ROUTE: " . implode("/", $folders));
170
171
        //$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...
172
173
        // This method generate a global regular expression which will be able to match all the URI supported by the route
174
        $regex = $this->parser->read($folders, $route);
175
176
        $this->logger->debug("ROUTE: " . $regex);
177
178
        //$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...
179
180
        $this->routes[$regex] = $route;
181
182
    }
183
184
}
185