Configuration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2.0003

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 22
c 1
b 0
f 1
dl 0
loc 34
ccs 21
cts 22
cp 0.9545
rs 9.568
cc 2
nc 2
nop 0
crap 2.0003
1
<?php
2
3
namespace Lamoda\Metric\MetricBundle\DependencyInjection;
4
5
use Lamoda\Metric\Collector\MetricCollectorInterface;
6
use Lamoda\Metric\Common\MetricInterface;
7
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Collector;
8
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\ResponseFactory;
9
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Source;
10
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Storage;
11
use Lamoda\Metric\Storage\MetricStorageInterface;
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
final class Configuration implements ConfigurationInterface
17
{
18
    /** {@inheritdoc} */
19 7
    public function getConfigTreeBuilder(): TreeBuilder
20
    {
21 7
        $builder = new TreeBuilder('lamoda_metrics');
22
23 7
        if (method_exists($builder, 'getRootNode')) {
24 7
            $root = $builder->getRootNode();
25
        } else {
26
            // BC layer for symfony/config 4.1 and older
27
            $root = $builder->root('lamoda_metrics');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

27
            $root = /** @scrutinizer ignore-deprecated */ $builder->root('lamoda_metrics');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
28
        }
29
30 7
        $root->addDefaultsIfNotSet();
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $root->/** @scrutinizer ignore-call */ 
31
               addDefaultsIfNotSet();
Loading history...
31
32 7
        $sources = $root->children()->arrayNode('sources');
33 7
        $sources->useAttributeAsKey('name', false);
34 7
        $this->createSources($sources->prototype('array'));
35
36 7
        $responseFactories = $root->children()->arrayNode('response_factories');
37 7
        $responseFactories->useAttributeAsKey('name', false);
38 7
        $this->createResponseFactory($responseFactories->prototype('array'));
39
40 7
        $responders = $root->children()->arrayNode('responders');
41 7
        $responders->useAttributeAsKey('name', false);
42 7
        $this->createResponder($responders->prototype('array'));
43
44 7
        $storages = $root->children()->arrayNode('storages');
45 7
        $storages->useAttributeAsKey('name', false);
46 7
        $this->createStorage($storages->prototype('array'));
47
48 7
        $collectors = $root->children()->arrayNode('collectors');
49 7
        $collectors->useAttributeAsKey('name', false);
50 7
        $this->createCollector($collectors->prototype('array'));
51
52 7
        return $builder;
53
    }
54
55 7
    private function createSources(ArrayNodeDefinition $source): void
56
    {
57 7
        $source->info(
58 7
            'Sources also can be configured as services via `' . DefinitionFactory\Source::TAG . '` tag with `' . DefinitionFactory\Source::ALIAS_ATTRIBUTE . '` attribute'
59
        );
60 7
        $source->canBeDisabled();
61 7
        $source->children()
62 7
            ->enumNode('type')
63 7
            ->cannotBeEmpty()
64 7
            ->defaultValue('service')
65 7
            ->values(Source::METRIC_SOURCE_TYPES)
66 7
            ->info('Type of the source');
67
68 7
        $source->children()
69 7
            ->scalarNode('id')
70 7
            ->defaultNull()
71 7
            ->info('Source service identifier [service]');
72
73 7
        $source->children()
74 7
            ->scalarNode('entity')
75 7
            ->defaultValue(MetricInterface::class)
76 7
            ->info('Entity class [doctrine]');
77
78 7
        $source->children()
79 7
            ->arrayNode('metrics')
80 7
            ->info('Metric services [composite]')
81 7
            ->defaultValue([])
82 7
            ->prototype('scalar');
83
84 7
        $source->children()
85 7
            ->scalarNode('storage')
86 7
            ->info('Storage name [storage]')
87 7
            ->defaultNull();
88 7
    }
89
90 7
    private function createResponseFactory(ArrayNodeDefinition $responseFactory): void
91
    {
92 7
        $responseFactory->info(
93 7
            'Response factories also can be configured as services via `' . DefinitionFactory\ResponseFactory::TAG . '` tag with `' . DefinitionFactory\ResponseFactory::ALIAS_ATTRIBUTE . '` attribute'
94
        );
95 7
        $responseFactory->canBeDisabled();
96 7
        $responseFactory->beforeNormalization()->ifString()->then(
97
            function (string $v) {
98
                return ['type' => 'service', 'id' => $v];
99 7
            }
100
        );
101 7
        $responseFactory->children()
102 7
            ->enumNode('type')
103 7
            ->cannotBeEmpty()
104 7
            ->defaultValue('service')
105 7
            ->values(ResponseFactory::TYPES)
106 7
            ->info('Type of the factory');
107
108 7
        $responseFactory->children()
109 7
            ->scalarNode('id')
110 7
            ->defaultNull()
111 7
            ->info('Response factory service identifier [service]');
112 7
    }
113
114 7
    private function createCollector(ArrayNodeDefinition $collector): void
115
    {
116 7
        $collector->info(
117 7
            'Collectors also can be configured as services via `' . DefinitionFactory\Collector::TAG . '` tag with `' . DefinitionFactory\Collector::ALIAS_ATTRIBUTE . '` attribute'
118
        );
119 7
        $collector->beforeNormalization()->ifString()->then(
120
            function (string $v) {
121
                return ['type' => Collector::COLLECTOR_TYPE_SERVICE, 'id' => $v];
122 7
            }
123
        );
124 7
        $collector->canBeDisabled();
125 7
        $collector->children()->scalarNode('id')
126 7
            ->info('Collector service ID')
127 7
            ->defaultNull()
128 7
            ->example(MetricCollectorInterface::class);
129
130 7
        $collector->children()
131 7
            ->enumNode('type')
132 7
            ->cannotBeEmpty()
133 7
            ->defaultValue('service')
134 7
            ->values(Collector::TYPES)
135 7
            ->info('Type of the collector');
136
137 7
        $collector->children()->arrayNode('collectors')
138 7
            ->prototype('scalar')
139 7
            ->info('Nested collectors')
140 7
            ->defaultValue([]);
141
142 7
        $collector->children()->arrayNode('sources')
143 7
            ->prototype('scalar')
144 7
            ->info('Metrics source names for responder controller')
145 7
            ->defaultValue([]);
146
147 7
        $collector->children()->arrayNode('metric_services')
148 7
            ->prototype('scalar')
149 7
            ->info('Append single metrics from services')
150 7
            ->defaultValue([]);
151
152 7
        $collector->children()
153 7
            ->arrayNode('default_tags')
154 7
            ->defaultValue([])
155 7
            ->info('Default tag values for metrics from this collector')
156 7
            ->prototype('scalar')
157 7
            ->cannotBeEmpty();
158 7
    }
159
160 7
    private function createStorage(ArrayNodeDefinition $storage): void
161
    {
162 7
        $storage->info(
163 7
            'Storages also can be configured as services via `' . DefinitionFactory\Storage::TAG . '` tag with `' . DefinitionFactory\Storage::ALIAS_ATTRIBUTE . '` attribute'
164
        );
165 7
        $storage->beforeNormalization()->ifString()->then(
166
            function (string $v) {
167
                return ['type' => 'service', 'id' => $v];
168 7
            }
169
        );
170 7
        $storage->canBeDisabled();
171 7
        $storage->children()->scalarNode('id')
172 7
            ->cannotBeEmpty()
173 7
            ->info('Storage service ID [service]')
174 7
            ->example(MetricStorageInterface::class);
175 7
        $storage->children()
176 7
            ->enumNode('type')
177 7
            ->cannotBeEmpty()
178 7
            ->defaultValue('service')
179 7
            ->values(Storage::TYPES)
180 7
            ->info('Type of the storage');
181 7
        $storage->children()
182 7
            ->booleanNode('mutator')
183 7
            ->defaultFalse()
184 7
            ->info('Configure storage as default metric mutator');
185 7
    }
186
187 7
    private function createResponder(ArrayNodeDefinition $responder): void
188
    {
189 7
        $responder->canBeDisabled();
190 7
        $responder->children()->scalarNode('path')
191 7
            ->cannotBeEmpty()
192 7
            ->info('Responder route path. Defaults to /$name')
193 7
            ->defaultNull()
194 7
            ->example('/prometheus');
195
196 7
        $options = $responder->children()->arrayNode('format_options');
197 7
        $options->info('Formatter options');
198 7
        $options->ignoreExtraKeys(false);
199 7
        $options->children()->scalarNode('prefix')
200 7
            ->info('Metrics prefix for responder')
201 7
            ->defaultValue('')
202 7
            ->example('project_name_');
203 7
        $options->children()->arrayNode('propagate_tags')
204 7
            ->info('Propagate tags to group [telegraf_json]')
205 7
            ->prototype('scalar')
206 7
            ->defaultValue([])
207 7
            ->example('type');
208 7
        $options->children()->arrayNode('group_by_tags')
209 7
            ->info('Arrange metrics to groups according to tag value. Tag name goes to group name [telegraf_json]')
210 7
            ->prototype('scalar')
211 7
            ->defaultValue([])
212 7
            ->example(['tag_1']);
213
214 7
        $responder->children()->scalarNode('response_factory')
215 7
            ->cannotBeEmpty()
216 7
            ->info('Response factory alias')
217 7
            ->defaultNull()
218 7
            ->example('prometheus');
219
220 7
        $responder->children()->scalarNode('collector')
221 7
            ->info('Collector alias')
222 7
            ->isRequired()
223 7
            ->cannotBeEmpty();
224 7
    }
225
}
226