Completed
Pull Request — master (#44)
by
unknown
23:19 queued 10s
created

HeaderAppendPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Client\Plugin;
4
5
use Psr\Http\Message\RequestInterface;
6
7
/**
8
 * Adds headers to the request.
9
 * If the header already exists the value will be appended to the current value.
10
 *
11
 * This only makes sense for headers that can have multiple values like 'Forwarded'
12
 *
13
 * @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
14
 */
15
class HeaderAppendPlugin implements Plugin
16
{
17
    private $headers = [];
18
19
    /**
20
     * @param array $headers headers to add to the request
21
     */
22 3
    public function __construct(array $headers)
23
    {
24 3
        $this->headers = $headers;
25 3
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
31
    {
32 1
        foreach ($this->headers as $header => $headerValue) {
33 1
            $request = $request->withAddedHeader($header, $headerValue);
34 1
        }
35
36 1
        return $next($request);
37
    }
38
}
39