Passed
Push — master ( 2172fa...eaad1b )
by Alexander
07:07
created

DefinitionResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 45
ccs 16
cts 17
cp 0.9412
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveArray() 0 8 2
A resolve() 0 9 3
A ensureResolvable() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definitions;
6
7
use Psr\Container\ContainerInterface;
8
9
class DefinitionResolver
10
{
11
    /**
12
     * Resolves dependencies by replacing them with the actual object instances.
13
     * @param ContainerInterface $container
14
     * @param DefinitionInterface[] $dependencies the dependencies
15
     * @return array the resolved dependencies
16
     */
17 37
    public static function resolveArray(ContainerInterface $container, array $dependencies): array
18
    {
19 37
        $result = [];
20 37
        foreach ($dependencies as $key => $definition) {
21 14
            $result[$key] = self::resolve($container, $definition);
22
        }
23
24 35
        return $result;
25
    }
26
27
    /**
28
     * This function resolves a definition recursively, checking for loops.
29
     * @param ContainerInterface $container
30
     * @param mixed $definition
31
     * @return mixed
32
     */
33 14
    public static function resolve(ContainerInterface $container, $definition)
34
    {
35 14
        if ($definition instanceof DefinitionInterface) {
36 14
            $definition = $definition->resolve($container);
37 2
        } elseif (is_array($definition)) {
38 2
            return self::resolveArray($container, $definition);
39
        }
40
41 12
        return $definition;
42
    }
43
44 8
    public static function ensureResolvable($value)
45
    {
46 8
        if ($value instanceof DefinitionInterface || is_array($value)) {
47 2
            return $value;
48
        }
49 6
        if (is_callable($value)) {
50
            return new CallableDefinition($value);
51
        }
52
53 6
        return new ValueDefinition($value);
54
    }
55
}
56