CacheTokenRepository::getAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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
     * @return void
70
     */
71
    private function createToken(PersistableToken $token)
72
    {
73
        $this->cache->put(
74
            $this->getKey($token->getType()),
75
            $token,
76
            $token->getExpiration()
77
        );
78
    }
79
80
    /**
81
     * Get the cache key
82
     *
83
     * @param string $type
84
     * @return string
85
     */
86
    public function getKey(string $type)
87
    {
88
        return self::PREFIX . $type;
89
    }
90
}
91