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

DynamicReference::to()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
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 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