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

HeaderDefaultsPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
 * Set default values for the request headers.
10
 * If a given header already exists the value wont be replaced and the request wont be changed.
11
 *
12
 * @author Soufiane Ghzal <[email protected]>
13
 */
14 View Code Duplication
final class HeaderDefaultsPlugin implements Plugin
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var array
18
     */
19
    private $headers = [];
20
21
    /**
22
     * @param array $headers headers to set to the request
23
     */
24
    public function __construct(array $headers)
25
    {
26
        $this->headers = $headers;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
33
    {
34
        foreach ($this->headers as $header => $headerValue) {
35
            if (!$request->hasHeader($header)) {
36
                $request = $request->withHeader($header, $headerValue);
37
            }
38
        }
39
40
        return $next($request);
41
    }
42
}
43