Completed
Push — master ( 89d155...6a85db )
by Dan
13:52 queued 06:12
created

Cache::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ds\Cache;
4
5
/**
6
 * Class Cache
7
 * @package Ds\Cache
8
 */
9
class Cache implements CacheInterface
10
{
11
    /**
12
     * @var CacheStorageInterface
13
     */
14
    protected $cache;
15
16
    /**
17
     * Create new instance with $CacheStorage
18
     *
19
     * @param  CacheStorageInterface $cacheStorage
20
     * @return CacheInterface
21
     */
22
    public function withCacheStorage(CacheStorageInterface $cacheStorage)
23
    {
24
        $new = clone $this;
25
        $new->cache = $cacheStorage;
26
        return $new;
27
    }
28
29
    /**
30
     * Get Cache Storage
31
     *
32
     * @return CacheStorageInterface
33
     */
34
    public function getCacheStorage()
35
    {
36
        return $this->cache;
37
    }
38
39
    /**
40
     * Cache constructor.
41
     *
42
     * @param CacheStorageInterface|null $cacheStorage
43
     */
44
    public function __construct(CacheStorageInterface $cacheStorage = null)
45
    {
46
        $this->cache = $cacheStorage;
47
48
        if ($cacheStorage === null) {
49
            $this->cache = new NullStorage();
50
        }
51
    }
52
53
    /**
54
     * Check if cache key exists.
55
     *
56
     * @param  $key
57
     * @return bool
58
     */
59
    public function has($key)
60
    {
61
        return $this->cache->has($key);
62
    }
63
64
    /**
65
     * Save Cache Value.
66
     *
67
     * @param  string $key
68
     * @param  mixed  $value
69
     * @param  int    $expires
70
     * @return void
71
     */
72
    public function set($key, $value, $expires)
73
    {
74
        return $this->cache->set($key, $value, $expires);
75
    }
76
77
    /**
78
     * Get Cache Value from key.
79
     *
80
     * @param  string $key
81
     * @return mixed
82
     */
83
    public function get($key = '')
84
    {
85
        return $this->cache->get($key);
86
    }
87
88
    /**
89
     * Delete Cache Key.
90
     *
91
     * @param $key
92
     */
93
    public function delete($key)
94
    {
95
        $this->cache->delete($key);
96
    }
97
}
98