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

BaseUriPlugin::handleRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 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
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 ($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