Completed
Pull Request — master (#14)
by Márk
06:15
created

HeaderAppendPlugin   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 1
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleRequest() 0 8 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
final class HeaderAppendPlugin implements Plugin
19
{
20
    /**
21
     * @var array
22
     */
23
    private $headers = [];
24
25
    /**
26
     * @param array $headers headers to add to the request
27
     */
28
    public function __construct(array $headers)
29
    {
30
        $this->headers = $headers;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
37
    {
38
        foreach ($this->headers as $header => $headerValue) {
39
            $request = $request->withAddedHeader($header, $headerValue);
40
        }
41
42
        return $next($request);
43
    }
44
}
45