Completed
Push — master ( 72e23c...e2e4ad )
by Paul
04:11
created

SimpleCacheTokenPersistence::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace LibLynx\Connect;
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