Completed
Push — master ( 35c1c3...0dd52b )
by Dawid
10s
created

Route   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
dl 0
loc 211
ccs 44
cts 48
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 19

18 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A withMethods() 0 6 1
A delete() 0 3 1
A generateNameFromPath() 0 4 1
A post() 0 3 1
A getName() 0 3 1
A getMethods() 0 3 1
A getPath() 0 3 1
A head() 0 3 1
A withAttributes() 0 6 1
A __construct() 0 8 2
A getController() 0 3 1
A patch() 0 3 1
A options() 0 3 1
A withController() 0 6 1
A put() 0 3 1
A getAttributes() 0 3 1
A getAttribute() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Http\Router;
4
5
use Igni\Http\Controller;
6
use Igni\Http\Request;
7
use Igni\Http\Route as RouteInterface;
8
9
/**
10
 * Proxy class for symfony's route.
11
 *
12
 * @package Igni\Http\Router
13
 */
14
class Route implements RouteInterface
15
{
16
    /** @var string */
17
    private $name;
18
19
    /** @var array */
20
    private $attributes = [];
21
22
    /** @var array */
23
    private $methods = [];
24
25
    /** @var string */
26
    private $path;
27
28
    /** @var callable|Controller */
29
    private $controller;
30
31
    /**
32
     * Route constructor.
33
     *
34
     * @param string $path
35
     * @param string $name
36
     * @param array $methods
37
     */
38 18
    public function __construct(string $path, array $methods = ['GET'], string $name = '')
39
    {
40 18
        if (empty($name)) {
41 18
            $name = self::generateNameFromPath($path);
42
        }
43 18
        $this->name = $name;
44 18
        $this->methods = $methods;
45 18
        $this->path = $path;
46 18
    }
47
48 15
    public function getName(): string
49
    {
50 15
        return $this->name;
51
    }
52
53 11
    public function withController($controller): RouteInterface
54
    {
55 11
        $instance = clone $this;
56 11
        $instance->controller = $controller;
57
58 11
        return $instance;
59
    }
60
61 9
    public function getController()
62
    {
63 9
        return $this->controller;
64
    }
65
66
    public function withMethods(array $methods): RouteInterface
67
    {
68
        $instance = clone $this;
69
        $instance->methods = $methods;
70
71
        return $instance;
72
    }
73
74
    /**
75
     * Returns route pattern.
76
     *
77
     * @return string
78
     */
79 16
    public function getPath(): string
80
    {
81 16
        return $this->path;
82
    }
83
84
    /**
85
     * Returns request methods.
86
     *
87
     * @return array
88
     */
89 14
    public function getMethods(): array
90
    {
91 14
        return $this->methods;
92
    }
93
94
    /**
95
     * Factories new instance of the current route with
96
     * attributes retrieved from client's request.
97
     *
98
     * @param array $attributes
99
     * @return Route
100
     */
101 11
    public function withAttributes(array $attributes): RouteInterface
102
    {
103 11
        $instance = clone $this;
104 11
        $instance->attributes = $attributes;
105
106 11
        return $instance;
107
    }
108
109
    /**
110
     * Returns attributes extracted from the uri.
111
     *
112
     * @return array
113
     */
114 10
    public function getAttributes(): array
115
    {
116 10
        return $this->attributes;
117
    }
118
119 1
    public function getAttribute(string $name)
120
    {
121 1
        return $this->attributes[$name] ?? null;
122
    }
123
124
    /**
125
     * Factories new instance of the route
126
     * that will be matched against get request.
127
     *
128
     * @param string $path
129
     * @param string $name
130
     * @return Route
131
     */
132 10
    public static function get(string $path, string $name = ''): Route
133
    {
134 10
        return new self($path, [Request::METHOD_GET], $name);
135
    }
136
137
    /**
138
     * Factories new instance of the route
139
     * that will be matched against post request.
140
     *
141
     * @param string $path
142
     * @param string $name
143
     * @return Route
144
     */
145 2
    public static function post(string $path, string $name = ''): Route
146
    {
147 2
        return new self($path, [Request::METHOD_POST], $name);
148
    }
149
150
    /**
151
     * Factories new instance of the route
152
     * that will be matched against put request.
153
     *
154
     * @param string $path
155
     * @param string $name
156
     * @return Route
157
     */
158 1
    public static function put(string $path, string $name = ''): Route
159
    {
160 1
        return new self($path, [Request::METHOD_PUT], $name);
161
    }
162
163
    /**
164
     * Factories new instance of the route
165
     * that will be matched against delete request.
166
     *
167
     * @param string $path
168
     * @param string $name
169
     * @return Route
170
     */
171 2
    public static function delete(string $path, string $name = ''): Route
172
    {
173 2
        return new self($path, [Request::METHOD_DELETE], $name);
174
    }
175
176
    /**
177
     * Factories new instance of the route
178
     * that will be matched against patch request.
179
     *
180
     * @param string $path
181
     * @param string $name
182
     * @return Route
183
     */
184 1
    public static function patch(string $path, string $name = ''): Route
185
    {
186 1
        return new self($path, [Request::METHOD_PATCH], $name);
187
    }
188
189
    /**
190
     * Factories new instance of the route
191
     * that will be matched against head request.
192
     *
193
     * @param string $path
194
     * @param string $name
195
     * @return Route
196
     */
197 1
    public static function head(string $path, string $name = ''): Route
198
    {
199 1
        return new self($path, [Request::METHOD_HEAD, Request::METHOD_GET], $name);
200
    }
201
202
    /**
203
     * Factories new instance of the route
204
     * that will be matched against options request.
205
     *
206
     * @param string $path
207
     * @param string $name
208
     * @return Route
209
     */
210 1
    public static function options(string $path, string $name = ''): Route
211
    {
212 1
        return new self($path, [Request::METHOD_OPTIONS], $name);
213
    }
214
215
    /**
216
     * Generates default name from given path expression,
217
     * /some/{resource} becomes some_resource
218
     * @param string $path
219
     * @return string
220
     */
221 18
    private static function generateNameFromPath(string $path): string
222
    {
223 18
        $path = preg_replace('/<[^>]+>/', '', $path);
224 18
        return str_replace(['{', '}', '?', '.', '/'], ['', '', '', '_', '_'], trim($path, '/'));
225
    }
226
}
227