Passed
Push — master ( 724fb9...d1a79e )
by Zlatin
01:53
created

Router::get()   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
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 3
crap 2
1
<?php
2
namespace DevOp\Core\Router;
3
4
use DevOp\Core\Router\Route;
5
use Psr\Http\Message\RequestInterface;
6
7
class Router
8
{
9
10
    /**
11
     * @var array
12
     */
13
    private $collection;
14
15
    /**
16
     * @param string $name
17
     * @param array $methods
18
     * @param string $pattern
19
     * @param object|callback|array $callback
20
     * @return $this
21
     */
22
    public function add($name, array $methods, $pattern, $callback)
23
    {
24
        /* @var $route Route */
25
        $route = new Route($pattern, $callback);
26
27
        foreach ($methods AS $method) {
28
            if (!isset($this->collection[$method])) {
29
                $this->collection[$method] = [];
30
            }
31
            $this->collection[$method][$name] = $route;
32
        }
33
34
        return $this;
35
    }
36
37
    public function match(RequestInterface $request)
38
    {
39
        if (isset($this->collection[$request->getMethod()])) {
40
            foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) {
41
                $match = null;
42
                if (preg_match_all($route->getRegEx(), $request->getRequestTarget(), $match)) {
43
                    var_dump($match);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($match) looks like debug code. Are you sure you do not want to remove it?
Loading history...
44
                }
45
            }
46
        }
47
    }
48
49
    public function getAll()
50
    {
51
        return $this->collection;
52
    }
53
54
    public function dispatch($method, $uri)
55
    {
56
57
        if (!isset($this->collection[$method])) {
58
            die('$)$');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
59
        }
60
61
        foreach ($this->collection[$method] AS /* @var $route Route */ $route) {
62
            if (preg_match("#^{$route->getPattern()}+$#iu", $uri)) {
63
                var_dump($route);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($route) looks like debug code. Are you sure you do not want to remove it?
Loading history...
64
            }
65
        }
66
    }
67
68
    public function get($name, $pattern, $callback)
69
    {
70
        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

70
        return $this->add($name, /** @scrutinizer ignore-type */ 'GET', $pattern, $callback);
Loading history...
71
    }
72
73
    public function post($name, $pattern, $callback)
74
    {
75
        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

75
        return $this->add($name, /** @scrutinizer ignore-type */ 'POST', $pattern, $callback);
Loading history...
76
    }
77
78
    public function put($name, $pattern, $callback)
79
    {
80
        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

80
        return $this->add($name, /** @scrutinizer ignore-type */ 'PUT', $pattern, $callback);
Loading history...
81
    }
82
83
    public function delete($name, $pattern, $callback)
84
    {
85
        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

85
        return $this->add($name, /** @scrutinizer ignore-type */ 'DELETE', $pattern, $callback);
Loading history...
86
    }
87
88
    public function options($name, $pattern, $callback)
89
    {
90
        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

90
        return $this->add($name, /** @scrutinizer ignore-type */ 'OPTIONS', $pattern, $callback);
Loading history...
91
    }
92
93
    public function patch($name, $pattern, $callback)
94
    {
95
        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

95
        return $this->add($name, /** @scrutinizer ignore-type */ 'PATCH', $pattern, $callback);
Loading history...
96
    }
97
98
    public function head($name, $pattern, $callback)
99
    {
100
        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

100
        return $this->add($name, /** @scrutinizer ignore-type */ 'HEAD', $pattern, $callback);
Loading history...
101
    }
102
103
    public function any($name, $pattern, $callback)
104
    {
105
        return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback);
106
    }
107
}
108