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

ShortCodePathMiddleware::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
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