DNBRequestHandler::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace E2Consult\DNBApiClient;
4
5
use Aws\Signature\SignatureV4;
6
use Aws\Credentials\Credentials;
7
use Psr\Http\Message\RequestInterface;
8
9
class DNBRequestHandler
10
{
11
    private $api_key;
12
    private $customerId;
13
    private $credentials;
14
15
    protected $signer;
16
    protected $token;
17
18
    public function __construct(Credentials $credentials, $api_key, $customerId)
19
    {
20
        $this->api_key = $api_key;
21
        $this->customerId = $customerId;
22
        $this->credentials = $credentials;
23
24
        $this->signer = new SignatureV4(
25
            config('services.dnb.servcice') ?? 'execute-api',
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            /** @scrutinizer ignore-call */ 
26
            config('services.dnb.servcice') ?? 'execute-api',
Loading history...
26
            config('services.dnb.region') ?? 'eu-west-1'
27
        );
28
        $this->token = new JwtToken($this->credentials, $this->api_key, $this->customerId);
29
    }
30
31
    public function __invoke(callable $handler)
32
    {
33
        return function (RequestInterface $request, array $options) use ($handler) {
34
            return $handler(
35
                $this->signer->signRequest(
36
                    $request->withAddedHeader('Accept', 'application/json')
37
                        ->withAddedHeader('Content-Type', 'application/json')
38
                        ->withAddedHeader('x-api-key', $this->api_key)
39
                        ->withAddedHeader('x-dnbapi-jwt', $this->token->getJwtToken()),
40
                    $this->credentials
41
                ), $options);
42
        };
43
    }
44
}
45