Router   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 112
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A post() 0 4 1
A get() 0 4 1
A getIdentifier() 0 8 2
A run() 0 12 3
A getMainPage() 0 10 3
1
<?php
2
/**
3
 * All requests are directed through this class and runs to correct route
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category   OpCacheGUI
8
 * @package    Network
9
 * @author     Pieter Hordijk <[email protected]>
10
 * @copyright  Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
11
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
12
 * @version    1.0.0
13
 */
14
namespace OpCacheGUI\Network;
15
16
/**
17
 * All requests are directed through this class and runs to correct route
18
 *
19
 * @category   OpCacheGUI
20
 * @package    Network
21
 * @author     Pieter Hordijk <[email protected]>
22
 */
23
class Router
24
{
25
    /**
26
     * @var int The types of URIs used by the system
27
     */
28
    const URL_REWRITE  = 1;
29
    const QUERY_STRING = 2;
30
31
    /**
32
     * @var \OpCacheGUI\Network\RequestData Instance of a request class
33
     */
34
    private $request;
35
36
    /**
37
     * @var \OpCacheGUI\Network\RouteBuilder The route factory
38
     */
39
    private $routeFactory;
40
41
    /**
42
     * @var int The type of the identifiers in URLs
43
     */
44
    private $identifierType;
45
46
    /**
47
     * @var array List of available routes
48
     */
49
    private $routes = [];
50
51
    /**
52
     * Creates instance
53
     *
54
     * @param \OpCacheGUI\Network\RequestData  $request        Instance of a request class
55
     * @param \OpCacheGUI\Network\RouteBuilder $routeFactory   Instance of a route builder
56
     * @param int                              $identifierType The type of URIs used by the system
57
     */
58 10
    public function __construct(RequestData $request, RouteBuilder $routeFactory, $identifierType = self::URL_REWRITE)
59
    {
60 10
        $this->request        = $request;
61 10
        $this->routeFactory   = $routeFactory;
62 10
        $this->identifierType = $identifierType;
63 10
    }
64
65
    /**
66
     * Adds a post route to the collection
67
     *
68
     * @param string   $identifier The identifier of this route
69
     * @param callable $callback   The callback to execute when this route matches
70
     */
71 3
    public function post($identifier, callable $callback)
72
    {
73 3
        $this->routes[] = $this->routeFactory->build($identifier, 'POST', $callback);
74 3
    }
75
76
    /**
77
     * Adds a get route to the collection
78
     *
79
     * @param string   $identifier The identifier of this route
80
     * @param callable $callback   The callback to execute when this route matches
81
     */
82 1
    public function get($identifier, callable $callback)
83
    {
84 1
        $this->routes[] = $this->routeFactory->build($identifier, 'GET', $callback);
85 1
    }
86
87
    /**
88
     * Finds the matching route and runs the callback on it
89
     *
90
     * @return mixed The result of the callback
91
     */
92 7
    public function run()
93
    {
94 7
        foreach ($this->routes as $route) {
95 6
            if (!$route->matchesRequest($this->getIdentifier(), $this->request->getVerb())) {
96 3
                continue;
97
            }
98
99 4
            return $route->run();
100
        }
101
102 3
        return $this->getMainPage();
103
    }
104
105
    /**
106
     * Gets the identifier of the current request
107
     *
108
     * @return string The identifier of the current request
109
     */
110 7
    public function getIdentifier()
111
    {
112 7
        if ($this->identifierType === self::URL_REWRITE) {
113 6
            return $this->request->path();
114
        }
115
116 1
        return $this->request->get();
117
    }
118
119
    /**
120
     * Gets the main page (either the status page or the login page)
121
     *
122
     * @return mixed The result of the callback
123
     */
124 3
    private function getMainPage()
125
    {
126 3
        foreach ($this->routes as $route) {
127 2
            if (!$route->matchesRequest('', 'GET')) {
128 1
                continue;
129
            }
130
131 2
            return $route->run();
132
        }
133 1
    }
134
}
135