AuthenticationMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 11 2
1
<?php
2
3
4
namespace Dolibarr\Client\HttpClient\Middleware;
5
6
use Dolibarr\Client\Security\Authentication\Authentication;
7
use Psr\Http\Message\RequestInterface;
8
9
/**
10
 * @author Laurent De Coninck <[email protected]>
11
 */
12
final class AuthenticationMiddleware
13
{
14
    /**
15
     * @var Authentication
16
     */
17
    private $authentication;
18
19
    /**
20
     * @param Authentication $authentication
21
     */
22
    public function __construct(Authentication $authentication)
23
    {
24
        $this->authentication = $authentication;
25
    }
26
27
    /**
28
     * @param callable $handler
29
     *
30
     * @return callable
31
     */
32
    public function __invoke(callable $handler)
33
    {
34
        return function (RequestInterface $request, array $options) use ($handler) {
35
            foreach ($this->authentication->getHeaders() as $header) {
36
                $request = $request->withHeader(
37
                    $header->getHeaderKey(),
38
                    $header->getHeaderValue()
39
                );
40
            }
41
42
            return $handler($request, $options);
43
        };
44
    }
45
}
46