Passed
Pull Request — master (#78)
by Dmitriy
02:32
created

DynamicReference   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 27
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A to() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Class DynamicReference allows us to define a dependency to a service not defined in the container.
11
 * Definition may be defined multiple ways {@see Normalizer}.
12
 * For example:
13
 * ```php
14
 * [
15
 *    InterfaceA::class => ConcreteA::class,
16
 *    'alternativeForA' => ConcreteB::class,
17
 *    Service1::class => [
18
 *        '__construct()' => [
19
 *            DynamicReference::to([
20
 *                'class' => SomeClass::class,
21
 *                '@someProp' => 15
22
 *            ])
23
 *        ]
24
 *    ]
25
 * ]
26
 * ```
27
 */
28
class DynamicReference implements ReferenceInterface
29
{
30
    private DefinitionInterface $definition;
31
32
    /**
33
     * @param mixed $definition
34
     */
35 5
    private function __construct($definition)
36
    {
37 5
        $this->definition = Normalizer::normalize($definition);
38 5
    }
39
40
    /**
41
     * @param mixed $definition
42
     *
43
     * @return ReferenceInterface
44
     *
45
     * {@see Normalizer}
46
     */
47 5
    public static function to($definition): ReferenceInterface
48
    {
49 5
        return new self($definition);
50
    }
51
52 5
    public function resolve(ContainerInterface $container)
53
    {
54 5
        return $this->definition->resolve($container);
55
    }
56
}
57