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

BaseUriPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 41
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 $uri        Has to contain a host name and cans have a path.
28
     * @param array        $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
29
     */
30
    public function __construct(UriInterface $uri, array $hostConfig = [])
31
    {
32
        $this->addHostPlugin = new AddHostPlugin($uri, $hostConfig);
33
34
        if (rtrim($uri->getPath(), '/')) {
35
            $this->addPathPlugin = new AddPathPlugin($uri);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
43
    {
44
        $addHostNext = function (RequestInterface $request) use ($next, $first) {
45
            return $this->addHostPlugin->handleRequest($request, $next, $first);
46
        };
47
48
        if ($this->addPathPlugin) {
49
            return $this->addPathPlugin->handleRequest($request, $addHostNext, $first);
50
        }
51
52
        return $addHostNext($request);
53
    }
54
}
55