Passed
Push — master ( 4261d5...023185 )
by Alexander
04:47 queued 02:09
created

Reference::resolve()   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 Yiisoft\Factory\Exception\InvalidConfigException;
8
use Yiisoft\Factory\DependencyResolverInterface;
9
10
use function is_string;
11
12
/**
13
 * The `Reference` defines a dependency to a service in the container or factory in another service definition.
14
 * For example:
15
 *
16
 * ```php
17
 * [
18
 *    InterfaceA::class => ConcreteA::class,
19
 *    'alternativeForA' => ConcreteB::class,
20
 *    MyService::class => [
21
 *        '__construct()' => [
22
 *            Reference::to('alternativeForA'),
23
 *        ],
24
 *    ],
25
 * ]
26
 * ```
27
 */
28
final class Reference implements ReferenceInterface
29
{
30
    private string $id;
31
32 27
    private function __construct(string $id)
33
    {
34 27
        $this->id = $id;
35 27
    }
36
37
    /**
38
     * @throws InvalidConfigException If ID is not string.
39
     */
40 28
    public static function to($id): self
41
    {
42 28
        if (!is_string($id)) {
43 1
            throw new InvalidConfigException('Reference ID must be string.');
44
        }
45
46 27
        return new self($id);
47
    }
48
49 27
    public function resolve(DependencyResolverInterface $container)
50
    {
51 27
        return $container->resolveReference($this->id);
52
    }
53
}
54