Passed
Push — master ( 7f6a5c...a1085f )
by Herberto
02:24
created

DependencyResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveDependencies() 0 13 2
A getDependenciesKey() 0 4 1
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\ClassHelper;
9
10
final class DependencyResolver implements DependencyResolverInterface
11
{
12
    /** @var CacheInterface */
13
    private $cache;
14
15 25
    public function __construct(CacheInterface $cache = null)
16
    {
17 25
        $this->cache = $cache ?? new NullCache();
18 25
    }
19
20 5
    public function resolveDependencies(string $dependentClass, string $dependentMethod): array
21
    {
22 5
        $dependenciesKey = $this->getDependenciesKey($dependentClass, $dependentMethod);
23
24
        try {
25 5
            $dependencies = $this->cache->fetch($dependenciesKey);
26 4
        } catch (CacheItemNotFoundException $e) {
27 4
            $dependencies = ClassHelper::getParameters($dependentClass, $dependentMethod);
28 4
            $this->cache->save($dependenciesKey, $dependencies);
29
        }
30
31 5
        return $dependencies;
32
    }
33
34 5
    private function getDependenciesKey(string $class, string $method = '__construct'): string
35
    {
36 5
        return sprintf('%s::%s', str_replace('\\', '_', $class), $method);
37
    }
38
}
39