OAuth   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 37
ccs 9
cts 9
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A requestToken() 0 21 1
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\RequestOptions;
6
use Sebdesign\VivaPayments\Responses\AccessToken;
7
8
class OAuth
9
{
10 6
    public function __construct(
11
        public readonly Client $client,
12
        public readonly string $clientId,
13
        public readonly string $clientSecret,
14
    ) {
15
    }
16
17
    /**
18
     * Request access token.
19
     *
20
     * @see https://developer.vivawallet.com/integration-reference/oauth2-authentication/
21
     *
22
     * @param  array<string,mixed>  $guzzleOptions  Additional options for the Guzzle client
23
     */
24 15
    public function requestToken(
25
        #[\SensitiveParameter] ?string $clientId = null,
26
        #[\SensitiveParameter] ?string $clientSecret = null,
27
        array $guzzleOptions = []
28
    ): AccessToken {
29 15
        $parameters = ['grant_type' => 'client_credentials'];
30
31 15
        $response = $this->client->post(
32 15
            $this->client->getAccountsUrl()->withPath('/connect/token'),
33
            [
34 5
                RequestOptions::FORM_PARAMS => $parameters,
35
                RequestOptions::AUTH => [
36 15
                    $clientId ?? $this->clientId,
37 15
                    $clientSecret ?? $this->clientSecret,
38
                ],
39
                ...$guzzleOptions,
40
            ]
41
        );
42
43
        /** @phpstan-ignore-next-line */
44 15
        return new AccessToken(...$response);
45
    }
46
}
47