Completed
Push — master ( c35a3d...e1bc0f )
by Derek Stephen
03:44
created

Router::setCustomRoutesFromConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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 18
    public function __construct(ServerRequestInterface $request)
24 18
    {
25 18
        $this->request = $request;
26 18
        $this->uri = $request->getURI();
27 18
        $this->controller = 'index';
28 18
        $this->action = 'index';
29 18
        $this->params = array();
30 18
        $this->routes = array();
31
32
        // get th' path 'n' query string from url
33 18
        $parse = parse_url($this->uri);
34 18
        $this->uri = $parse['path'];
35 18
    }
36
37
38
    /**
39
     * @return bool
40
     */
41 17
    private function matchCustomRoute()
42 17
    {
43
        /** @var \Bone\Mvc\Router\Route $route */
44 17
        foreach ($this->routes as $route) {
45
            // if the regex ain't for the home page an' it matches our route
46 17
            $strings = $route->getRegexStrings();
47 17
            if ($strings[0] != '\/' && $matches = $route->checkRoute($this->uri)) {
48
                // Garrr me hearties! It be a custom route from th' configgeration!
49 1
                $this->controller = $route->getControllerName();
50 1
                $this->action = $route->getActionName();
51 1
                $this->params = $route->getParams();
52 17
                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 3
    private function setController(array $matches)
70 3
    {
71 3
        $this->controller = $matches['controller'];
72 3
    }
73
74
75
    /**
76
     * @param array $matches
77
     */
78 2
    private function setAction(array $matches)
79 2
    {
80 2
        $this->action = $matches['action'];
81 2
    }
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 14
    private function matchControllerRoute()
120 14
    {
121 14
        return $this->regexMatch(Regex\Url::CONTROLLER);
122
    }
123
124
125
    /**
126
     *  gets custom routes from config
127
     */
128 17
    private function setCustomRoutesFromConfig()
129 17
    {
130
        // we be checkin' our instruction fer configgered routes
131 17
        $configgeration = Registry::ahoy()->get('routes');
132
133
        // stick some voodoo pins in the map
134 17
        foreach ($configgeration as $route => $options) {
135
            // add the route t' the map
136 17
            $this->routes[] = new Route($route, $options);
137
        }
138 17
    }
139
140
141
    /**
142
     *  Merges params from config
143
     */
144 17
    private function setParams()
145 17
    {
146
        // be addin' the $_GET an' $_POST t' th' params!
147 17
        $method = $this->request->getMethod();
148 17
        $serverParams = $this->request->getServerParams();
149 17
        $queryParams = $this->request->getQueryParams();
150 17
        if ($method == "POST") {
151 1
            $this->params = array_merge($this->params, $serverParams);
152
        }
153 17
        $this->params = array_merge($this->params, $queryParams);
154 17
    }
155
156
157
    /**
158
     *  Tells the Navigator to go to the / route
159
     */
160 17
    private function matchRoute()
161 17
    {
162
        // we be startin' off assumin' th' voyage will be a disaster
163 17
        $this->controller = 'error';
164 17
        $this->action = 'not-found';
165
166
        // Get th' navigator!
167 17
        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 1
            $this->setController($matches);
177 1
            $this->setAction($matches);
178 14
        } 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 17
    }
185
186
    /**
187
     *  Tells the Navigator to go to the / route
188
     */
189 17
    private function sailHome()
190 17
    {
191 17
        $routes = Registry::ahoy()->get('routes');
192 17
        $home_page = $routes['/'];
193 17
        $this->controller = $home_page['controller'];
194 17
        $this->action = $home_page['action'];
195 17
        $this->params = $home_page['params'];
196 17
    }
197
198
199
    /**
200
     *  Figger out where we be goin'
201
     */
202 17
    public function parseRoute()
203 17
    {
204
205
        // start at the home page
206 17
        $this->sailHome();
207
208
        // which way be we goin' ?
209 17
        $path = $this->uri;
210
211
        // Has th' route been set?
212 17
        if ($path != '/') {
213
            // Set the routes configgerd in th' config.php
214 17
            $this->setCustomRoutesFromConfig();
215
216
            // Match the feckin' route ya blubberin' seadog!
217 17
            $this->matchRoute();
218
219
            // Merge th' GET POST and config params
220 17
            $this->setParams();
221
        }
222 17
    }
223
224 18
    public function getController()
225 18
    {
226 18
        return $this->controller;
227
    }
228
229 15
    public function getAction()
230 15
    {
231 15
        return $this->action;
232
    }
233
234 2
    public function getParams()
235 2
    {
236 2
        return $this->params;
237
    }
238
}