AuthenticationMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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