Passed
Push — master ( dd3fe0...2f26d2 )
by Zlatin
02:38
created

Router::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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)
51
    {
52
53
        if (!isset($this->collection[$request->getMethod()])) {
54
            throw new Exceptions\RouteNotFoundException();
55
        }
56
57
        $uri = $request->getUri()->getPath();
58
        foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) {
59
            if (preg_match("#^{$route->getPattern()}+$#iu", $uri, $match)) {
60
                return $this->process($request, $response, $route);
61
            }
62
        }
63
64
        throw new Exceptions\RouteNotFoundException();
65
    }
66
67
    /**
68
     * 
69
     * @param Route $route
70
     * @return ResponseInterface
71
     * @throws Exceptions\RouteIsNotCallableException
72
     */
73
    public function process(RequestInterface $request, ResponseInterface $response, Route $route)
74
    {
75
        if ($route->getCallback() === 'string') {
76
            if (strchr($route->getCallback(), ':')) {
0 ignored issues
show
Bug introduced by
$route->getCallback() of type callable is incompatible with the type string expected by parameter $haystack of strchr(). ( Ignorable by Annotation )

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

76
            if (strchr(/** @scrutinizer ignore-type */ $route->getCallback(), ':')) {
Loading history...
77
                list($controller, $method) = explode(':', $route->getCallback());
0 ignored issues
show
Bug introduced by
$route->getCallback() of type callable is incompatible with the type string expected by parameter $string of explode(). ( Ignorable by Annotation )

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

77
                list($controller, $method) = explode(':', /** @scrutinizer ignore-type */ $route->getCallback());
Loading history...
78
            } else {
79
                $controller = $route->getCallback();
80
                $method = '__invoke';
81
            }
82
            return call_user_func_array([$controller, $method], [$request, $response]);
83
        } else if (is_callable($route->getCallback())) {
84
            return call_user_func_array($route->getCallback(), [$request, $response]);
0 ignored issues
show
Bug introduced by
It seems like $route->getCallback() can also be of type object; however, parameter $function of call_user_func_array() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            return call_user_func_array(/** @scrutinizer ignore-type */ $route->getCallback(), [$request, $response]);
Loading history...
85
        }
86
        
87
        throw new Exceptions\RouteIsNotCallableException();
88
    }
89
90
    public function get($name, $pattern, $callback)
91
    {
92
        return $this->add($name, 'GET', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'GET' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

92
        return $this->add($name, /** @scrutinizer ignore-type */ 'GET', $pattern, $callback);
Loading history...
93
    }
94
95
    public function post($name, $pattern, $callback)
96
    {
97
        return $this->add($name, 'POST', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'POST' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

97
        return $this->add($name, /** @scrutinizer ignore-type */ 'POST', $pattern, $callback);
Loading history...
98
    }
99
100
    public function put($name, $pattern, $callback)
101
    {
102
        return $this->add($name, 'PUT', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'PUT' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

102
        return $this->add($name, /** @scrutinizer ignore-type */ 'PUT', $pattern, $callback);
Loading history...
103
    }
104
105
    public function delete($name, $pattern, $callback)
106
    {
107
        return $this->add($name, 'DELETE', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'DELETE' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

107
        return $this->add($name, /** @scrutinizer ignore-type */ 'DELETE', $pattern, $callback);
Loading history...
108
    }
109
110
    public function options($name, $pattern, $callback)
111
    {
112
        return $this->add($name, 'OPTIONS', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'OPTIONS' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

112
        return $this->add($name, /** @scrutinizer ignore-type */ 'OPTIONS', $pattern, $callback);
Loading history...
113
    }
114
115
    public function patch($name, $pattern, $callback)
116
    {
117
        return $this->add($name, 'PATCH', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'PATCH' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

117
        return $this->add($name, /** @scrutinizer ignore-type */ 'PATCH', $pattern, $callback);
Loading history...
118
    }
119
120
    public function head($name, $pattern, $callback)
121
    {
122
        return $this->add($name, 'HEAD', $pattern, $callback);
0 ignored issues
show
Bug introduced by
'HEAD' of type string is incompatible with the type array expected by parameter $methods of DevOp\Core\Router\Router::add(). ( Ignorable by Annotation )

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

122
        return $this->add($name, /** @scrutinizer ignore-type */ 'HEAD', $pattern, $callback);
Loading history...
123
    }
124
125
    public function any($name, $pattern, $callback)
126
    {
127
        return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback);
128
    }
129
}
130