Passed
Pull Request — main (#6)
by Peter
04:29
created

RefreshToken   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 28
c 6
b 0
f 0
dl 0
loc 70
ccs 25
cts 31
cp 0.8065
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 23 1
A getExpiresAtFromResponse() 0 21 6
A getAccessTokenFromResponse() 0 3 2
A resolveRefreshAuth() 0 4 1
1
<?php
2
3
namespace Pelmered\LaravelHttpOAuthHelper;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Illuminate\Http\Client\PendingRequest;
8
use Illuminate\Http\Client\Response;
9
use Illuminate\Support\Facades\Http;
10
use InvalidArgumentException;
11
12
class RefreshToken
13
{
14
    protected PendingRequest $httpClient;
15
16
    /**
17
     * @var array<string, mixed>
18
     */
19
    protected array $requestBody = [];
20
21
    /**
22
     * @throws Exception
23
     */
24 13
    public function __invoke(
25
        string $refreshUrl,
26
        Credentials $credentials,
27
        Options $options,
28
    ): AccessToken {
29 13
        $this->httpClient = Http::asForm();
30
31 13
        $this->requestBody = [
32 13
            'grant_type' => $options->grantType,
33 13
            'scope'      => $options->getScopes(),
34 13
        ];
35
36 13
        $this->resolveRefreshAuth($credentials, $options);
37
38 13
        $response = $this->httpClient->post($refreshUrl, $this->requestBody);
39
40 13
        return new AccessToken(
41 13
            accessToken: $this->getAccessTokenFromResponse($response, $options->accessToken),
42 13
            expiresAt: $this->getExpiresAtFromResponse($response, $options->expires),
43
            //tokenType: $options['auth_type'],
44 13
            tokenType: $options->tokenType,
45 13
            customCallback: $options->tokenTypeCustomCallback,
46 13
            tokenName: $options->tokenName,
47 13
        );
48
    }
49
50 13
    protected function resolveRefreshAuth(Credentials $credentials, Options $options): void
51
    {
52 13
        $this->httpClient  = $credentials->addAuthToRequest($this->httpClient, $options);
53 13
        $this->requestBody = $credentials->addAuthToBody($this->requestBody, $options);
54
    }
55
56 13
    protected function getAccessTokenFromResponse(Response $response, callable|string $accessTokenOption): string
57
    {
58 13
        return is_callable($accessTokenOption) ? $accessTokenOption($response) : $response->json()[$accessTokenOption];
59
    }
60
61 13
    protected function getExpiresAtFromResponse(Response $response, callable|string|int|Carbon $expiresOption): Carbon
62
    {
63 13
        $expires = is_callable($expiresOption) ? $expiresOption($response) : $expiresOption;
64
65 13
        if (is_string($expires)) {
66
            if (isset($response->json()[$expires])) {
67
                $expires = $response->json()[$expires];
68
            }
69
70
            return Carbon::parse($expires)->subMinute();
71
        }
72
73 13
        if (is_int($expires)) {
74 13
            return Carbon::now()->addSeconds($expires)->subMinute();
75
        }
76
77
        if ($expires instanceof Carbon) {
78
            return $expires->subMinute();
79
        }
80
81
        throw new InvalidArgumentException('Invalid expires option');
82
    }
83
}
84