Passed
Push — main ( a00e1e...c2dc62 )
by Peter
03:59
created

RefreshToken   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 30
c 6
b 0
f 0
dl 0
loc 74
ccs 33
cts 33
cp 1
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 23 1
A getAccessTokenFromResponse() 0 3 2
A resolveRefreshAuth() 0 4 1
B getExpiresAtFromResponse() 0 25 7
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 24
    public function __invoke(
25
        string $refreshUrl,
26
        Credentials $credentials,
27
        Options $options,
28
    ): AccessToken {
29 24
        $this->httpClient = Http::asForm();
30
31 24
        $this->requestBody = [
32 24
            'grant_type' => $options->grantType,
33 24
            'scope'      => $options->getScopes(),
34 24
        ];
35
36 24
        $this->resolveRefreshAuth($credentials, $options);
37
38 23
        $response = $this->httpClient->post($refreshUrl, $this->requestBody);
39
40 23
        return new AccessToken(
41 23
            accessToken: $this->getAccessTokenFromResponse($response, $options->accessToken),
42 23
            expiresAt: $this->getExpiresAtFromResponse($response, $options->expires),
43
            //tokenType: $options['auth_type'],
44 23
            tokenType: $options->tokenType,
45 23
            customCallback: $options->tokenTypeCustomCallback,
46 23
            tokenName: $options->tokenName,
47 23
        );
48
    }
49
50 24
    protected function resolveRefreshAuth(Credentials $credentials, Options $options): void
51
    {
52 24
        $this->httpClient  = $credentials->addAuthToRequest($this->httpClient, $options);
53 23
        $this->requestBody = $credentials->addAuthToBody($this->requestBody, $options);
54
    }
55
56 23
    protected function getAccessTokenFromResponse(Response $response, callable|string $accessTokenOption): string
57
    {
58 23
        return is_callable($accessTokenOption) ? $accessTokenOption($response) : $response->json()[$accessTokenOption];
59
    }
60
61 23
    protected function getExpiresAtFromResponse(Response $response, callable|string|int|Carbon $expiresOption): Carbon
62
    {
63 23
        $expires = is_callable($expiresOption) ? $expiresOption($response) : $expiresOption;
64
65 23
        if (is_string($expires)) {
66 2
            if (isset($response->json()[$expires])) {
67 2
                $expires = $response->json()[$expires];
68
            }
69
70 2
            if (is_int($expires)) {
71 1
                return Carbon::now()->addSeconds($expires - 60);
72
            }
73
74 1
            return Carbon::parse($expires)->subMinute();
75
        }
76
77 21
        if (is_int($expires)) {
78 19
            return Carbon::now()->addSeconds($expires)->subMinute();
79
        }
80
81 2
        if ($expires instanceof Carbon) {
82 1
            return $expires->subMinute();
83
        }
84
85 1
        throw new InvalidArgumentException('Invalid expires option');
86
    }
87
}
88