Completed
Pull Request — master (#5)
by Pavel
11:51
created

Configuration::createSources()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 27
cts 27
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 1
crap 1
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();
22
23 7
        $root = $builder->root('lamoda_metrics');
24 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

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