Completed
Pull Request — master (#14)
by Márk
03:42
created

HeaderAppendPlugin::handleRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
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
8
/**
9
 * Adds headers to the request.
10
 * If the header already exists the value will be appended to the current value.
11
 *
12
 * This only makes sense for headers that can have multiple values like 'Forwarded'
13
 *
14
 * @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
15
 *
16
 * @author Soufiane Ghzal <[email protected]>
17
 *
18
 * TODO: make class final in version 2.0, once plugins does not extend it anymore.
19
 */
20
/*final*/ class HeaderAppendPlugin implements Plugin
21
{
22
    /**
23
     * @var array
24
     */
25
    private $headers = [];
26
27
    /**
28
     * @param array $headers headers to add to the request
29
     */
30 3
    public function __construct(array $headers)
31
    {
32 3
        $this->headers = $headers;
33 3
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
39
    {
40 1
        foreach ($this->headers as $header => $headerValue) {
41 1
            $request = $request->withAddedHeader($header, $headerValue);
42 1
        }
43
44 1
        return $next($request);
45
    }
46
}
47