Completed
Push — master ( c4d148...c0e5c2 )
by Frederik
02:33
created

SimpleCallbackAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Genkgo\Cache\Adapter;
3
4
use Genkgo\Cache\CacheAdapterInterface;
5
use Genkgo\Cache\CallbackCacheInterface;
6
7
/**
8
 * Class SimpleCallbackAdapter
9
 * @package Genkgo\Cache
10
 */
11
class SimpleCallbackAdapter implements CallbackCacheInterface
12
{
13
    /**
14
     * @var CacheAdapterInterface
15
     */
16
    private $cache;
17
18
    /**
19
     * @param CacheAdapterInterface $cache
20
     */
21
    public function __construct(CacheAdapterInterface $cache)
22
    {
23
        $this->cache = $cache;
24
    }
25
26
    /**
27
     * @param $key
28
     * @param callable $cb
29
     * @return mixed|null
30
     */
31
    public function get($key, callable $cb)
32
    {
33
        $item = $this->cache->get($key);
34
        if ($item === null) {
35
            $item = $cb();
36
            $this->cache->set($key, $item);
37
            return $item;
38
        } else {
39
            return $item;
40
        }
41
    }
42
}
43