Completed
Pull Request — master (#2069)
by Andreas
20:35
created

CachingClassNameResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Proxy\Resolver;
6
7
use function get_class;
8
9
/**
10
 * @internal
11
 */
12
final class CachingClassNameResolver implements ClassNameResolver
13
{
14
    /** @var ClassNameResolver */
15
    private $resolver;
16
17
    /**
18
     * @var array<string, string>
19
     */
20
    private $resolvedNames = [];
21
22 1773
    public function __construct(ClassNameResolver $resolver)
23
    {
24 1773
        $this->resolver = $resolver;
25 1773
    }
26
27
    /**
28
     * Gets the real class name of a class name that could be a proxy.
29
     */
30 1548
    public function getRealClass(string $class) : string
31
    {
32 1548
        if (! isset($this->resolvedNames[$class])) {
33 1548
            $this->resolvedNames[$class] = $this->resolver->getRealClass($class);
34
        }
35
36 1548
        return $this->resolvedNames[$class];
37
    }
38
39
    /**
40
     * Gets the real class name of an object (even if its a proxy).
41
     */
42
    public function getClass(object $object) : string
43
    {
44
        return $this->getRealClass(get_class($object));
45
    }
46
}
47