Passed
Push — master ( ab5cea...195c1d )
by Dominic
02:29
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\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;
0 ignored issues
show
Bug Best Practice introduced by
The property cache does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
	}
27
28
	public function getAccessToken()
29
	{
30
		return $this->getToken(AccessToken::TYPE);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getToken(t...Auth\AccessToken::TYPE) also could return the type tbclla\Revolut\Interfaces\PersistableToken which includes types incompatible with the return type mandated by tbclla\Revolut\Interface...itory::getAccessToken() of null|tbclla\Revolut\Auth\AccessToken. Consider adding a type-check to rule them out.
Loading history...
31
	}
32
33
	public function getRefreshToken()
34
	{
35
		return $this->getToken(RefreshToken::TYPE);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getToken(t...uth\RefreshToken::TYPE) also could return the type tbclla\Revolut\Interfaces\PersistableToken which includes types incompatible with the return type mandated by tbclla\Revolut\Interface...tory::getRefreshToken() of null|tbclla\Revolut\Auth\RefreshToken. Consider adding a type-check to rule them out.
Loading history...
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