Completed
Pull Request — master (#46)
by David
02:57
created

Psr6CacheStorage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 14 3
A save() 0 7 1
1
<?php
2
3
namespace Kevinrob\GuzzleCache\Storage;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Kevinrob\GuzzleCache\CacheEntry;
7
8
class Psr6CacheStorage implements CacheStorageInterface
9
{
10
    /**
11
     * @var CacheItemPoolInterface
12
     */
13
    protected $cachePool;
14
15
    /**
16
     * @param CacheItemPoolInterface $cachePool
17
     */
18
    public function __construct(CacheItemPoolInterface $cachePool)
19
    {
20
        $this->cachePool = $cachePool;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function fetch($key)
27
    {
28
        $item = $this->cachePool->getItem($key);
29
30
        if ($item->isHit()) {
31
            $cache = unserialize($item->get());
32
33
            if ($cache instanceof CacheEntry) {
34
                return $cache;
35
            }
36
        }
37
38
        return;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function save($key, CacheEntry $data)
45
    {
46
        $item = $this->cachePool->getItem($key);
47
        $item->set(serialize($data));
48
49
        return $this->cachePool->save($item);
50
    }
51
}
52