Completed
Push — V6 ( e31388...b1e39c )
by Georges
02:21
created

CacheConditionalHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
namespace phpFastCache\Helper;
15
16
use Psr\Cache\CacheItemPoolInterface;
17
18
/**
19
 * Class CacheConditional
20
 * @package phpFastCache\Helper
21
 */
22
class CacheConditionalHelper
23
{
24
    /**
25
     * @var CacheItemPoolInterface
26
     */
27
    protected $cacheInstance;
28
29
    /**
30
     * CachePromise constructor.
31
     * @param CacheItemPoolInterface $cacheInstance
32
     */
33
    public function __construct(CacheItemPoolInterface $cacheInstance)
34
    {
35
        $this->cacheInstance = $cacheInstance;
36
    }
37
38
    /**
39
     * @param string $cacheKey
40
     * @param callable $callback
41
     */
42
    public function get($cacheKey, callable $callback)
43
    {
44
        $cacheItem = $this->cacheInstance->getItem($cacheKey);
45
46
        if(!$cacheItem->isHit())
47
        {
48
            $cacheItem->set($callback());
49
            $this->cacheInstance->save($cacheItem);
50
        }
51
52
        return $cacheItem->get();
53
    }
54
}