Total Complexity | 10 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Coverage | 88% |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | class TrailingSlashMiddleware extends AbstractTrailingSlashMiddleware |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private const HEADER = 'Location'; |
||
18 | |||
19 | 6 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
20 | { |
||
21 | 6 | $config = $this->getConfig(); |
|
22 | 6 | $uri = $request->getUri(); |
|
23 | 6 | $response = $handler->handle($request); |
|
24 | |||
25 | 6 | if (isset($config['path_disable'])) { |
|
26 | foreach ($config['path_disable'] as $path) { |
||
27 | if (str_starts_with($uri->getPath(), $path)) { |
||
28 | return $response; |
||
29 | } |
||
30 | } |
||
31 | } |
||
32 | |||
33 | 6 | $path = $this->normalize($uri->getPath()); |
|
34 | |||
35 | 6 | if ($path === $uri->getPath()) { |
|
36 | 2 | return $response; |
|
37 | } |
||
38 | |||
39 | 4 | $location = $uri->withPath($path) |
|
40 | 4 | ->__toString(); |
|
41 | 4 | $factory = Factory::getResponseFactory(); |
|
42 | 4 | $response = $factory->createResponse(HttpStatus::STATUS_MOVED_PERMANENTLY); |
|
43 | |||
44 | 4 | return $response->withHeader(self::HEADER, $location); |
|
45 | } |
||
46 | |||
47 | 6 | private function normalize(string $path): string |
|
63 | } |
||
64 | } |
||
65 |