GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2b8c74...685370 )
by Andreas
03:07
created

Router::clearRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Starlit App.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\App;
10
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Route;
13
use Symfony\Component\Routing\RouteCollection;
14
use Symfony\Component\Routing\RequestContext;
15
use Symfony\Component\Routing\Matcher\UrlMatcher;
16
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
17
use Starlit\Utils\Str;
18
19
/**
20
 * Class for routing URL request to controllers.
21
 *
22
 * @author Andreas Nilsson <http://github.com/jandreasn>
23
 */
24
class Router
25
{
26
    /**
27
     * @var BaseApp
28
     */
29
    protected $app;
30
31
    /**
32
     * @var RouteCollection
33
     */
34
    protected $routes;
35
36
    /**
37
     * @var string
38
     */
39
    protected $controllerNamespace = 'App\\Controller';
40
41
    /**
42
     * @var string|null
43
     */
44
    protected $defaultModule;
45
46
    /**
47
     * @var string
48
     */
49
    protected $defaultController = 'index';
50
51
    /**
52
     * @var string
53
     */
54
    protected $defaultAction = 'index';
55
56
    /**
57
     * @var string
58
     */
59
    protected $controllerClassSuffix = 'Controller';
60
61
    /**
62
     * @var string
63
     */
64
    protected $actionMethodSuffix = 'Action';
65
66
    /**
67
     * Constructor.
68
     *
69
     * @param BaseApp $app
70
     * @param array   $options
71
     */
72 9
    public function __construct(BaseApp $app, array $options = [])
73
    {
74 9
        $this->app = $app;
75 9
        $this->routes = new RouteCollection();
76
77 9
        $this->setOptions($options);
78
79
        // Default routes
80 9
        $this->addRoute(new Route('/'));
81 9
        $this->addRoute(new Route('/{action}', [], ['action' => '[a-z-]+']));
82 9
        $this->addRoute(new Route('/{controller}/{action}', [], ['controller' => '[a-z-]+', 'action' => '[a-z-]+']));
83 9
    }
84
85
    /**
86
     * @param array $options
87
     */
88 9
    public function setOptions(array $options)
89
    {
90 9
        if (isset($options['controllerNamespace'])) {
91 9
            $this->controllerNamespace = $options['controllerNamespace'];
92
        }
93
94 9
        if (isset($options['defaultModule'])) {
95 1
            $this->defaultModule = $options['defaultModule'];
96
        }
97
98 9
        if (isset($options['defaultController'])) {
99 1
            $this->defaultController = $options['defaultController'];
100
        }
101
102 9
        if (isset($options['defaultAction'])) {
103 1
            $this->defaultAction = $options['defaultAction'];
104
        }
105
106 9
        if (!empty($options['routes'])) {
107 1
            foreach ($options['routes'] as $path => $routeConfig) {
108 1
                $defaults = isset($routeConfig['defaults']) ? $routeConfig['defaults'] : [];
109 1
                $requirements = isset($routeConfig['requirements']) ? $routeConfig['requirements'] : [];
110 1
                $this->addRoute(new Route($path, $defaults, $requirements));
111
            }
112
        }
113 9
    }
114
115
    /**
116
     * @return string
117
     */
118 1
    public function getDefaultModule()
119
    {
120 1
        return $this->defaultModule;
121
    }
122
123
    /**
124
     * @return string
125
     */
126 1
    public function getDefaultController()
127
    {
128 1
        return $this->defaultController;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1
    public function getDefaultAction()
135
    {
136 1
        return $this->defaultAction;
137
    }
138
139
    /**
140
     * Add route.
141
     *
142
     * @param Route $route
143
     */
144 9
    public function addRoute(Route $route)
145
    {
146
        // We use path as name (sees no use for names)
147 9
        $this->routes->add($route->getPath(), $route);
148 9
    }
149
150
    /**
151
     * Clear any added routes.
152
     */
153 1
    public function clearRoutes()
154
    {
155 1
        $this->routes = new RouteCollection();
156 1
    }
157
158
    /**
159
     * Get routes collection.
160
     *
161
     * @return RouteCollection|Route[]
162
     */
163 1
    public function getRoutes()
164
    {
165 1
        return $this->routes;
166
    }
167
168
    /**
169
     * Resolve controller action and return callable properties.
170
     *
171
     * @param Request $request
172
     * @return AbstractController
173
     * @throws ResourceNotFoundException
174
     */
175 3
    public function route(Request $request)
176
    {
177
        // Match route
178 3
        $context = new RequestContext();
179 3
        $context->fromRequest($request);
180 3
        $matcher = new UrlMatcher($this->routes, $context);
181
182 3
        $match = $matcher->match($request->getPathInfo());
183 3
        $request->attributes->add($match);
184
185
186
        // Set request properties with defaults
187 3
        $module = $this->getRequestModule($request);
188 3
        $controller = $this->getRequestController($request);
189 3
        $action = $this->getRequestAction($request);
190 3
        $request->attributes->add(compact('module', 'controller', 'action'));
191
192
        // Get callable names
193 3
        $controllerClass = $this->getControllerClass($controller, $module);
194 3
        $actionMethod = $this->getActionMethod($action);
195
196
        // Check that controller exist
197 3
        if (!class_exists($controllerClass)) {
198 1
            throw new ResourceNotFoundException("Controller \"{$controllerClass}\" does not exist");
199
        }
200
201
        // Check that action exist (we don't use method_exists because PHP's method case insensitivity)
202 2
        $controllerMethods = get_class_methods($controllerClass);
203 2
        if (!in_array($actionMethod, $controllerMethods, true)) {
204 1
            throw new ResourceNotFoundException("Action method \"{$controllerClass}::{$actionMethod}\" does not exist");
205
        }
206
207 1
        $actualController = new $controllerClass($this->app, $request);
208
209 1
        return $actualController;
210
    }
211
212
    /**
213
     * @param string      $controller Controller name as lowercase separated string
214
     * @param string|null $module     Module as lowercase separated string
215
     * @return string
216
     */
217 4
    public function getControllerClass($controller, $module = null)
218
    {
219 4
        $moduleNamespace = null;
220 4
        if ($module) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $module of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
221 1
            $moduleNamespace = Str::separatorToCamel($module, '-', true);
222
        }
223
224 4
        $controllerClassName = Str::separatorToCamel($controller, '-', true) . $this->controllerClassSuffix;
225
226 4
        return '\\' . implode('\\', array_filter([
227 4
            $moduleNamespace,
228 4
            $this->controllerNamespace,
229 4
            $controllerClassName
230
        ]));
231
    }
232
233
    /**
234
     * @param string $action Action name as lowercase separated string
235
     * @return string
236
     */
237 4
    public function getActionMethod($action)
238
    {
239 4
        return Str::separatorToCamel($action, '-') . $this->actionMethodSuffix;
240
    }
241
242
    /**
243
     * @param Request $request
244
     * @return string
245
     */
246 3
    public function getRequestModule(Request $request)
247
    {
248 3
        return $request->attributes->get('module', $this->defaultModule);
249
    }
250
251
    /**
252
     * @param Request $request
253
     * @return string
254
     */
255 3
    public function getRequestController(Request $request)
256
    {
257 3
        return $request->attributes->get('controller', $this->defaultController);
258
    }
259
260
    /**
261
     * @param Request $request
262
     * @return string
263
     */
264 3
    public function getRequestAction(Request $request)
265
    {
266 3
        return $request->attributes->get('action', $this->defaultAction);
267
    }
268
}
269