|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory; |
|
4
|
|
|
|
|
5
|
|
|
use Lamoda\Metric\Common\Source\IterableMetricSource; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
|
|
11
|
|
|
/** @internal */ |
|
12
|
|
|
final class Source |
|
13
|
|
|
{ |
|
14
|
|
|
public const TAG = 'lamoda_metrics.source'; |
|
15
|
|
|
public const ALIAS_ATTRIBUTE = 'alias'; |
|
16
|
|
|
|
|
17
|
|
|
public const METRIC_SOURCE_TYPES = [ |
|
18
|
|
|
self::METRIC_SOURCE_SERVICE, |
|
19
|
|
|
self::METRIC_SOURCE_COMPOSITE, |
|
20
|
|
|
self::METRIC_SOURCE_STORAGE, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
private const ID_PREFIX = 'lamoda_metric.metric_source.'; |
|
24
|
|
|
|
|
25
|
|
|
private const METRIC_SOURCE_COMPOSITE = 'composite'; |
|
26
|
|
|
private const METRIC_SOURCE_SERVICE = 'service'; |
|
27
|
|
|
private const METRIC_SOURCE_STORAGE = 'storage'; |
|
28
|
|
|
|
|
29
|
4 |
|
public static function register(ContainerBuilder $container, string $name, array $config) |
|
30
|
|
|
{ |
|
31
|
4 |
|
switch ($config['type']) { |
|
32
|
4 |
|
case static::METRIC_SOURCE_COMPOSITE: |
|
33
|
4 |
|
static::createCompositeMetricDefinition($container, $name, $config); |
|
34
|
4 |
|
break; |
|
35
|
4 |
|
case static::METRIC_SOURCE_SERVICE: |
|
36
|
|
|
static::createServiceMetricDefinition($container, $name, $config); |
|
37
|
|
|
break; |
|
38
|
4 |
|
case static::METRIC_SOURCE_STORAGE: |
|
39
|
4 |
|
static::createServiceMetricDefinition( |
|
40
|
4 |
|
$container, |
|
41
|
4 |
|
$name, |
|
42
|
4 |
|
['id' => Storage::createId($config['storage'])] |
|
43
|
|
|
); |
|
44
|
4 |
|
break; |
|
45
|
|
|
default: |
|
46
|
|
|
throw new \InvalidArgumentException('Invalid metric source type: ' . $config['type']); |
|
47
|
|
|
} |
|
48
|
4 |
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
public static function createId(string $name): string |
|
51
|
|
|
{ |
|
52
|
4 |
|
return self::ID_PREFIX . $name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
public static function createReference(string $name): Reference |
|
56
|
|
|
{ |
|
57
|
4 |
|
return new Reference(self::createId($name)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
private static function createServiceMetricDefinition(ContainerBuilder $container, string $name, array $config) |
|
61
|
|
|
{ |
|
62
|
4 |
|
if (!array_key_exists('id', $config) || !is_string($config['id'])) { |
|
63
|
|
|
throw new \InvalidArgumentException('`id` key should be configured for metric source'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
$container->setAlias(self::createId($name), $config['id']); |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
4 |
|
private static function createCompositeMetricDefinition(ContainerBuilder $container, string $name, array $config) |
|
70
|
|
|
{ |
|
71
|
4 |
|
if (!array_key_exists('metrics', $config) || !is_array($config['metrics'])) { |
|
72
|
|
|
throw new \InvalidArgumentException('`metrics` key should be configured for composite source'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
$metrics = []; |
|
76
|
|
|
|
|
77
|
4 |
|
foreach ($config['metrics'] as $ref) { |
|
78
|
4 |
|
$metrics[] = new Reference($ref); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
$definition = new Definition(IterableMetricSource::class, [new Definition(\ArrayIterator::class, [$metrics])]); |
|
82
|
|
|
|
|
83
|
4 |
|
$container->setDefinition(self::createId($name), $definition); |
|
84
|
4 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|