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; |
|
|
|
|
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; |
|
|
|
|
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(); |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
92
|
|
|
$item = $cache->getItem($this->cacheKey)->set($token->getToken())->expiresAfter((int)$ttl); |
93
|
|
|
$cache->save($item); |
94
|
|
|
} |
95
|
2 |
|
} |
96
|
|
|
} |
97
|
|
|
|
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 theid
property of an instance of theAccount
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.