Completed
Push — master ( 789cc6...743104 )
by David
53:58 queued 32:45
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 0
Metric Value
c 1
b 0
f 0
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\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
 * @author Soufiane Ghzal <[email protected]>
16
 */
17
class HeaderAppendPlugin implements Plugin
18
{
19
    private $headers = [];
20
21
    /**
22
     * @param array $headers headers to add to the request
23
     */
24 3
    public function __construct(array $headers)
25
    {
26 3
        $this->headers = $headers;
27 3
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
33
    {
34 1
        foreach ($this->headers as $header => $headerValue) {
35 1
            $request = $request->withAddedHeader($header, $headerValue);
36 1
        }
37
38 1
        return $next($request);
39
    }
40
}
41