Completed
Push — master ( c8fd93...a9ab00 )
by Georges
01:49
created

PsrCacheAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 9 2
A save() 0 8 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author PastisD https://github.com/PastisD
13
 * @author Alexander (asm89) <[email protected]>
14
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
15
 *
16
 */
17
18
namespace phpFastCache\Bundle\Twig\CacheExtension\CacheProvider;
19
20
use phpFastCache\Bundle\Twig\CacheExtension\CacheProviderInterface;
21
use Psr\Cache\CacheItemPoolInterface;
22
23
/**
24
 * Adapter class to make extension interoperable with every PSR-6 adapter.
25
 *
26
 * @see http://php-cache.readthedocs.io/
27
 *
28
 * @author Rvanlaak <[email protected]>
29
 */
30
class PsrCacheAdapter implements CacheProviderInterface
31
{
32
    /**
33
     * @var CacheItemPoolInterface
34
     */
35
    private $cache;
36
37
    /**
38
     * @param CacheItemPoolInterface $cache
39
     */
40
    public function __construct(CacheItemPoolInterface $cache)
41
    {
42
        $this->cache = $cache;
43
    }
44
45
    /**
46
     * @param string $key
47
     * @return mixed|false
48
     */
49
    public function fetch($key)
50
    {
51
        // PSR-6 implementation returns null, CacheProviderInterface expects false
52
        $item = $this->cache->getItem($key);
53
        if ($item->isHit()) {
54
            return $item->get();
55
        }
56
        return false;
57
    }
58
59
    /**
60
     * @param string $key
61
     * @param string $value
62
     * @param int|\DateInterval $lifetime
63
     * @return bool
64
     */
65
    public function save($key, $value, $lifetime = 0)
66
    {
67
        $item = $this->cache->getItem($key);
68
        $item->set($value);
69
        $item->expiresAfter($lifetime);
70
71
        return $this->cache->save($item);
72
    }
73
74
}
75