Router   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A route() 0 23 3
A routeCall() 0 10 2
1
<?php
2
3
namespace cvweiss\projectbase;
4
5
class Router
6
{
7
    public function route($view)
8
    {
9
        $uri = filter_input(INPUT_SERVER, 'REQUEST_URI');
10
        $method = ucfirst(strtolower(filter_input(INPUT_SERVER, 'REQUEST_METHOD')));
11
        $call = "do$method";
12
        $params = Config::getInstance()->getAll();
13
14
        $ex = explode('?', $uri);
15
        $uri = $ex[0] == '/' ? 'index' : $ex[0];
16
        $params['title'] = $uri;
17
18
        $ex = explode('/', $uri);
19
        $ex = array_diff($ex, ['']);
20
        while (sizeof($ex) > 0)
21
        {
22
            $className = '\\cvweiss\projectbase\\Controller\\' . implode('\\', $ex);
23
            $this->routeCall($className, $call, $view, $params);
24
            array_unshift($params, array_pop($ex));
25
        }
26
27
        Logger::debug("404 $uri");
28
        $view->error(404, "$uri $method could not be found", $params);
29
    }
30
31
    protected function routeCall($className, $call, $view, $params)
32
    {
33
        if (class_exists($className))
34
        {   
35
            Logger::debug("200 $className $call");
36
            $class = new $className();
37
            $class->$call($view, $params);
38
            throw new \Exception("Called $className::$call but code did not terminate as expected.");
39
        }
40
    }
41
}
42