RefreshToken::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.9666
ccs 13
cts 13
cp 1
crap 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