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

Psr6CacheStorage::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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