Completed
Pull Request — master (#48)
by Sullivan
07:44
created

BaseUriPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A handleRequest() 0 12 2
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
 * This plugin combines the host and path plugins.
11
 *
12
 * @author Sullivan Senechal <[email protected]>
13
 */
14
final class BaseUriPlugin implements Plugin
15
{
16
    /**
17
     * @var AddHostPlugin
18
     */
19
    private $addHostPlugin;
20
21
    /**
22
     * @var AddPathPlugin|null
23
     */
24
    private $addPathPlugin = null;
25
26
    /**
27
     * @param UriInterface $host
28
     * @param array        $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
29
     */
30 5
    public function __construct(UriInterface $host, array $hostConfig = [])
31
    {
32 5
        $this->addHostPlugin = new AddHostPlugin($host, $hostConfig);
33
34 5
        if (rtrim($host->getPath(), '/')) {
35 4
            $this->addPathPlugin = new AddPathPlugin($host);
36 4
        }
37 5
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
43
    {
44 3
        $addHostNext = function (RequestInterface $request) use ($next, $first) {
45 3
            return $this->addHostPlugin->handleRequest($request, $next, $first);
46 3
        };
47
48 3
        if ($this->addPathPlugin) {
49 2
            return $this->addPathPlugin->handleRequest($request, $addHostNext, $first);
50
        }
51
52 1
        return $addHostNext($request);
53
    }
54
}
55