Completed
Push — master ( 0629e4...73c381 )
by David
17s queued 10s
created

ClassBoundCacheContract::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\CacheUtils;
6
7
use ReflectionClass;
8
9
class ClassBoundCacheContract implements ClassBoundCacheContractInterface
10
{
11
    /** @var ClassBoundCacheInterface */
12
    private $classBoundCache;
13
14
    public function __construct(ClassBoundCacheInterface $classBoundCache)
15
    {
16
        $this->classBoundCache = $classBoundCache;
17
    }
18
19
    /**
20
     * @param string $key An optional key to differentiate between cache items attached to the same class.
21
     *
22
     * @return mixed
23
     */
24
    public function get(ReflectionClass $reflectionClass, callable $resolver, string $key = '')
25
    {
26
        $cacheKey = $reflectionClass->getName() . '__' . $key;
27
        $item = $this->classBoundCache->get($cacheKey);
28
        if ($item !== null) {
29
            return $item;
30
        }
31
32
        $item = $resolver();
33
34
        $this->classBoundCache->set($cacheKey, $item, $reflectionClass);
35
36
        return $item;
37
    }
38
}
39