Completed
Push — master ( 955d05...19c28e )
by David
03:37
created

src/Plugin/HeaderDefaultsPlugin.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 header to default value if it does not exist.
10
 *
11
 * If a given header already exists the value wont be replaced and the request wont be changed.
12
 *
13
 * @author Soufiane Ghzal <[email protected]>
14
 */
15 View Code Duplication
final class HeaderDefaultsPlugin implements Plugin
0 ignored issues
show
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...
16
{
17
    /**
18
     * @var array
19
     */
20
    private $headers = [];
21
22
    /**
23
     * @param array $headers Hashmap of header name to header value
24
     */
25 3
    public function __construct(array $headers)
26
    {
27 3
        $this->headers = $headers;
28 3
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
34
    {
35 1
        foreach ($this->headers as $header => $headerValue) {
36 1
            if (!$request->hasHeader($header)) {
37 1
                $request = $request->withHeader($header, $headerValue);
38
            }
39
        }
40
41 1
        return $next($request);
42
    }
43
}
44