Completed
Push — sam-rework ( 1696e6 )
by Andrii
38:13 queued 23:11
created

Reference::__set_state()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace yii\di;
5
6
use Psr\Container\ContainerInterface;
7
use yii\di\contracts\DependencyInterface;
8
9
/**
10
 * Class Reference allows us to define a dependency to a service in the container in another service definition.
11
 * For example:
12
 * ```php
13
 * [
14
 *    InterfaceA::class => ConcreteA::class,
15
 *    'alternativeForA' => ConcreteB::class,
16
 *    Service1::class => [
17
 *        '__construct()' => [
18
 *            Reference::to('alternativeForA')
19
 *        ]
20
 *    ]
21
 * ]
22
 * ```
23
 */
24
class Reference implements DependencyInterface
25
{
26
    private $id;
27
28
    private function __construct($id)
29
    {
30
        $this->id = $id;
31
    }
32
33
    public function getId(): string
34
    {
35
        return $this->id;
36
    }
37
38
    public static function to(string $id)
39
    {
40
        return new self($id);
41
    }
42
43
    /**
44
     * @param Container $container
45
     */
46
    public function resolve(ContainerInterface $container)
47
    {
48
        return $container->get($this->id);
49
    }
50
}
51