Completed
Push — dev-master ( 7faf0d...4d197b )
by Derek Stephen
03:55
created

Router::matchCustomRoute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 10
cp 0.7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 0
crap 4.432
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Mvc\Router\Route;
6
use Bone\Regex;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class Router
10
{
11
    private $request;
12
    private $uri;
13
    private $controller;
14
    private $action;
15
    private $params;
16
    private $routes;
17
18
19
    /**
20
     *  We be needin' t' look at th' map
21
     * @param ServerRequestInterface $request
22
     */
23 17
    public function __construct(ServerRequestInterface $request)
24 17
    {
25 17
        $this->request = $request;
26 17
        $this->uri = $request->getURI();
27 17
        $this->controller = 'index';
28 17
        $this->action = 'index';
29 17
        $this->params = array();
30 17
        $this->routes = array();
31
32
        // get th' path 'n' query string from url
33 17
        $parse = parse_url($this->uri);
34 17
        $this->uri = $parse['path'];
35 17
    }
36
37
38
    /**
39
     * @return bool
40
     */
41 16
    private function matchCustomRoute()
42 16
    {
43
        /** @var \Bone\Mvc\Router\Route $route */
44 16
        foreach ($this->routes as $route) {
45
            // if the regex ain't for the home page an' it matches our route
46 16
            $strings = $route->getRegexStrings();
47 16
            if ($strings[0] != '\/' && $matches = $route->checkRoute($this->uri)) {
48
                // Garrr me hearties! It be a custom route from th' configgeration!
49
                $this->controller = $route->getControllerName();
50
                $this->action = $route->getActionName();
51
                $this->params = $route->getParams();
52 16
                return true;
53
            }
54
        }
55 16
        return false;
56
    }
57
58
59 16
    private function regexMatch($regex_string)
60 16
    {
61 16
        $regex = new Regex($regex_string);
62 16
        return $regex->getMatches($this->uri);
63
    }
64
65
66
    /**
67
     * @param array $matches
68
     */
69 4
    private function setController(array $matches)
70 4
    {
71 4
        $this->controller = $matches['controller'];
72 4
    }
73
74
75
    /**
76
     * @param array $matches
77
     */
78 3
    private function setAction(array $matches)
79 3
    {
80 3
        $this->action = $matches['action'];
81 3
    }
82
83
84
    /**
85
     * @param array $matches
86
     */
87 1
    private function setVarValPairs(array $matches)
88 1
    {
89 1
        $ex = explode('/', $matches['varvalpairs']);
90 1
        for ($x = 0; $x <= count($ex) - 1; $x += 2) {
91 1
            if (isset($ex[$x + 1])) {
92 1
                $this->params[$ex[$x]] = $ex[$x + 1];
93
            }
94
        }
95 1
    }
96
97
98
    /**
99
     * @return array
100
     */
101 16
    private function matchControllerActionParamsRoute()
102 16
    {
103 16
        return $this->regexMatch(Regex\Url::CONTROLLER_ACTION_VARS);
104
    }
105
106
107
    /**
108
     * @return array|null
109
     */
110 15
    private function matchControllerActionRoute()
111 15
    {
112 15
        return $this->regexMatch(Regex\Url::CONTROLLER_ACTION);
113
    }
114
115
116
    /**
117
     * @return array
118
     */
119 13
    private function matchControllerRoute()
120 13
    {
121 13
        return $this->regexMatch(Regex\Url::CONTROLLER);
122
    }
123
124
125
    /**
126
     *  gets custom routes from config
127
     */
128 16
    private function setCustomRoutesFromConfig()
129 16
    {
130
        // we be checkin' our instruction fer configgered routes
131 16
        $configgeration = Registry::ahoy()->get('routes');
132
133
        // stick some voodoo pins in the map
134 16
        foreach ($configgeration as $route => $options) {
135
            // add the route t' the map
136 16
            $this->routes[] = new Route($route, $options);
137
        }
138 16
    }
139
140
141
    /**
142
     *  Merges params from config
143
     */
144 16
    private function setParams()
145 16
    {
146
        // be addin' the $_GET an' $_POST t' th' params!
147 16
        $method = $this->request->getMethod();
148 16
        $serverParams = $this->request->getServerParams();
149 16
        $queryParams = $this->request->getQueryParams();
150 16
        if ($method == "POST") {
151
            $this->params = array_merge($this->params, $serverParams);
152
        }
153 16
        $this->params = array_merge($this->params, $queryParams);
154 16
    }
155
156
157
    /**
158
     *  Tells the Navigator to go to the / route
159
     */
160 16
    private function matchRoute()
161 16
    {
162
        // we be startin' off assumin' th' voyage will be a disaster
163 16
        $this->controller = 'error';
164 16
        $this->action = 'not-found';
165
166
        // Get th' navigator!
167 16
        if ($this->matchCustomRoute()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
168
            // gaaarrrr, we're set to go!
169 16
        } elseif ($matches = $this->matchControllerActionParamsRoute()) {
170
            // we have a controller action var val match Cap'n!
171 1
            $this->setController($matches);
172 1
            $this->setAction($matches);
173 1
            $this->setVarValPairs($matches);
174 15
        } elseif ($matches = $this->matchControllerActionRoute()) {
175
            // we have a controller action match Cap'n!
176 2
            $this->setController($matches);
177 2
            $this->setAction($matches);
178 13
        } elseif ($matches = $this->matchControllerRoute()) {
179
            // we have a controller action match Cap'n!
180
            // settin' the destination controller and action and params
181 1
            $this->setController($matches);
182 1
            $this->action = 'index';
183
        }
184 16
    }
185
186
    /**
187
     *  Tells the Navigator to go to the / route
188
     */
189 16
    private function sailHome()
190 16
    {
191 16
        $routes = Registry::ahoy()->get('routes');
192 16
        $home_page = $routes['/'];
193 16
        $this->controller = $home_page['controller'];
194 16
        $this->action = $home_page['action'];
195 16
        $this->params = $home_page['params'];
196 16
    }
197
198
199
    /**
200
     *  Figger out where we be goin'
201
     */
202 16
    public function parseRoute()
203 16
    {
204
205
        // start at the home page
206 16
        $this->sailHome();
207
208
        // which way be we goin' ?
209 16
        $path = $this->uri;
210
211
        // Has th' route been set?
212 16
        if ($path != '/') {
213
            // Set the routes configgerd in th' config.php
214 16
            $this->setCustomRoutesFromConfig();
215
216
            // Match the feckin' route ya blubberin' seadog!
217 16
            $this->matchRoute();
218
219
            // Merge th' GET POST and config params
220 16
            $this->setParams();
221
222 16
            return;
223
        }
224
    }
225
226 17
    public function getController()
227 17
    {
228 17
        return $this->controller;
229
    }
230
231 13
    public function getAction()
232 13
    {
233 13
        return $this->action;
234
    }
235
236 1
    public function getParams()
237 1
    {
238 1
        return $this->params;
239
    }
240
}