Completed
Pull Request — master (#48)
by Sullivan
17:01 queued 06:36
created

AddPathPlugin::handleRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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 $uri;
20
21
    /**
22
     * @param UriInterface $uri
23
     */
24
    public function __construct(UriInterface $uri)
25
    {
26
        if ($uri->getPath() === '') {
27
            throw new \LogicException('URI path cannot be empty');
28
        }
29
30
        if (substr($uri->getPath(), -1) === '/') {
31
            throw new \LogicException('URI path cannot end with a slash.');
32
        }
33
34
        $this->uri = $uri;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
41
    {
42
        $request = $request->withUri($request->getUri()
43
            ->withPath($this->uri->getPath().$request->getUri()->getPath())
44
        );
45
46
        return $next($request);
47
    }
48
}
49