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

DependencyResolver::getDependenciesKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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