Cache::has()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 8
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
use Sugarcrm\UpgradeSpec\Cache\Exception\CacheException;
7
8
class Cache
9
{
10
    /**
11
     * @var CacheInterface
12
     */
13
    private $cacheAdapter;
14
15
    /**
16
     * Cache constructor.
17
     *
18
     * @param CacheInterface $cacheAdapter
19
     */
20
    public function __construct(CacheInterface $cacheAdapter)
21
    {
22
        $this->cacheAdapter = $cacheAdapter;
23
    }
24
25
    /**
26
     * @param $key
27
     *
28
     * @return bool
29
     */
30 View Code Duplication
    public function has($key)
31
    {
32
        try {
33
            return $this->cacheAdapter->has($key);
34
        } catch (CacheException $e) {
35
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
36
        }
37
    }
38
39
    /**
40
     * @param $key
41
     * @param null $default
42
     *
43
     * @return mixed
44
     */
45 View Code Duplication
    public function get($key, $default = null)
46
    {
47
        try {
48
            if (is_array($key) || $key instanceof \Traversable) {
49
                return $this->cacheAdapter->getMultiple($key, $default);
50
            }
51
52
            return $this->cacheAdapter->get($key, $default);
53
        } catch (CacheException $e) {
54
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
55
        }
56
    }
57
58
    /**
59
     * @param $key
60
     * @param $value
61
     * @param null $ttl
62
     *
63
     * @return bool
64
     */
65 View Code Duplication
    public function set($key, $value, $ttl = null)
66
    {
67
        try {
68
            return $this->cacheAdapter->set($key, $value, $ttl);
69
        } catch (CacheException $e) {
70
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
71
        }
72
    }
73
74
    /**
75
     * @param $values
76
     * @param null $ttl
77
     *
78
     * @return bool
79
     */
80
    public function setMultiple($values, $ttl = null)
81
    {
82
        try {
83
            return $this->cacheAdapter->setMultiple($values, $ttl);
84
        } catch (CacheException $e) {
85
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
86
        }
87
    }
88
89
    /**
90
     * @param $key
91
     *
92
     * @return bool
93
     */
94 View Code Duplication
    public function delete($key)
95
    {
96
        try {
97
            if (is_array($key) || $key instanceof \Traversable) {
98
                return $this->cacheAdapter->deleteMultiple($key);
99
            }
100
101
            return $this->cacheAdapter->delete($key);
102
        } catch (CacheException $e) {
103
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
104
        }
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function clear()
111
    {
112
        try {
113
            return $this->cacheAdapter->clear();
114
        } catch (CacheException $e) {
115
            throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
116
        }
117
    }
118
}
119