ClientCredentials::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 ClientCredentials implements GrantTypeInterface
8
{
9
    private ClientInterface $client;
10
11
    private array $config;
12
13
    /**
14
     * @throws \InvalidArgumentException
15
     */
16 10
    public function __construct(ClientInterface $client, array $config)
17
    {
18 10
        $this->client = $client;
19
20 10
        $required = ['client_id', 'client_secret', 'token_uri'];
21
22 10
        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 9
        $this->config = array_merge([
29 9
            'scope' => '',
30 9
        ], $config);
31
    }
32
33 4
    public function getToken(?string $refreshToken = null): array
34
    {
35 4
        $response = $this->client->request('POST', $this->config['token_uri'], [
36 4
            'headers' => [
37 4
                'Accept' => 'application/json',
38 4
                'Content-Type' => 'application/json',
39 4
                'Authorization' => 'Basic '.base64_encode($this->config['client_id'].':'.$this->config['client_secret']),
40 4
            ],
41 4
            'json' => [
42 4
                'grant_type' => 'client_credentials',
43 4
                'scope' => $this->config['scope'],
44 4
            ],
45 4
        ]);
46
47 3
        return json_decode($response->getBody(), true);
48
    }
49
}
50