Completed
Push — master ( f7d058...3104fd )
by Patrick
13:36
created

BaseUriPlugin::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Starweb\HttpClient\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\UriInterface;
8
use Http\Client\Common\Plugin\AddHostPlugin;
9
10
/**
11
 * Forked version of the original plugin from the php-http/client-common:1.x branch which uses our custom fixed
12
 * version of the AddPathPlugin regarding identifiert collision
13
 *
14
 * @see AddPathPlugin
15
 * @see https://github.com/php-http/client-common/blob/1.9.1/src/Plugin/BaseUriPlugin.php
16
 *
17
 * Combines the AddHostPlugin and AddPathPlugin.
18
 *
19
 * @author Sullivan Senechal <[email protected]>
20
 */
21
final class BaseUriPlugin implements Plugin
22
{
23
    /**
24
     * @var AddHostPlugin
25
     */
26
    private $addHostPlugin;
27
28
    /**
29
     * @var AddPathPlugin|null
30
     */
31
    private $addPathPlugin = null;
32
33
    /**
34
     * @param UriInterface $uri        Has to contain a host name and cans have a path.
35
     * @param array        $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
36
     */
37
    public function __construct(UriInterface $uri, array $hostConfig = [])
38
    {
39
        $this->addHostPlugin = new AddHostPlugin($uri, $hostConfig);
40
41
        if (rtrim($uri->getPath(), '/')) {
42
            $this->addPathPlugin = new AddPathPlugin($uri);
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
50
    {
51
        $addHostNext = function (RequestInterface $request) use ($next, $first) {
52
            return $this->addHostPlugin->handleRequest($request, $next, $first);
53
        };
54
55
        if ($this->addPathPlugin) {
56
            return $this->addPathPlugin->handleRequest($request, $addHostNext, $first);
57
        }
58
59
        return $addHostNext($request);
60
    }
61
}
62