Test Failed
Branch master (16012d)
by Daniel
02:43
created

Router   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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
    /**
11
     * Placeholder for the dispatcher class.
12
     * 
13
     * @var object
14
     */
15
    private $dispatcher;
16
17
    /**
18
     * Main controller namespace.
19
     * 
20
     * @var string
21
     */
22
    private $namespace = 'Vertex\\Controller\\';
23
24
    /**
25
     * Create a new router.
26
     *
27
     * @var \FastRoute\simpleDispatcher
28
     * @return void
1 ignored issue
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct($dispatcher)
31
    {
32
        $this->dispatcher = $dispatcher;
33
    }
34
35
    /**
36
     * Dispatch the route.
37
     *
38
     * @return void
39
     */
40
    public function dispatch()
41
    {
42
        $routeInfo = $this->info();
43
44
        switch ($routeInfo[0]) {
45
            case Dispatcher::NOT_FOUND:
46
                echo $this->error('404', 'Page Could Not Be Found');
47
                break;
48
            case Dispatcher::METHOD_NOT_ALLOWED:
49
                echo $this->error('405', 'Method is not allowed');
50
                break;
51
            case Dispatcher::FOUND:
52
                echo $this->found($routeInfo[1], $routeInfo[2]);
53
                break;
54
        }
55
    }
56
57
    /**
58
     * Returns the current route information.
59
     * 
60
     * @return array
61
     */
62
    public function info()
63
    {
64
        return $this->dispatcher->dispatch($_SERVER['REQUEST_METHOD'], strtok($_SERVER['REQUEST_URI'], '?'));
65
    }
66
67
    /**
68
     * Check if a route was found.
69
     * 
70
     * @param  string $handler    
71
     * @param  array $parameters 
72
     */
73
    private function found($handler, $parameters)
74
    {
75
        /* its a closure */
76
        if (is_callable($handler)) {
77
            return $this->handle($handler, $parameters);
78
        }
79
80
        /* its a class */
81
        list($class, $method) = explode("@", $handler, 2);
82
        $class = $this->namespace . $class;
83
        return $this->handle([new $class, $method], $parameters);        
84
    }
85
86
    /**
87
     * Handle the callback.
88
     * 
89
     * @param  string $callback   
90
     * @param  array $parameters 
91
     */
92
    public function handle($callback, ...$parameters)
93
    {
94
        $callback = call_user_func_array($callback, ...$parameters);
95
        
96
        echo (is_array($callback)) ? json_encode($callback) : $callback;
97
    }
98
99
    /**
100
     * Error responce.
101
     * 
102
     * @param  string $type        
103
     * @param  string $description 
104
     */
105
    private function error($type, $description)
106
    {
107
        return View::render('errors.request', [
108
            'title' => $type,
109
            'error_number' => $type,
110
            'error_message' => $description
111
        ]);        
112
    }
113
}
114