Completed
Pull Request — master (#48)
by Sullivan
05:32
created

AddPathPlugin::handleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\UriInterface;
8
9
/**
10
 * Add base path to the request URI. Useful for base API URLs like http://domain.com/api.
11
 *
12
 * @author Sullivan Senechal <[email protected]>
13
 */
14
final class AddPathPlugin implements Plugin
15
{
16
    /**
17
     * @var UriInterface
18
     */
19
    private $host;
20
21
    /**
22
     * @param UriInterface $host
23
     */
24 7
    public function __construct(UriInterface $host)
25
    {
26 7
        if ($host->getPath() === '') {
27
            throw new \LogicException('Host path can not be empty');
28
        }
29
30 7
        $this->host = $host;
31 7
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 3
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
37
    {
38 3
        $request = $request->withUri($request->getUri()
39 3
            ->withPath($this->host->getPath().$request->getUri()->getPath())
40 3
        );
41
42 3
        return $next($request);
43
    }
44
}
45