Completed
Push — master ( be772b...99d542 )
by Tomas
16:16
created

CacheHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Eziat\PermissionBundle\Utils;
6
7
use Psr\Cache\CacheItemPoolInterface;
8
9
/**
10
 * @author Tomas Jakl <[email protected]>
11
 */
12
class CacheHelper implements CacheHelperInterface
13
{
14
    /**
15
     * @var CacheItemPoolInterface
16
     */
17
    private $cache;
18
19
    /**
20
     * @var int
21
     */
22
    private $cacheExpireAt;
23
24
    public function __construct(CacheItemPoolInterface $cache, int $cacheExpireAt)
25
    {
26
        $this->cache = $cache;
27
        $this->cacheExpireAt = $cacheExpireAt;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function invalidateCache(string $cacheId) : void
34
    {
35
        $this->cache->deleteItem($cacheId);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function save(string $cacheId, $entry) : void
42
    {
43
        $cacheItem = $this->cache->getItem($cacheId);
44
        $cacheItem->set($entry);
45
        $cacheItem->expiresAfter($this->cacheExpireAt);
46
        $this->cache->save($cacheItem);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function delete(string $cacheId) : void
53
    {
54
        $this->cache->deleteItem($cacheId);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function get(string $cacheId)
61
    {
62
        $cacheItem = $this->cache->getItem($cacheId);
63
        if (!$cacheItem->isHit()) {
64
            return null;
65
        } else {
66
            return $cacheItem->get();
67
        }
68
    }
69
}