Completed
Push — master ( 49a7fe...07d1f7 )
by Guillaume
02:11
created

SimpleCacheTokenPersistence::deleteToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Eljam\GuzzleJwt\Persistence;
4
5
use Eljam\GuzzleJwt\JwtToken;
6
use Eljam\GuzzleJwt\Persistence\TokenPersistenceInterface;
7
use Psr\SimpleCache\CacheInterface;
8
9
/**
10
 * PersistenceInterface backed by a PSR-16 SimpleCache
11
 * @author Nicolas Reynis (nreynis)
12
 */
13
class SimpleCacheTokenPersistence implements TokenPersistenceInterface
14
{
15
    /**
16
     * @var CacheInterface
17
     */
18
    private $cache;
19
20
    /**
21
     * @var int
22
     */
23
    private $ttl;
24
25
    /**
26
     * @var string
27
     */
28
    private $cacheKey;
29
30
    public function __construct(CacheInterface $cache, $ttl = 1800, $cacheKey = 'eljam.jwt.token')
31
    {
32
        $this->cache = $cache;
33
        $this->ttl = $ttl;
34
        $this->cacheKey = $cacheKey;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function saveToken(JwtToken $token)
41
    {
42
        /*
43
         * TTL does not need to match token expiration,
44
         * it'll be revalidated by manager so we can safely
45
         * return a stale token.
46
         */
47
        $this->cache->set($this->cacheKey, $token, $this->ttl);
48
        return;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function restoreToken()
55
    {
56
        return $this->cache->get($this->cacheKey);
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function deleteToken()
63
    {
64
        $this->cache->deleteItem($this->cacheKey);
0 ignored issues
show
Bug introduced by
The method deleteItem() does not exist on Psr\SimpleCache\CacheInterface. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
65
        return;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function hasToken()
72
    {
73
        return $this->cache->has($this->cacheKey);
74
    }
75
}
76