Completed
Pull Request — master (#44)
by
unknown
36:18 queued 14s
created

HeaderAddPlugin::handleRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Http\Client\Plugin;
4
5
use Psr\Http\Message\RequestInterface;
6
7
/**
8
 * Adds headers to the request only if the request does not already own it.
9
 */
10 View Code Duplication
class HeaderAddPlugin 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...
11
{
12
13
    private $headers = [];
14
15
    /**
16
     * @param array $headers headers to add to the request
17
     */
18 3
    public function __construct(array $headers)
19
    {
20 3
        $this->headers = $headers;
21 3
    }
22
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
28
    {
29 1
        foreach ($this->headers as $header => $headerValue) {
30 1
            if (!$request->hasHeader($header)) {
31 1
                $request = $request->withHeader($header, $headerValue);
32 1
            }
33 1
        }
34
35
36 1
        return $next($request);
37
    }
38
}
39