1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tbclla\Revolut\Repositories; |
4
|
|
|
|
5
|
|
|
use tbclla\Revolut\Auth\AccessToken; |
6
|
|
|
use tbclla\Revolut\Auth\RefreshToken; |
7
|
|
|
use tbclla\Revolut\Interfaces\PersistableToken; |
8
|
|
|
use tbclla\Revolut\Interfaces\TokenRepository; |
9
|
|
|
use Illuminate\Cache\Repository as Cache; |
10
|
|
|
|
11
|
|
|
class CacheTokenRepository implements TokenRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The cache key prefix |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const PREFIX = 'revolut_'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Illuminate\Cache\Repository $cache |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Cache $cache) |
24
|
|
|
{ |
25
|
|
|
$this->cache = $cache; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getAccessToken() |
29
|
|
|
{ |
30
|
|
|
return $this->getToken(AccessToken::TYPE); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getRefreshToken() |
34
|
|
|
{ |
35
|
|
|
return $this->getToken(RefreshToken::TYPE); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function createAccessToken(string $value) |
39
|
|
|
{ |
40
|
|
|
$this->createToken($accessToken = new AccessToken([ |
41
|
|
|
'value' => $value |
42
|
|
|
])); |
43
|
|
|
|
44
|
|
|
return $accessToken; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function createRefreshToken(string $value) |
48
|
|
|
{ |
49
|
|
|
$this->createToken($refreshToken = new RefreshToken([ |
50
|
|
|
'value' => $value |
51
|
|
|
])); |
52
|
|
|
|
53
|
|
|
return $refreshToken; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a token from the cache |
58
|
|
|
* |
59
|
|
|
* @param string $type |
60
|
|
|
* @return \tbclla\Revolut\Interfaces\PersistableToken|null |
61
|
|
|
*/ |
62
|
|
|
private function getToken($type) |
63
|
|
|
{ |
64
|
|
|
return $this->cache->get($this->getKey($type)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Put the token into the cache |
69
|
|
|
* |
70
|
|
|
* @param \tbclla\Revolut\Interfaces\PersistableToken $token |
71
|
|
|
*/ |
72
|
|
|
private function createToken(PersistableToken $token) |
73
|
|
|
{ |
74
|
|
|
$this->cache->put( |
75
|
|
|
$this->getKey($token->getType()), |
76
|
|
|
$token, |
77
|
|
|
$token->getExpiration() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the cache key |
83
|
|
|
* |
84
|
|
|
* @param string $type |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getKey(string $type) |
88
|
|
|
{ |
89
|
|
|
return self::PREFIX . $type; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|