Passed
Push — master ( d8c2a1...59198c )
by Kevin
07:55
created

LaravelCacheStorage::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Illuminate\Contracts\Cache\Repository as Cache;
6
use Kevinrob\GuzzleCache\CacheEntry;
7
8
class LaravelCacheStorage implements CacheStorageInterface
9
{
10
    /**
11
     * @var Cache
12
     */
13
    protected $cache;
14
15
    /**
16
     * @param Cache $cache
17
     */
18
    public function __construct(Cache $cache)
19
    {
20
        $this->cache = $cache;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function fetch($key)
27
    {
28
        try {
29
            $cache = unserialize($this->cache->get($key));
30
            if ($cache instanceof CacheEntry) {
31
                return $cache;
32
            }
33
        } catch (\Exception $ignored) {
34
            return;
35
        }
36
37
        return;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function save($key, CacheEntry $data)
44
    {
45
        try {
46
            // getTTL returns seconds, Laravel needs minutes
47
            $lifeTime = $data->getTTL() / 60;
48
            if ($lifeTime === 0) {
49
                return $this->cache->forever(
50
                    $key,
51
                    serialize($data)
52
                );
53
            } else if ($lifeTime > 0) {
54
                return $this->cache->add(
55
                    $key,
56
                    serialize($data),
57
                    $lifeTime
58
                );
59
            }
60
        } catch (\Exception $ignored) {
61
            // No fail if we can't save it the storage
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function delete($key)
71
    {
72
        return $this->cache->forget($key);
73
    }
74
}
75