Completed
Pull Request — master (#7)
by Franz
01:26
created

PathPrefixingHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Broker;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
class PathPrefixingHandler implements RequestHandlerInterface
11
{
12
    /** @var RequestHandlerInterface */
13
    private $wrapped;
14
15
    /** @var string */
16
    private $prefix;
17
18
    public function __construct(RequestHandlerInterface $wrapped, string $prefix)
19
    {
20
        $this->wrapped = $wrapped;
21
        $this->prefix = $prefix;
22
    }
23
24
    /**
25
     * Handle the request and return a response.
26
     */
27
    public function handle(ServerRequestInterface $request): ResponseInterface
28
    {
29
        $uri = $request->getUri();
30
        return $this->wrapped->handle(
31
            $request->withUri(
32
                $uri->withPath(
33
                    $this->prefix . $uri->getPath()
34
                )
35
            )
36
        );
37
    }
38
}
39