Router   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 11
c 5
b 0
f 0
lcom 1
cbo 1
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatch() 0 16 4
A info() 0 4 1
A found() 0 12 2
A handle() 0 6 2
A error() 0 8 1
1
<?php
2
3
namespace Vertex\Core; 
4
5
use \FastRoute\Dispatcher;
6
7
class Router
8
{ 
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
9
    /**
10
     * Placeholder for the dispatcher class.
11
     * 
12
     * @var object
13
     */
14
    private $dispatcher;
15
16
    /**
17
     * Main controller namespace.
18
     * 
19
     * @var string
20
     */
21
    private $namespace = 'Vertex\\Controller\\';
22
23
    /**
24
     * Create a new router.
25
     *
26
     * @var \FastRoute\simpleDispatcher
27
     * @return void
28
     */
29
    public function __construct($dispatcher)
30
    {
31
        $this->dispatcher = $dispatcher;
32
    }
33
34
    /**
35
     * Dispatch the route.
36
     *
37
     * @return void
38
     */
39
    public function dispatch()
40
    {
41
        $routeInfo = $this->info();
42
43
        switch ($routeInfo[0]) {
44
            case Dispatcher::NOT_FOUND:
45
                echo $this->error('404', 'Page Could Not Be Found');
46
                break;
47
            case Dispatcher::METHOD_NOT_ALLOWED:
48
                echo $this->error('405', 'Method is not allowed');
49
                break;
50
            case Dispatcher::FOUND:
51
                echo $this->found($routeInfo[1], $routeInfo[2]);
52
                break;
53
        }
54
    }
55
56
    /**
57
     * Returns the current route information.
58
     * 
59
     * @return array
60
     */
61
    public function info()
62
    {
63
        return $this->dispatcher->dispatch($_SERVER['REQUEST_METHOD'], strtok($_SERVER['REQUEST_URI'], '?'));
64
    }
65
66
    /**
67
     * Check if a route was found.
68
     * 
69
     * @param  string $handler    
70
     * @param  array $parameters 
71
     */
72
    private function found($handler, $parameters)
73
    {
74
        /* its a closure */
75
        if (is_callable($handler)) {
76
            return $this->handle($handler, $parameters);
77
        }
78
79
        /* its a class */
80
        list($class, $method) = explode("@", $handler, 2);
81
        $class = $this->namespace . $class;
82
        return $this->handle([new $class, $method], $parameters);        
83
    }
84
85
    /**
86
     * Handle the callback.
87
     * 
88
     * @param  string $callback   
89
     * @param  array $parameters 
90
     */
91
    public function handle($callback, ...$parameters)
92
    {
93
        $callback = call_user_func_array($callback, ...$parameters);
94
        
95
        echo (is_array($callback)) ? json_encode($callback) : $callback;
96
    }
97
98
    /**
99
     * Error responce.
100
     * 
101
     * @param  string $type        
102
     * @param  string $description 
103
     */
104
    private function error($type, $description)
105
    {
106
        return View::render('errors.request', [
107
            'title' => $type,
108
            'error_number' => $type,
109
            'error_message' => $description
110
        ]);        
111
    }
112
}
113