Completed
Push — master ( 789cc6...743104 )
by David
53:58 queued 32:45
created

HeaderDefaultsPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
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
 * Set default values for the request headers.
9
 * If a given header already exists the value wont be replaced and the request wont be changed.
10
 *
11
 * @author Soufiane Ghzal <[email protected]>
12
 */
13 View Code Duplication
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...
14
{
15
    private $headers = [];
16
17
    /**
18
     * @param array $headers headers to set to the request
19
     */
20 3
    public function __construct(array $headers)
21
    {
22 3
        $this->headers = $headers;
23 3
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
29
    {
30 1
        foreach ($this->headers as $header => $headerValue) {
31 1
            if (!$request->hasHeader($header)) {
32 1
                $request = $request->withHeader($header, $headerValue);
33 1
            }
34 1
        }
35
36 1
        return $next($request);
37
    }
38
}
39