RefreshToken   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 46
rs 10
ccs 22
cts 22
cp 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getToken() 0 15 1
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Auth\GrantTypes;
4
5
use GuzzleHttp\ClientInterface;
6
7
class RefreshToken implements GrantTypeInterface
8
{
9
    private ClientInterface $client;
10
11
    private array $config;
12
13
    /**
14
     * @throws \InvalidArgumentException
15
     */
16 3
    public function __construct(ClientInterface $client, array $config)
17
    {
18 3
        $this->client = $client;
19
20 3
        $required = ['client_id', 'client_secret', 'token_uri'];
21
22 3
        if ($missing = array_diff($required, array_keys($config))) {
23 1
            $message = 'Parameters: '.implode(', ', $missing).' are required.';
24
25 1
            throw new \InvalidArgumentException($message, 0);
26
        }
27
28 2
        $this->config = array_merge([
29 2
            'scope' => '',
30 2
        ], $config);
31
    }
32
33
    /**
34
     * @return array<string, mixed>
35
     *
36
     * @throws \GuzzleHttp\Exception\TransferException
37
     */
38 2
    public function getToken(?string $refreshToken = null): array
39
    {
40 2
        $response = $this->client->request('POST', $this->config['token_uri'], [
41 2
            'headers' => [
42 2
                'Accept' => 'application/json',
43 2
                'Content-Type' => 'application/json',
44 2
                'Authorization' => 'Basic '.base64_encode($this->config['client_id'].':'.$this->config['client_secret']),
45 2
            ],
46 2
            'json' => [
47 2
                'grant_type' => 'refresh_token',
48 2
                'refresh_token' => $refreshToken,
49 2
            ],
50 2
        ]);
51
52 2
        return json_decode($response->getBody(), true);
53
    }
54
}
55