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

AddPathPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 31
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A handleRequest() 0 8 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