BasicRouter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Voltage;
6
7
use Lit\Voltage\Interfaces\RouterStubResolverInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * Basic router implementation that do strict match against method & path
12
 */
13
class BasicRouter extends AbstractRouter
14
{
15
    protected $mounts = [];
16
    protected $routes = [];
17
    protected $autoSlash = true;
18
    protected $methodNotAllowed;
19
20
    /**
21
     * @param RouterStubResolverInterface $stubResolver     The stub resolver.
22
     * @param mixed                       $notFound         Stub when route is not found.
23
     * @param mixed                       $methodNotAllowed Stub when route exists but method is mismatch.
24
     */
25
    public function __construct(RouterStubResolverInterface $stubResolver, $notFound = null, $methodNotAllowed = null)
26
    {
27
        parent::__construct($stubResolver, $notFound);
28
29
        $this->methodNotAllowed = $methodNotAllowed ?: $notFound;
30
    }
31
32
    protected function findStub(ServerRequestInterface $request)
33
    {
34
        $method = strtolower($request->getMethod());
35
        $path = $request->getUri()->getPath();
36
        $path = self::autoPrependSlash($path);
37
38
        $routes = $this->routes;
39
40
        if (isset($routes[$path][$method])) {
41
            return $routes[$path][$method];
42
        } elseif (isset($routes[$path]) && isset($this->methodNotAllowed)) {
43
            return $this->methodNotAllowed;
44
        } else {
45
            return $this->notFound;
46
        }
47
    }
48
49
    /**
50
     * Register a route
51
     *
52
     * @param string $method    The HTTP method.
53
     * @param string $path      The full URI path.
54
     * @param mixed  $routeStub The route.
55
     * @return $this
56
     */
57
    public function register(string $method, string $path, $routeStub): BasicRouter
58
    {
59
        $this->routes[$path][strtolower($method)] = $routeStub;
60
        return $this;
61
    }
62
63
    /**
64
     * Register a GET route
65
     *
66
     * @param string $path      The full URI path.
67
     * @param mixed  $routeStub The route.
68
     * @return $this
69
     */
70
    public function get(string $path, $routeStub): BasicRouter
71
    {
72
        return $this->register('get', $path, $routeStub);
73
    }
74
75
76
    /**
77
     * Register a POST route
78
     *
79
     * @param string $path      The full URI path.
80
     * @param mixed  $routeStub The route.
81
     * @return $this
82
     */
83
    public function post(string $path, $routeStub): BasicRouter
84
    {
85
        return $this->register('post', $path, $routeStub);
86
    }
87
88
89
    /**
90
     * Register a PUT route
91
     *
92
     * @param string $path      The full URI path.
93
     * @param mixed  $routeStub The route.
94
     * @return $this
95
     */
96
    public function put(string $path, $routeStub): BasicRouter
97
    {
98
        return $this->register('put', $path, $routeStub);
99
    }
100
101
102
    /**
103
     * Register a DELETE route
104
     *
105
     * @param string $path      The full URI path.
106
     * @param mixed  $routeStub The route.
107
     * @return $this
108
     */
109
    public function delete(string $path, $routeStub): BasicRouter
110
    {
111
        return $this->register('delete', $path, $routeStub);
112
    }
113
114
    /**
115
     * Register a PATCH route
116
     *
117
     * @param string $path      The full URI path.
118
     * @param mixed  $routeStub The route.
119
     * @return $this
120
     */
121
    public function patch(string $path, $routeStub): BasicRouter
122
    {
123
        return $this->register('patch', $path, $routeStub);
124
    }
125
}
126