Completed
Pull Request — master (#42)
by Tobias
06:06
created

BaseHostPlugin::handleRequest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Http\Client\Plugin;
4
5
use Http\Promise\Promise;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\UriInterface;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * Allow to modify the the schema and the host of an existing request.
12
 *
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class BaseHostPlugin implements Plugin
16
{
17
    /**
18
     * @var UriInterface
19
     */
20
    private $baseUri;
21
22
    /**
23
     * @var bool
24
     */
25
    private $replace;
26
27
    /**
28
     * Available options for $config are:
29
     *  - replace: bool True will replace all hosts, false will only add host when none is specified.
30
     *
31
     * @param UriInterface $baseUri
32
     * @param bool $replace
0 ignored issues
show
Bug introduced by
There is no parameter named $replace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
     */
34
    public function __construct(UriInterface $baseUri, array $config = [])
35
    {
36
        $this->baseUri = $baseUri;
37
38
        $resolver = new OptionsResolver();
39
        $this->configureOptions($resolver);
40
        $options = $resolver->resolve($config);
41
42
        $this->replace = $options['replace'];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
49
    {
50
        if ($this->replace || $request->getUri()->getHost() === '') {
51
            $uri = $request->getUri()->withHost($this->baseUri->getHost());
52
            $uri = $uri->withScheme($this->baseUri->getScheme());
53
54
            $request = $request->withUri($uri);
55
        }
56
57
        return $next($request);
58
    }
59
60
    /**
61
     * @param OptionsResolver $resolver
62
     */
63
    private function configureOptions(OptionsResolver $resolver)
64
    {
65
        $resolver->setDefaults([
66
            'replace' => false,
67
        ]);
68
        $resolver->setAllowedTypes('replace', 'bool');
69
    }
70
}
71