TokenStore::generateCacheKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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