Passed
Push — master ( 2a9684...6905aa )
by Kevin
01:55
created

Psr16CacheStorage::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Kevinrob\GuzzleCache\CacheEntry;
6
use Psr\SimpleCache\CacheInterface;
7
8
class Psr16CacheStorage implements CacheStorageInterface
9
{
10
    /**
11
     * @var CacheInterface
12
     */
13
    private $cache;
14
15 2
    public function __construct(CacheInterface $cache)
16
    {
17 2
        $this->cache = $cache;
18 2
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 2
    public function fetch($key)
24
    {
25 2
        $data = $this->cache->get($key);
26 2
        if ($data instanceof CacheEntry) {
27 2
            return $data;
28
        }
29
30 2
        return null;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function save($key, CacheEntry $data)
37
    {
38 2
      $ttl = $data->getTTL();
39 2
      if ($ttl === 0) {
40
        return $this->cache->set($key, $data);
41
      }
42 2
      return $this->cache->set($key, $data, $data->getTTL());
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function delete($key)
49
    {
50 2
        return $this->cache->delete($key);
51
    }
52
}
53