ClientCredentials   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
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 41
rs 10
ccs 22
cts 22
cp 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getToken() 0 15 1
A __construct() 0 15 2
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