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

WordPressObjectCacheStorage::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
c 1
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Kevinrob\GuzzleCache\CacheEntry;
6
7
class WordPressObjectCacheStorage implements CacheStorageInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $group;
13
14
    /**
15
     * @param string $group
16
     */
17
    public function __construct($group = 'guzzle')
18
    {
19
        $this->group = $group;
20
    }
21
22
    /**
23
     * @param string $key
24
     *
25
     * @return CacheEntry|null the data or false
26
     */
27
    public function fetch($key)
28
    {
29
        try {
30
            $cache = unserialize(wp_cache_get($key, $this->group));
31
            if ($cache instanceof CacheEntry) {
32
                return $cache;
33
            }
34
        } catch (\Exception $ignored) {
35
            // Don't fail if we can't load it
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param CacheEntry $data
44
     *
45
     * @return bool
46
     */
47
    public function save($key, CacheEntry $data)
48
    {
49
        try {
50
            return wp_cache_set($key, serialize($data), $this->group, $data->getTTL());
51
        } catch (\Exception $ignored) {
52
            // Don't fail if we can't save it
53
        }
54
55
        return false;
56
    }
57
58
    /**
59
     * @param string $key
60
     *
61
     * @return bool
62
     */
63
    public function delete($key)
64
    {
65
        try {
66
            return wp_cache_delete($key, $this->group);
67
        } catch (\Exception $ignored) {
68
            // Don't fail if we can't delete it
69
        }
70
71
        return false;
72
    }
73
}
74