Passed
Push — master ( 195c1d...80cc67 )
by Dominic
02:46
created

CacheTokenRepository::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Contracts\Cache\Factory as CacheFactory;
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
	 * A cache repository
22
	 * 
23
	 * @var \Illuminate\Cache\Repository
24
	 */
25
	private $cache;
26
27
	/**
28
	 * @param \Illuminate\Contracts\Cache\Factory $cache
29
	 * @param string $driver
30
	 * @return void
31
	 */
32
	public function __construct(CacheFactory $cache, string $driver = null)
33
	{
34
		$this->cache = $cache->store($driver);
35
	}
36
37
	public function getAccessToken()
38
	{
39
		return $this->cache->get($this->getKey(AccessToken::TYPE));
40
	}
41
42
	public function getRefreshToken()
43
	{
44
		return $this->cache->get($this->getKey(RefreshToken::TYPE));
45
	}
46
47
	public function createAccessToken(string $value)
48
	{
49
		$this->createToken($accessToken = new AccessToken([
50
			'value' => $value
51
		]));
52
53
		return $accessToken;
54
	}
55
56
	public function createRefreshToken(string $value)
57
	{
58
		$this->createToken($refreshToken = new RefreshToken([
59
			'value' => $value
60
		]));
61
62
		return $refreshToken;
63
	}
64
65
	/**
66
	 * Put the token into the cache
67
	 * 
68
	 * @param \tbclla\Revolut\Interfaces\PersistableToken $token
69
	 */
70
	private function createToken(PersistableToken $token)
71
	{
72
		$this->cache->put(
73
			$this->getKey($token->getType()),
74
			$token,
75
			$token->getExpiration()
76
		);
77
	}
78
79
	/**
80
	 * Get the cache key
81
	 *
82
	 * @param string $type
83
	 * @return string
84
	 */
85
	public function getKey(string $type)
86
	{
87
		return self::PREFIX . $type;
88
	}
89
}
90