Passed
Push — master ( 2c1787...c36eb2 )
by Jesús
04:16 queued 13s
created

InstanceCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver\InstanceCreator;
6
7
use Gacela\Framework\ClassResolver\DependencyResolver\DependencyResolver;
8
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
9
10
final class InstanceCreator
11
{
12
    private GacelaConfigFileInterface $gacelaConfigFile;
13
14
    private ?DependencyResolver $dependencyResolver = null;
15
16
    /** @var array<class-string,list<mixed>> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string,list<mixed>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,list<mixed>>.
Loading history...
17
    private array $cachedDependencies = [];
18
19 21
    public function __construct(GacelaConfigFileInterface $gacelaConfigFile)
20
    {
21 21
        $this->gacelaConfigFile = $gacelaConfigFile;
22
    }
23
24 21
    public function createByClassName(string $className): ?object
25
    {
26 21
        if (class_exists($className)) {
27 20
            if (!isset($this->cachedDependencies[$className])) {
28 20
                $this->cachedDependencies[$className] = $this
29 20
                    ->getDependencyResolver()
30 20
                    ->resolveDependencies($className);
31
            }
32
33
            /** @psalm-suppress MixedMethodCall */
34 20
            return new $className(...$this->cachedDependencies[$className]);
35
        }
36
37 1
        return null;
38
    }
39
40 20
    private function getDependencyResolver(): DependencyResolver
41
    {
42 20
        if (null === $this->dependencyResolver) {
43 20
            $this->dependencyResolver = new DependencyResolver(
44 20
                $this->gacelaConfigFile
45
            );
46
        }
47
48 20
        return $this->dependencyResolver;
49
    }
50
}
51