Passed
Push — master ( b268a6...9e113b )
by Zlatin
01:21
created

Router::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
crap 12
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
     * @param ResponseInterface $response
47
     * @return ResponseInterface
48
     * @throws Exceptions\RouteNotFoundException
49
     */
50
    public function dispatch(RequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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

50
    public function dispatch(RequestInterface $request, /** @scrutinizer ignore-unused */ ResponseInterface $response)

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...
51
    {
52
53
        if (!isset($this->collection[$request->getMethod()])) {
54
            throw new Exceptions\RouteNotFoundException();
55
        }
56
57
        $uri = $request->getUri()->getPath();
58
59
        foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) {
60
            if (preg_match("#^{$route->getRegEx()}+$#iu", $uri, $match)) {
61
                return $this->process($route, $match);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->process($route, $match) returns the type array which is incompatible with the documented return type Psr\Http\Message\ResponseInterface.
Loading history...
62
            }
63
        }
64
65
        throw new Exceptions\RouteNotFoundException();
66
    }
67
68
    /**
69
     * @param Route $route
70
     * @param mixed $match
71
     * @return array
72
     * @throws Exceptions\RouteIsNotCallableException
73
     */
74
    public function process(Route $route, $match)
75
    {
76
        if (is_callable($route->getCallback(), true)) {
77
            return [
78
                'callable' => $route->getCallback(),
79
                'arguments' => array_map(function($value) use ($match) {
80
                    return isset($match[$value]) ? $match[$value] : null;
81
                }, $route->getParameters())
82
            ];
83
        }
84
85
        throw new Exceptions\RouteIsNotCallableException(sprintf('Callback %s is not callable', $route->getCallback()));
86
    }
87
88
    public function get($name, $pattern, $callback)
89
    {
90
        return $this->add($name, ['GET'], $pattern, $callback);
91
    }
92
93
    public function post($name, $pattern, $callback)
94
    {
95
        return $this->add($name, ['POST'], $pattern, $callback);
96
    }
97
98
    public function put($name, $pattern, $callback)
99
    {
100
        return $this->add($name, ['PUT'], $pattern, $callback);
101
    }
102
103
    public function delete($name, $pattern, $callback)
104
    {
105
        return $this->add($name, ['DELETE'], $pattern, $callback);
106
    }
107
108
    public function options($name, $pattern, $callback)
109
    {
110
        return $this->add($name, ['OPTIONS'], $pattern, $callback);
111
    }
112
113
    public function patch($name, $pattern, $callback)
114
    {
115
        return $this->add($name, ['PATCH'], $pattern, $callback);
116
    }
117
118
    public function head($name, $pattern, $callback)
119
    {
120
        return $this->add($name, ['HEAD'], $pattern, $callback);
121
    }
122
123
    public function any($name, $pattern, $callback)
124
    {
125
        return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback);
126
    }
127
}
128