Completed
Push — master ( 243075...d2a042 )
by Alejandro
03:26
created

PathVersionMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Middleware;
5
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class PathVersionMiddleware implements MiddlewareInterface
12
{
13
    /**
14
     * Process an incoming server request and return a response, optionally delegating
15
     * to the next middleware component to create the response.
16
     *
17
     * @param Request $request
18
     * @param RequestHandlerInterface $handler
19
     *
20
     * @return Response
21
     * @throws \InvalidArgumentException
22
     */
23 2
    public function process(Request $request, RequestHandlerInterface $handler): Response
24
    {
25 2
        $uri = $request->getUri();
26 2
        $path = $uri->getPath();
27
28
        // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
29 2
        if (\strpos($path, '/v') !== 0) {
30 1
            $request = $request->withUri($uri->withPath('/v1' . $uri->getPath()));
31
        }
32
33 2
        return $handler->handle($request);
34
    }
35
}
36