CacheFallback   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A get() 0 8 2
A set() 0 8 2
A delete() 0 8 2
A clear() 0 8 2
A getMultiple() 0 8 2
A setMultiple() 0 8 2
A deleteMultiple() 0 8 2
A has() 0 8 2
1
<?php
2
3
namespace BenTools\Cache\Fallback;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
final class CacheFallback implements CacheInterface
8
{
9
    /**
10
     * @var CacheInterface
11
     */
12
    private $main;
13
14
    /**
15
     * @var CacheInterface
16
     */
17
    private $fallback;
18
19
    public function __construct(CacheInterface $main, CacheInterface $fallback, CacheInterface ...$fallbacks)
20
    {
21
        $this->main = $main;
22
        $nextFallback = \array_shift($fallbacks);
23
        $this->fallback = null !== $nextFallback ? new self($fallback, $nextFallback, ...$fallbacks) : $fallback;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function get($key, $default = null)
30
    {
31
        try {
32
            return $this->main->get($key, $default);
33
        } catch (\Exception $e) {
34
            return $this->fallback->get($key, $default);
35
        }
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function set($key, $value, $ttl = null)
42
    {
43
        try {
44
            return $this->main->set($key, $value, $ttl);
45
        } catch (\Exception $e) {
46
            return $this->fallback->set($key, $value, $ttl);
47
        }
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function delete($key)
54
    {
55
        try {
56
            return $this->main->delete($key);
57
        } catch (\Exception $e) {
58
            return $this->fallback->delete($key);
59
        }
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function clear()
66
    {
67
        try {
68
            return $this->main->clear();
69
        } catch (\Exception $e) {
70
            return $this->fallback->clear();
71
        }
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function getMultiple($keys, $default = null)
78
    {
79
        try {
80
            return $this->main->getMultiple($keys, $default);
81
        } catch (\Exception $e) {
82
            return $this->fallback->getMultiple($keys, $default);
83
        }
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function setMultiple($values, $ttl = null)
90
    {
91
        try {
92
            return $this->main->setMultiple($values, $ttl);
93
        } catch (\Exception $e) {
94
            return $this->fallback->setMultiple($values, $ttl);
95
        }
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function deleteMultiple($keys)
102
    {
103
        try {
104
            return $this->main->deleteMultiple($keys);
105
        } catch (\Exception $e) {
106
            return $this->fallback->deleteMultiple($keys);
107
        }
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function has($key)
114
    {
115
        try {
116
            return $this->main->has($key);
117
        } catch (\Exception $e) {
118
            return $this->fallback->has($key);
119
        }
120
    }
121
}
122