PsrCacheAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

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
declare(strict_types=1);
18
19
namespace Phpfastcache\Bundle\Twig\CacheExtension\CacheProvider;
20
21
use Phpfastcache\Bundle\Twig\CacheExtension\CacheProviderInterface;
22
use Psr\Cache\CacheItemPoolInterface;
23
24
/**
25
 * Adapter class to make extension interoperable with every PSR-6 adapter.
26
 *
27
 * @see http://php-cache.readthedocs.io/
28
 *
29
 * @author Rvanlaak <[email protected]>
30
 */
31
class PsrCacheAdapter implements CacheProviderInterface
32
{
33
    /**
34
     * @var CacheItemPoolInterface
35
     */
36
    private $cache;
37
38
    /**
39
     * @param CacheItemPoolInterface $cache
40
     */
41
    public function __construct(CacheItemPoolInterface $cache)
42
    {
43
        $this->cache = $cache;
44
    }
45
46
    /**
47
     * @param string $key
48
     * @return mixed|false
49
     */
50
    public function fetch($key)
51
    {
52
        // PSR-6 implementation returns null, CacheProviderInterface expects false
53
        $item = $this->cache->getItem($key);
54
        if ($item->isHit()) {
55
            return $item->get();
56
        }
57
        return false;
58
    }
59
60
    /**
61
     * @param string $key
62
     * @param string $value
63
     * @param int|\DateInterval $lifetime
64
     * @return bool
65
     */
66
    public function save($key, $value, $lifetime = 0)
67
    {
68
        $item = $this->cache->getItem($key);
69
        $item->set($value);
70
        $item->expiresAfter($lifetime);
71
72
        return $this->cache->save($item);
73
    }
74
}
75