Passed
Push — 2.x ( 3908db...6bae43 )
by Darío
07:58
created

PayPalApi   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
dl 0
loc 46
ccs 19
cts 20
cp 0.95
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withHandler() 0 3 1
A setCredentials() 0 4 1
A setAuthentication() 0 4 1
A getToken() 0 15 2
1
<?php
2
3
namespace PaymentGateway\PayPalSdk;
4
5
use EasyHttp\GuzzleLayer\GuzzleClient;
6
use EasyHttp\LayerContracts\Contracts\EasyClientContract;
7
use EasyHttp\LayerContracts\Contracts\HttpClientRequest;
8
9
abstract class PayPalApi
10
{
11
    protected EasyClientContract $client;
12
    protected string $baseUri;
13
    protected string $username;
14
    protected string $password;
15
    protected array $token;
16
17 18
    public function __construct(string $baseUri)
18
    {
19 18
        $this->baseUri = $baseUri;
20 18
        $this->client = new GuzzleClient();
21
    }
22
23 18
    public function setCredentials(string $username, string $password)
24
    {
25 18
        $this->username = $username;
26 18
        $this->password = $password;
27
    }
28
29 18
    public function withHandler(callable $handler)
30
    {
31 18
        $this->client->withHandler($handler);
32
    }
33
34 18
    protected function getToken(): array
35
    {
36 18
        if ($this->token ?? null) {
37
            return $this->token;
38
        }
39
40 18
        $client = clone $this->client;
41
42 18
        $client->prepareRequest('POST', $this->baseUri . '/v1/oauth2/token');
43 18
        $client->getRequest()->setBasicAuth($this->username, $this->password);
0 ignored issues
show
Bug introduced by
The method setBasicAuth() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest. It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest. ( Ignorable by Annotation )

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

43
        $client->getRequest()->/** @scrutinizer ignore-call */ setBasicAuth($this->username, $this->password);
Loading history...
44 18
        $client->getRequest()->setQuery(['grant_type' => 'client_credentials']);
45
46 18
        $this->token = $client->execute()->parseJson();
47
48 18
        return $this->token;
49
    }
50
51 18
    protected function setAuthentication(): HttpClientRequest
52
    {
53 18
        return $this->client->getRequest()
54 18
            ->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']);
55
    }
56
}
57