LaravelCacheAccessTokenCacher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace LaravelIproSoftwareApi\AccessToken;
4
5
use Illuminate\Contracts\Cache\Repository as Cache;
6
use IproSoftwareApi\Contracts\AccessToken;
7
use IproSoftwareApi\Contracts\AccessTokenCacher;
8
9
class LaravelCacheAccessTokenCacher implements AccessTokenCacher
10
{
11
    /** @var string */
12
    protected $key;
13
14
    /** @var Cache */
15
    protected $cache;
16
17
    /**
18
     * LaravelCacheAccessTokenCacher constructor.
19
     * @param Cache $cache
20
     * @param string $key
21
     */
22 2
    public function __construct(Cache $cache, string $key = 'iprosoftware-api-access-token')
23
    {
24 2
        $this->key   = $key;
25 2
        $this->cache = $cache;
26
    }
27
28
    /**
29
     * Store an item in the cache.
30
     *
31
     * @param mixed $accessToken
32
     * @param int $ttl - time in seconds
33
     *
34
     * @return bool
35
     * @static
36
     */
37 1
    public function put(AccessToken $accessToken, int $ttl = null)
38
    {
39 1
        return $this->cache->put($this->key, serialize($accessToken), $ttl ?? null);
40
    }
41
42
    /**
43
     * Retrieve an item from the cache by key.
44
     *
45
     * @return \IproSoftwareApi\Contracts\AccessToken|null
46
     * @static
47
     */
48 1
    public function get(): ?AccessToken
49
    {
50 1
        $token = unserialize($this->cache->get($this->key));
51
52 1
        return ($token instanceof AccessToken) ? $token : null;
53
    }
54
}
55