JwtToken::getFreshToken()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace E2Consult\DNBApiClient;
4
5
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, E2Consult\DNBApiClient\Client. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use GuzzleHttp\Psr7\Request;
7
use Aws\Signature\SignatureV4;
8
use Aws\Credentials\Credentials;
9
use Illuminate\Support\Facades\Cache;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class JwtToken
12
{
13
    private $api_key;
14
    private $credentials;
15
16
    protected $cache_key;
17
    protected $customerId;
18
19
    protected $token;
20
    protected $signer;
21
    protected $duration = 600;
22
23
    public function __construct(Credentials $credentials, $api_key, $customerId)
24
    {
25
        $this->credentials = $credentials;
26
        $this->api_key = $api_key;
27
        $this->customerId = $customerId;
28
29
        $this->signer = new SignatureV4(
30
            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

30
            /** @scrutinizer ignore-call */ 
31
            config('services.dnb.servcice') ?? 'execute-api',
Loading history...
31
            config('services.dnb.region') ?? 'eu-west-1'
32
        );
33
34
        $this->cache_key = 'services.dnb.token.'.$this->customerId;
35
        $this->token = null;
36
    }
37
38
    public function getTokenKey()
39
    {
40
        return $this->cache_key;
41
    }
42
43
    public function getJwtToken()
44
    {
45
        if (is_null($this->token)) {
46
            if (is_null($this->token = Cache::get($this->cache_key))) {
47
                $this->token = $this->getFreshToken(true);
48
            }
49
        }
50
51
        return $this->token->jwtToken;
52
    }
53
54
    public function getFreshToken($cache = true)
55
    {
56
        $signedrequest = $this->signer->signRequest(
57
            new Request('GET', config('services.dnb.endpoint').'/token?customerId='.json_encode([
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

57
            new Request('GET', /** @scrutinizer ignore-call */ config('services.dnb.endpoint').'/token?customerId='.json_encode([
Loading history...
58
                    'type' => 'SSN',
59
                    'value' => $this->customerId
60
                ]), [
61
                    'Accept'        => 'application/json',
62
                    'Content-Type'  => 'application/json',
63
                    'x-api-key'     => $this->api_key
64
                ]
65
            ),
66
            $this->credentials
67
        );
68
        $response = (new Client)->send($signedrequest);
69
70
        return tap(collect(json_decode($response->getBody())->tokenInfo)->first(), function ($token) use ($cache) {
0 ignored issues
show
Bug introduced by
The function tap 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

70
        return /** @scrutinizer ignore-call */ tap(collect(json_decode($response->getBody())->tokenInfo)->first(), function ($token) use ($cache) {
Loading history...
Bug introduced by
The function collect 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

70
        return tap(/** @scrutinizer ignore-call */ collect(json_decode($response->getBody())->tokenInfo)->first(), function ($token) use ($cache) {
Loading history...
71
            if ($cache) {
72
                Cache::put($this->cache_key, $token, $this->duration);
73
            }
74
        });
75
    }
76
}
77