CRouterBasic::handle()   C
last analyzed

Complexity

Conditions 11
Paths 97

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
nc 97
nop 0
dl 0
loc 52
ccs 0
cts 32
cp 0
crap 132
rs 6.9006
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Anax\Route;
4
5
/**
6
 * A container for routes.
7
 *
8
 */
9
class CRouterBasic implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\DI\TInjectionAware;
12
13
14
15
    /**
16
     * Properties
17
     *
18
     */
19
    private $routes         = [];    // All the routes
20
    private $internalRoutes = [];    // All internal routes
21
    private $defaultRoute   = null;  // A default rout to catch all
22
23
24
25
    /**
26
     * Get all routes.
27
     *
28
     * @return array with all routes.
29
     */
30
    public function getAll()
31
    {
32
        return $this->routes;
33
    }
34
35
36
37
    /**
38
     * Get all internal routes.
39
     *
40
     * @return array with internal routes.
41
     */
42
    public function getInternal()
43
    {
44
        return $this->internalRoutes;
45
    }
46
47
48
49
    /**
50
     * Add a route to the router.
51
     *
52
     * @param string $rule   for this route
53
     * @param mixed  $action null, string or callable to implement a controller for the route
54
     *
55
     * @return class as new route
56
     */
57
    public function add($rule, $action = null)
58
    {
59
        $route = $this->di->get('route');
60
        $route->set($rule, $action);
61
        $this->routes[] = $route;
62
63
        // Set as default route
64
        if ($rule == "*") {
65
            $this->defaultRoute = $route;
66
        }
67
        
68
        return $route;
69
    }
70
71
72
73
    /**
74
     * Add an internal (not exposed to url-matching) route to the router.
75
     *
76
     * @param string $rule   for this route
77
     * @param mixed  $action null, string or callable to implement a controller for the route
78
     *
79
     * @return class as new route
80
     */
81
    public function addInternal($rule, $action = null)
82
    {
83
        $route = $this->di->get('route');
84
        $route->set($rule, $action);
85
        $this->internalRoutes[$rule] = $route;
86
        return $route;
87
    }
88
89
90
91
    /**
92
     * Add an internal (not exposed to url-matching) route to the router.
93
     *
94
     * @param string $rule   for this route
95
     * @param mixed  $action null, string or callable to implement a controller for the route
0 ignored issues
show
Bug introduced by
There is no parameter named $action. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
     *
97
     * @return class as new route
98
     */
99
    public function handleInternal($rule)
100
    {
101
        if (isset($this->internalRoutes[$rule])) {
102
            $route = $this->internalRoutes[$rule];
103
            $route->handle();
104
        } else {
105
            throw new \Anax\Exception\NotFoundException("No internal route to handle: " . $rule);
106
        }
107
    }
108
109
110
111
    /**
112
     * Handle the routes and match them towards the request, dispatch them when a match is made.
113
     *
114
     * @return $this
115
     */
116
    public function handle()
117
    {
118
        try {
119
            $query = $this->di->request->getRoute();
120
            $parts = $this->di->request->getRouteParts();
121
122
            // Match predefined routes
123
            foreach ($this->routes as $route) {
124
                if ($route->match($query)) {
125
                    return $route->handle();
126
                }
127
            }
128
129
            // Default handling route as :controller/:action/:params using the dispatcher
130
            $dispatcher = $this->di->dispatcher;
131
            $dispatcher->setControllerName(isset($parts[0]) ? $parts[0] : 'index');
132
133
            if ($dispatcher->isValidController()) {
134
                $dispatcher->setActionName(isset($parts[1]) ? $parts[1] : 'index');
135
136
                $params = [];
137
                if (isset($parts[2])) {
138
                    $params = $parts;
139
                    array_shift($params);
140
                    array_shift($params);
141
                }
142
                $dispatcher->setParams($params);
143
144
                if ($dispatcher->isCallable()) {
145
                    return $dispatcher->dispatch();
146
                }
147
            }
148
149
            // Use the "catch-all" route
150
            if ($this->defaultRoute) {
151
                return $this->defaultRoute->handle();
152
            }
153
154
            // No route was matched
155
            $this->handleInternal('404');
156
        } catch (\Exception $e) {
157
            // Exception codes can match a route for a http status code
158
            $code = $e->getCode();
159
            $statusCodes = [403, 404, 500];
160
            if (in_array($code, $statusCodes)) {
161
                $this->di->flash->setMessage($e->getMessage());
162
                $this->handleInternal($code);
163
            } else {
164
                throw $e;
165
            }
166
        }
167
    }
168
}
169