Passed
Pull Request — master (#290)
by Dmitriy
02:50
created

Factory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Definitions\Exception\CircularReferenceException;
9
use Yiisoft\Definitions\Exception\InvalidConfigException;
10
use Yiisoft\Definitions\DefinitionStorage;
11
use Yiisoft\Di\Helpers\DefinitionNormalizer;
12
13
use function array_keys;
14
use function implode;
15
16
final class Factory
17
{
18
    private ContainerInterface $container;
19
20
    private DefinitionStorage $definitions;
21
22
    private array $building;
23
24
25 117
    public function __construct(ContainerInterface $container, DefinitionStorage $definitions = null)
26
    {
27 117
        $this->container = $container;
28
29 117
        if($definitions === null) {
30 10
            $this->definitions = new DefinitionStorage();
31 10
            $this->definitions->setDelegateContainer($container);
32
        } else {
33 117
            $this->definitions = $definitions;
34
        }
35 117
    }
36
37
    /**
38
     * Creates new instance by either interface name or alias.
39
     *
40
     * @param string $id The interface or an alias name that was previously registered.
41
     *
42
     * @throws CircularReferenceException
43
     * @throws InvalidConfigException
44
     * @throws NotFoundException
45
     *
46
     * @return mixed|object New built instance of the specified class.
47
     *
48
     * @internal
49
     */
50 103
    public function create(string $id, array $parameters = [])
51
    {
52 103
        if (isset($this->building[$id])) {
53 94
            if ($id === ContainerInterface::class) {
54 94
                return $this->container;
55
            }
56 4
            throw new CircularReferenceException(sprintf(
57 4
                'Circular reference to "%s" detected while building: %s.',
58
                $id,
59 4
                implode(', ', array_keys($this->building))
60
            ));
61
        }
62
63 103
        $this->building[$id] = 1;
64
        try {
65
            /** @var mixed $object */
66 103
            $object = $this->buildInternal($id, $parameters);
67 94
        } finally {
68 103
            unset($this->building[$id]);
69
        }
70
71 94
        return $object;
72
    }
73
74 9
    public function withDefinitions(array $definitions)
75
    {
76 9
        $new = clone $this;
77 9
        $new->definitions = new DefinitionStorage($definitions);
78 9
        $new->definitions->setDelegateContainer($this->container);
79
80 9
        return $new;
81
    }
82
83
    /**
84
     * @param string $id
85
     *
86
     * @throws InvalidConfigException
87
     * @throws NotFoundException
88
     *
89
     * @return mixed|object
90
     */
91 103
    private function buildInternal(string $id, array $parameters)
92
    {
93 103
        if ($this->definitions->has($id)) {
94 94
            $definition = $this->definitions->get($id);
95 94
            if (is_array($definition) && !is_callable($definition, true)) {
96 47
                $constructorArguments = $definition['__construct()'] ?? [];
97 47
                $definition['__construct()'] = $this->mergeArguments($constructorArguments, $parameters);
98
            }
99 94
            $definition = DefinitionNormalizer::normalize($definition, $id);
100
101 94
            return $definition->resolve($this->container->get(ContainerInterface::class));
102
        }
103
104 10
        throw new NotFoundException($id, $this->definitions->getBuildStack());
105
    }
106
107 47
    private function mergeArguments(array $selfArguments, array $otherArguments): array
108
    {
109
        /** @var mixed $argument */
110 47
        foreach ($otherArguments as $name => $argument) {
111
            /** @var mixed */
112 6
            $selfArguments[$name] = $argument;
113
        }
114
115 47
        return $selfArguments;
116
    }
117
}
118