SimpleCacheTokenPersistence   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A saveToken() 0 4 1
A restoreToken() 0 10 2
A deleteToken() 0 4 1
A hasToken() 0 4 1
1
<?php
2
3
namespace LibLynx\Connect\HTTPClient;
4
5
use kamermans\OAuth2\Persistence\TokenPersistenceInterface;
6
use kamermans\OAuth2\Token\TokenInterface;
7
use Psr\SimpleCache\CacheInterface;
8
9
class SimpleCacheTokenPersistence implements TokenPersistenceInterface
10
{
11
    /**
12
     * @var CacheInterface
13
     */
14
    private $cache;
15
16
    /**
17
     * @var string
18
     */
19
    private $cacheKey;
20
21
    public function __construct(CacheInterface $cache, $cacheKey = 'guzzle-oauth2-token')
22
    {
23
        $this->cache = $cache;
24
        $this->cacheKey = $cacheKey;
25
    }
26
27
    public function saveToken(TokenInterface $token)
28
    {
29
        $this->cache->set($this->cacheKey, $token->serialize());
30
    }
31
32
    public function restoreToken(TokenInterface $token)
33
    {
34
        $data = $this->cache->get($this->cacheKey);
35
36
        if (!is_array($data)) {
37
            return null;
38
        }
39
40
        return $token->unserialize($data);
41
    }
42
43
    public function deleteToken()
44
    {
45
        $this->cache->delete($this->cacheKey);
46
    }
47
48
    public function hasToken()
49
    {
50
        $this->cache->has($this->cacheKey);
51
    }
52
}
53