CacheBackend::getCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Flying\Struct\Storage;
4
5
use Doctrine\Common\Cache\Cache;
6
use Flying\Struct\ConfigurationManager;
7
8
/**
9
 * Implementation of structures storage backend using structures cache
10
 */
11
class CacheBackend implements BackendInterface
12
{
13
    /**
14
     * Cache to use as a storage
15
     *
16
     * @var Cache
17
     */
18
    private $cache;
19
20
    /**
21
     * Load information by given key from storage
22
     *
23
     * @param string $key
24
     * @return mixed
25
     */
26 1
    public function load($key)
27
    {
28 1
        $contents = $this->getCache()->fetch($key);
29 1
        if ($contents === false) {
30 1
            return null;
31
        }
32 1
        return $contents;
33
    }
34
35
    /**
36
     * Get cache to use as storage
37
     *
38
     * @return Cache
39
     */
40 1
    protected function getCache()
41
    {
42 1
        if (!$this->cache) {
43 1
            $this->cache = ConfigurationManager::getConfiguration()->getCache();
44 1
        }
45 1
        return $this->cache;
46
    }
47
48
    /**
49
     * Save given contents into storage
50
     *
51
     * @param string $key
52
     * @param mixed $contents
53
     * @return void
54
     */
55 1
    public function save($key, $contents)
56
    {
57 1
        $this->getCache()->save($key, $contents);
58 1
    }
59
60
    /**
61
     * Check if storage has an entry with given key
62
     *
63
     * @param string $key
64
     * @return boolean
65
     */
66 1
    public function has($key)
67
    {
68 1
        return $this->getCache()->contains($key);
69
    }
70
71
    /**
72
     * Remove storage entry with given key
73
     *
74
     * @param string $key
75
     * @return void
76
     */
77 1
    public function remove($key)
78
    {
79 1
        $this->getCache()->delete($key);
80 1
    }
81
82
    /**
83
     * Clear storage contents
84
     *
85
     * @return void
86
     */
87
    public function clear()
88
    {
89
        // Cache doesn't have such function
90
    }
91
}
92