OAuth::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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