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

Reference   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 6
dl 0
loc 24
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A __construct() 0 3 1
A to() 0 7 2
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