Passed
Pull Request — master (#5)
by Pavel
05:52
created

Storage::createReference()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Reference;
7
8
/**
9
 * @internal
10
 */
11
final class Storage
12
{
13
    public const TAG = 'lamoda_metrics.storage';
14
    public const ALIAS_ATTRIBUTE = 'alias';
15
    public const REGISTRY_ID = 'lamoda_metrics.storage_registry';
16
17
    public const TYPES = [
18
        self::STORAGE_TYPE_SERVICE,
19
    ];
20
21
    private const STORAGE_TYPE_SERVICE = 'service';
22
23
    private const MUTATOR_STORAGE_ID = 'lamoda_metrics.metric_mutator_storage';
24
    private const MUTATOR_ID = 'lamoda_metrics.metric_mutator';
25
    private const ID_PREFIX = 'lamoda_metrics.storage.';
26
27 4
    public static function createId(string $name): string
28
    {
29 4
        return self::ID_PREFIX . $name;
30
    }
31
32 4
    public static function createReference(string $name): Reference
33
    {
34 4
        return new Reference(self::createId($name));
35
    }
36
37 4
    public static function register(ContainerBuilder $container, string $name, array $config)
38
    {
39 4
        switch ($config['type']) {
40 4
            case self::STORAGE_TYPE_SERVICE:
41 4
                $container->setAlias(self::createId($name), $config['id']);
42 4
                break;
43
        }
44
45 4
        if ($config['mutator'] ?? false) {
46 4
            self::registerAsMutator($container, $name);
47
        }
48
49 4
        self::addToRegistry($container, $name);
50 4
    }
51
52
    /**
53
     * @param ContainerBuilder $container
54
     * @param string           $name
55
     */
56 4
    private static function addToRegistry(ContainerBuilder $container, string $name): void
57
    {
58 4
        $container->getDefinition(self::REGISTRY_ID)->addMethodCall('register', [$name, self::createReference($name)]);
59 4
    }
60
61
    /**
62
     * @param ContainerBuilder $container
63
     * @param string           $name
64
     */
65 4
    private static function registerAsMutator(ContainerBuilder $container, string $name): void
66
    {
67 4
        $container->setAlias(self::MUTATOR_STORAGE_ID, self::createId($name));
68 4
        $container->getDefinition(self::MUTATOR_ID)->setArguments([self::createReference($name)]);
69 4
    }
70
}
71