CRouterBasic   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 142
rs 10
wmc 16
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 2
A addInternal() 0 7 1
A handleInternal() 0 9 2
C handle() 0 58 11
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
     * Add a route to the router.
27
     *
28
     * @param string $rule   for this route
29
     * @param mixed  $action null, string or callable to implement a controller for the route
30
     *
31
     * @return class as new route
32
     */
33
    public function add($rule, $action = null)
34
    {
35
        $route = $this->di->get('route');
36
        $route->set($rule, $action);
37
        $this->routes[] = $route;
38
39
        // Set as default route
40
        if ($rule == "*") {
41
            $this->defaultRoute = $route;
42
        }
43
        
44
        return $route;
45
    }
46
47
48
49
    /**
50
     * Add an internal (not exposed to url-matching) 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 addInternal($rule, $action = null)
58
    {
59
        $route = $this->di->get('route');
60
        $route->set($rule, $action);
61
        $this->internalRoutes[$rule] = $route;
62
        return $route;
63
    }
64
65
66
67
    /**
68
     * Add an internal (not exposed to url-matching) route to the router.
69
     *
70
     * @param string $rule   for this route
71
     * @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...
72
     *
73
     * @return class as new route
74
     */
75
    public function handleInternal($rule)
76
    {
77
        if (isset($this->internalRoutes[$rule])) {
78
            $route = $this->internalRoutes[$rule];
79
            $route->handle();
80
        } else {
81
            throw new \Anax\Exception\NotFoundException("No internal route to handle: " . $rule);
82
        }
83
    }
84
85
86
87
    /**
88
     * Handle the routes and match them towards the request, dispatch them when a match is made.
89
     *
90
     * @return $this
91
     */
92
    public function handle()
93
    {
94
        try {
95
96
            $query = $this->di->request->getRoute();
97
            $parts = $this->di->request->getRouteParts();
98
99
            // Match predefined routes
100
            foreach ($this->routes as $route) {
101
                if ($route->match($query)) {
102
                    return $route->handle();
103
                }
104
            }
105
106
            // Default handling route as :controller/:action/:params using the dispatcher
107
            $dispatcher = $this->di->dispatcher;
108
            $dispatcher->setControllerName(isset($parts[0]) ? $parts[0] : 'index');
109
110
            if ($dispatcher->isValidController()) {
111
112
                $dispatcher->setActionName(isset($parts[1]) ? $parts[1] : 'index');
113
114
                $params = [];
115
                if (isset($parts[2])) {
116
                    $params = $parts;
117
                    array_shift($params);
118
                    array_shift($params);
119
                }
120
                $dispatcher->setParams($params);
121
122
                if ($dispatcher->isCallable()) {
123
                    return $dispatcher->dispatch();
124
                }
125
            }
126
127
            // Use the "catch-all" route
128
            if ($this->defaultRoute) {
129
                return $this->defaultRoute->handle();
130
            }
131
132
            // No route was matched
133
            $this->handleInternal('404');
134
        
135
        } catch (\Exception $e) {
136
137
            // Exception codes can match a route for a http status code
138
            $code = $e->getCode();
139
            $statusCodes = [403, 404, 500];
140
            if (in_array($code, $statusCodes)) {
141
142
                $this->di->flash->setMessage($e->getMessage());
143
                $this->handleInternal($code);
144
            
145
            } else {
146
                throw $e;
147
            }
148
        }
149
    }
150
}
151