Passed
Push — master ( 9e113b...59ff1d )
by Zlatin
01:34
created

Router   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 19.15%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 124
ccs 9
cts 47
cp 0.1915
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 3
A getAll() 0 3 1
A patch() 0 3 1
A get() 0 3 1
A any() 0 3 1
A dispatch() 0 16 4
A head() 0 3 1
A put() 0 3 1
A delete() 0 3 1
A process() 0 17 4
A options() 0 3 1
A post() 0 3 1
1
<?php
2
namespace DevOp\Core\Router;
3
4
use DevOp\Core\Router\Route;
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
class Router
9
{
10
11
    /**
12
     * @var array
13
     */
14
    private $collection;
15
16
    /**
17
     * @param string $name
18
     * @param array $methods
19
     * @param string $pattern
20
     * @param object|callback|array $callback
21
     * @return $this
22
     */
23 2
    public function add($name, array $methods, $pattern, $callback)
24
    {
25
        /* @var $route Route */
26 2
        $route = new Route($pattern, $callback);
27
28 2
        foreach ($methods AS $method) {
29 2
            if (!isset($this->collection[$method])) {
30 2
                $this->collection[$method] = [];
31 1
            }
32 2
            $this->collection[$method][$name] = $route;
33 1
        }
34
35 2
        return $this;
36
    }
37
38
    public function getAll()
39
    {
40
        return $this->collection;
41
    }
42
43
    /**
44
     * 
45
     * @param RequestInterface $request
46
     * @return array
47
     * @throws Exceptions\RouteNotFoundException
48
     */
49
    public function dispatch(RequestInterface $request)
50
    {
51
52
        if (!isset($this->collection[$request->getMethod()])) {
53
            throw new Exceptions\RouteNotFoundException();
54
        }
55
56
        $uri = $request->getUri()->getPath();
57
58
        foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) {
59
            if (preg_match("#^{$route->getRegEx()}+$#iu", $uri, $match)) {
60
                return $this->process($request, $route, $match);
61
            }
62
        }
63
64
        throw new Exceptions\RouteNotFoundException();
65
    }
66
67
    /**
68
     * @param RequestInterface $request
69
     * @param ResponseInterface $response
70
     * @param Route $route
71
     * @param array $match
72
     * @return array
73
     * @throws Exceptions\RouteIsNotCallableException
74
     */
75
    public function process(RequestInterface $request, Route $route, $match)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
    public function process(/** @scrutinizer ignore-unused */ RequestInterface $request, Route $route, $match)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        
78
        $arguments = [];
79
        foreach ($route->getParameters() AS $param) {
80
            if (isset($match[$param])) {
81
                $arguments[$param] = $match[$param];
82
            }
83
        }
84
85
        $route->setValues($arguments);
86
        
87
        if (is_callable($route->getCallback(), true)) {
88
            return $route;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $route returns the type DevOp\Core\Router\Route which is incompatible with the documented return type array.
Loading history...
89
        }
90
        
91
        throw new Exceptions\RouteIsNotCallableException;
92
    }
93
94
    public function get($name, $pattern, $callback)
95
    {
96
        return $this->add($name, ['GET'], $pattern, $callback);
97
    }
98
99
    public function post($name, $pattern, $callback)
100
    {
101
        return $this->add($name, ['POST'], $pattern, $callback);
102
    }
103
104
    public function put($name, $pattern, $callback)
105
    {
106
        return $this->add($name, ['PUT'], $pattern, $callback);
107
    }
108
109
    public function delete($name, $pattern, $callback)
110
    {
111
        return $this->add($name, ['DELETE'], $pattern, $callback);
112
    }
113
114
    public function options($name, $pattern, $callback)
115
    {
116
        return $this->add($name, ['OPTIONS'], $pattern, $callback);
117
    }
118
119
    public function patch($name, $pattern, $callback)
120
    {
121
        return $this->add($name, ['PATCH'], $pattern, $callback);
122
    }
123
124
    public function head($name, $pattern, $callback)
125
    {
126
        return $this->add($name, ['HEAD'], $pattern, $callback);
127
    }
128
129
    public function any($name, $pattern, $callback)
130
    {
131
        return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback);
132
    }
133
}
134