TokenStore   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCacheKey() 0 3 1
A get() 0 19 2
1
<?php
2
3
namespace Pelmered\LaravelHttpOAuthHelper;
4
5
use Exception;
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\Str;
8
9
class TokenStore
10
{
11 5
    protected static function generateCacheKey(string $refreshUrl): string
12
    {
13 5
        return 'oauth_token_'.Str::of($refreshUrl)->replace(['https://', '/'], ['', '-'])->__toString();
14
    }
15
16
    /**
17
     * @throws Exception|\Psr\SimpleCache\InvalidArgumentException
18
     */
19 8
    public static function get(
20
        string $refreshUrl,
21
        Credentials $credentials,
22
        Options $options,
23
    ): AccessToken {
24 8
        $cacheKey = $options->cacheKey ?? static::generateCacheKey($refreshUrl);
25
26 8
        $accessToken = Cache::store($options->cacheDriver)->get($cacheKey);
27
28 8
        if ($accessToken) {
29 2
            return $accessToken;
30
        }
31
32 8
        $accessToken = app(RefreshToken::class)(...func_get_args());
33 8
        $ttl         = $accessToken->getExpiresIn();
34
35 8
        Cache::store($options->cacheDriver)->put($cacheKey, $accessToken, $ttl);
36
37 8
        return $accessToken;
38
    }
39
}
40