SimpleCallbackAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 2
    public function __construct(CacheAdapterInterface $cache)
22
    {
23 2
        $this->cache = $cache;
24 2
    }
25
26
    /**
27
     * @param $key
28
     * @param callable $cb
29
     * @return mixed|null
30
     */
31 2
    public function get($key, callable $cb)
32
    {
33 2
        $item = $this->cache->get($key);
34 2
        if ($item === null) {
35 1
            $item = $cb();
36 1
            $this->cache->set($key, $item);
37 1
            return $item;
38
        } else {
39 1
            return $item;
40
        }
41
    }
42
}
43