ClassBoundCacheContract   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 13 2
A __construct() 0 3 1
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 ReflectionClass<object> $reflectionClass
21
     * @param string $key An optional key to differentiate between cache items attached to the same class.
22
     *
23
     * @return mixed
24
     */
25
    public function get(ReflectionClass $reflectionClass, callable $resolver, string $key = '', ?int $ttl = null)
26
    {
27
        $cacheKey = $reflectionClass->getName() . '__' . $key;
28
        $item = $this->classBoundCache->get($cacheKey);
29
        if ($item !== null) {
30
            return $item;
31
        }
32
33
        $item = $resolver();
34
35
        $this->classBoundCache->set($cacheKey, $item, $reflectionClass, $ttl);
36
37
        return $item;
38
    }
39
}
40