Passed
Push — master ( 78880b...ac7e2f )
by Jens
20:00 queued 13s
created

CacheTokenProvider::refreshToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Client\OAuth;
7
8
use Psr\Cache\CacheItemPoolInterface;
9
use Psr\SimpleCache\CacheInterface;
10
11
class CacheTokenProvider implements RefreshTokenProvider
12
{
13
    const TOKEN_CACHE_KEY = 'commercetools_io_access_token';
14
15
    /**
16
     * @var TokenProvider
17
     */
18
    private $tokenProvider;
19
20
    /**
21
     * @var CacheItemPoolInterface
22
     */
23
    private $cache;
24
25
    private $cacheKey;
26
27
    /**
28
     * CacheTokenProvider constructor.
29
     * @param RefreshTokenProvider $tokenProvider
30
     * @param CacheItemPoolInterface|CacheInterface $cache
31
     * @param string $cacheKey
32
     */
33 9
    public function __construct(RefreshTokenProvider $tokenProvider, $cache, $cacheKey = null)
34
    {
35 9
        $this->tokenProvider = $tokenProvider;
36 9
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache can also be of type Psr\SimpleCache\CacheInterface. However, the property $cache is declared as type Psr\Cache\CacheItemPoolInterface. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37 9
        $this->cacheKey = self::TOKEN_CACHE_KEY . "_" . (!is_null($cacheKey) ? $cacheKey : sha1('access_token'));
38 9
    }
39
40
41
    /**
42
     * @inheritDoc
43
     */
44 7
    public function getToken()
45
    {
46 7
        $item = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
47
48 7
        $token = $this->getCacheToken();
49 7
        if (!is_null($token)) {
50 6
            return new Token($token);
51
        }
52
53 1
        return $this->refreshToken();
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 2
    public function refreshToken()
60
    {
61 2
        $token = $this->tokenProvider->refreshToken();
0 ignored issues
show
Bug introduced by
The method refreshToken() does not exist on Commercetools\Core\Client\OAuth\TokenProvider. It seems like you code against a sub-type of said class. However, the method does not exist in Commercetools\Core\Clien...th\PreAuthTokenProvider. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        /** @scrutinizer ignore-call */ 
62
        $token = $this->tokenProvider->refreshToken();
Loading history...
62
        // ensure token to be invalidated in cache before TTL
63 2
        $ttl = max(1, floor($token->getTtl()/2));
64
65 2
        $this->cache($token, $ttl);
66
67 2
        return $token;
68
    }
69
70 7
    private function getCacheToken()
71
    {
72 7
        $cache = $this->cache;
73 7
        if ($cache instanceof CacheInterface) {
74 7
            $var = $cache->get($this->cacheKey, null);
75 7
            return $var;
76
        } elseif ($cache instanceof CacheItemPoolInterface) {
0 ignored issues
show
introduced by
$cache is always a sub-type of Psr\Cache\CacheItemPoolInterface.
Loading history...
77
            $item = $cache->getItem($this->cacheKey);
78
            if ($item->isHit()) {
79
                return $item->get();
80
            }
81
        }
82
83
        return null;
84
    }
85
86 2
    private function cache(Token $token, $ttl)
87
    {
88 2
        $cache = $this->cache;
89 2
        if ($cache instanceof CacheInterface) {
90 2
            $cache->set($this->cacheKey, $token->getToken(), (int)$ttl);
91
        } elseif ($cache instanceof CacheItemPoolInterface) {
0 ignored issues
show
introduced by
$cache is always a sub-type of Psr\Cache\CacheItemPoolInterface.
Loading history...
92
            $item = $cache->getItem($this->cacheKey)->set($token->getToken())->expiresAfter((int)$ttl);
93
            $cache->save($item);
94
        }
95 2
    }
96
}
97