Completed
Push — dev-master ( 82c8e5...74b82e )
by Derek Stephen
03:46
created

Router::setVarValPairs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
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 16
    public function __construct(ServerRequestInterface $request)
24 16
    {
25 16
        $this->request = $request;
26 16
        $this->uri = $request->getURI();
27 16
        $this->controller = 'index';
28 16
        $this->action = 'index';
29 16
        $this->params = array();
30 16
        $this->routes = array();
31
32
        // get th' path 'n' query string from url
33 16
        $parse = parse_url($this->uri);
34 16
        $this->uri = $parse['path'];
35 16
    }
36
37
38
    /**
39
     * @return bool
40
     */
41 15
    private function matchCustomRoute()
42 15
    {
43
        /** @var \Bone\Mvc\Router\Route $route */
44 15
        foreach ($this->routes as $route) {
45
            // if the regex ain't for the home page an' it matches our route
46 15
            $strings = $route->getRegexStrings();
47 15
            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 15
                return true;
53
            }
54
        }
55 14
        return false;
56
    }
57
58
59 14
    private function regexMatch($regex_string)
60 14
    {
61 14
        $regex = new Regex($regex_string);
62 14
        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 14
    private function matchControllerActionParamsRoute()
102 14
    {
103 14
        return $this->regexMatch(Regex\Url::CONTROLLER_ACTION_VARS);
104
    }
105
106
107
    /**
108
     * @return array|null
109
     */
110 13
    private function matchControllerActionRoute()
111 13
    {
112 13
        return $this->regexMatch(Regex\Url::CONTROLLER_ACTION);
113
    }
114
115
116
    /**
117
     * @return array
118
     */
119 12
    private function matchControllerRoute()
120 12
    {
121 12
        return $this->regexMatch(Regex\Url::CONTROLLER);
122
    }
123
124
125
    /**
126
     *  gets custom routes from config
127
     */
128 15
    private function setCustomRoutesFromConfig()
129 15
    {
130
        // we be checkin' our instruction fer configgered routes
131 15
        $configgeration = Registry::ahoy()->get('routes');
132
133
        // stick some voodoo pins in the map
134 15
        foreach ($configgeration as $route => $options) {
135
            // add the route t' the map
136 15
            $this->routes[] = new Route($route, $options);
137
        }
138 15
    }
139
140
141
    /**
142
     *  Merges params from config
143
     */
144 15
    private function setParams()
145 15
    {
146
        // be addin' the $_GET an' $_POST t' th' params!
147 15
        $method = $this->request->getMethod();
148 15
        $serverParams = $this->request->getServerParams();
149 15
        $queryParams = $this->request->getQueryParams();
150 15
        if ($method == "POST") {
151
            $this->params = array_merge($this->params, $serverParams);
152
        }
153 15
        $this->params = array_merge($this->params, $queryParams);
154 15
    }
155
156
157
    /**
158
     *  Tells the Navigator to go to the / route
159
     */
160 15
    private function matchRoute()
161 15
    {
162
        // we be startin' off assumin' th' voyage will be a disaster
163 15
        $this->controller = 'error';
164 15
        $this->action = 'not-found';
165
166
        // Get th' navigator!
167 15
        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 14
        } 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 13
        } elseif ($matches = $this->matchControllerActionRoute()) {
175
            // we have a controller action match Cap'n!
176 1
            $this->setController($matches);
177 1
            $this->setAction($matches);
178 12
        } 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 15
    }
185
186
    /**
187
     *  Tells the Navigator to go to the / route
188
     */
189 15
    private function sailHome()
190 15
    {
191 15
        $routes = Registry::ahoy()->get('routes');
192 15
        $home_page = $routes['/'];
193 15
        $this->controller = $home_page['controller'];
194 15
        $this->action = $home_page['action'];
195 15
        $this->params = $home_page['params'];
196 15
    }
197
198
199
    /**
200
     *  Figger out where we be goin'
201
     */
202 15
    public function parseRoute()
203 15
    {
204
205
        // start at the home page
206 15
        $this->sailHome();
207
208
        // which way be we goin' ?
209 15
        $path = $this->uri;
210
211
        // Has th' route been set?
212 15
        if ($path != '/') {
213
            // Set the routes configgerd in th' config.php
214 15
            $this->setCustomRoutesFromConfig();
215
216
            // Match the feckin' route ya blubberin' seadog!
217 15
            $this->matchRoute();
218
219
            // Merge th' GET POST and config params
220 15
            $this->setParams();
221
        }
222 15
    }
223
224 16
    public function getController()
225 16
    {
226 16
        return $this->controller;
227
    }
228
229 13
    public function getAction()
230 13
    {
231 13
        return $this->action;
232
    }
233
234 2
    public function getParams()
235 2
    {
236 2
        return $this->params;
237
    }
238
}