Completed
Push — master ( 076b0c...47e232 )
by Alejandro
10s
created

ShortCodePathMiddleware   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class ShortCodePathMiddleware implements MiddlewareInterface
12
{
13
    private const OLD_PATH_PREFIX = '/short-codes';
14
    private const NEW_PATH_PREFIX = '/short-urls';
15
16
    /**
17
     * Process an incoming server request and return a response, optionally delegating
18
     * response creation to a handler.
19
     */
20 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
21
    {
22 1
        $uri = $request->getUri();
23 1
        $path = $uri->getPath();
24
25
        // If the path starts with the old prefix, replace it by the new one
26 1
        return $handler->handle(
27 1
            $request->withUri($uri->withPath(\str_replace(self::OLD_PATH_PREFIX, self::NEW_PATH_PREFIX, $path)))
28
        );
29
    }
30
}
31