DependencyResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveDependencies() 0 13 2
A getDependenciesKey() 0 4 2
1
<?php
2
3
namespace Hgraca\MicroDI\DependencyResolver;
4
5
use Hgraca\Cache\CacheInterface;
6
use Hgraca\Cache\Exception\CacheItemNotFoundException;
7
use Hgraca\Cache\Null\NullCache;
8
use Hgraca\Helper\InstanceHelper;
9
10
final class DependencyResolver implements DependencyResolverInterface
11
{
12
    /** @var CacheInterface */
13
    private $cache;
14
15 28
    public function __construct(CacheInterface $cache = null)
16
    {
17 28
        $this->cache = $cache ?? new NullCache();
18 28
    }
19
20 7
    public function resolveDependencies($callable): array
21
    {
22 7
        $dependenciesKey = $this->getDependenciesKey($callable);
23
24
        try {
25 7
            $dependencies = $this->cache->fetch($dependenciesKey);
26 6
        } catch (CacheItemNotFoundException $e) {
27 6
            $dependencies = InstanceHelper::getParameters($callable);
28 6
            $this->cache->save($dependenciesKey, $dependencies);
29
        }
30
31 7
        return $dependencies;
32
    }
33
34 7
    private function getDependenciesKey($callable): string
35
    {
36 7
        return is_object($callable) ? spl_object_hash($callable) : md5(serialize($callable));
37
    }
38
}
39