Configuration::addLoggingSection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * Class Configuration.
21
 *
22
 * @author Aaron Scherer <[email protected]>
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * Generates the configuration tree builder.
28
     *
29
     * @return TreeBuilder The tree builder
30
     */
31 2
    public function getConfigTreeBuilder()
32
    {
33 2
        $treeBuilder = new TreeBuilder();
34 2
        $rootNode    = $treeBuilder->root('cache');
35
36 2
        $rootNode->children()
37 2
            ->append($this->addSessionSupportSection())
38 2
            ->append($this->addDoctrineSection())
39 2
            ->append($this->addRouterSection())
40 2
            ->append($this->addAnnotationSection())
41 2
            ->append($this->addSerializerSection())
42 2
            ->append($this->addValidationSection())
43 2
            ->append($this->addLoggingSection())
44 2
            ->append($this->addDataCollectorSection())
45 2
            ->end();
46
47 2
        return $treeBuilder;
48
    }
49
50
    /**
51
     * Normalizes the enabled field to be truthy.
52
     *
53
     * @param NodeDefinition $node
54
     *
55
     * @return Configuration
56
     */
57 2
    private function normalizeEnabled(NodeDefinition $node)
58
    {
59 2
        $node->beforeNormalization()
60 2
                ->always()
61 2
                ->then(
62 2
                    function ($v) {
63 2
                        if (is_string($v['enabled'])) {
64
                            $v['enabled'] = $v['enabled'] === 'true';
65
                        }
66 2
                        if (is_int($v['enabled'])) {
67
                            $v['enabled'] = $v['enabled'] === 1;
68
                        }
69
70 2
                        return $v;
71 2
                    }
72
                )
73 2
            ->end();
74
75 2
        return $this;
76
    }
77
78
    /**
79
     * Configure the "cache.session" section.
80
     *
81
     * @return ArrayNodeDefinition
82
     */
83 2 View Code Duplication
    private function addSessionSupportSection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85 2
        $tree = new TreeBuilder();
86 2
        $node = $tree->root('session');
87
88
        $node
89 2
            ->canBeEnabled()
90 2
            ->addDefaultsIfNotSet()
91 2
            ->children()
92 2
                ->scalarNode('service_id')->isRequired()->end()
93 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
94 2
                ->scalarNode('prefix')->defaultValue('session_')->end()
95 2
                ->scalarNode('ttl')->end()
96 2
            ->end();
97
98 2
        $this->normalizeEnabled($node);
99
100 2
        return $node;
101
    }
102
103
    /**
104
     * Configure the "cache.serializer" section.
105
     *
106
     * @return ArrayNodeDefinition
107
     */
108 2 View Code Duplication
    private function addSerializerSection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110 2
        $tree = new TreeBuilder();
111 2
        $node = $tree->root('serializer');
112
113
        $node
114 2
            ->canBeEnabled()
115 2
            ->addDefaultsIfNotSet()
116 2
            ->children()
117 2
            ->scalarNode('service_id')->isRequired()->end()
118 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
119 2
                ->scalarNode('prefix')->defaultValue('')->end()
120 2
            ->end();
121
122 2
        $this->normalizeEnabled($node);
123
124 2
        return $node;
125
    }
126
127
    /**
128
     * Configure the "cache.serializer" section.
129
     *
130
     * @return ArrayNodeDefinition
131
     */
132 2 View Code Duplication
    private function addValidationSection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134 2
        $tree = new TreeBuilder();
135 2
        $node = $tree->root('validation');
136
137
        $node
138 2
            ->canBeEnabled()
139 2
            ->addDefaultsIfNotSet()
140 2
            ->children()
141 2
                ->scalarNode('service_id')->isRequired()->end()
142 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
143 2
                ->scalarNode('prefix')->defaultValue('')->end()
144 2
            ->end();
145
146 2
        $this->normalizeEnabled($node);
147
148 2
        return $node;
149
    }
150
151
    /**
152
     * Configure the "cache.annotation" section.
153
     *
154
     * @return ArrayNodeDefinition
155
     */
156 2 View Code Duplication
    private function addAnnotationSection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158 2
        $tree = new TreeBuilder();
159 2
        $node = $tree->root('annotation');
160
161
        $node
162 2
            ->canBeEnabled()
163 2
            ->addDefaultsIfNotSet()
164 2
            ->children()
165 2
                ->scalarNode('service_id')->isRequired()->end()
166 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
167 2
                ->scalarNode('prefix')->defaultValue('')->end()
168 2
            ->end();
169
170 2
        $this->normalizeEnabled($node);
171
172 2
        return $node;
173
    }
174
175
    /**
176
     * @return ArrayNodeDefinition
177
     */
178 2
    private function addLoggingSection()
179
    {
180 2
        $tree = new TreeBuilder();
181 2
        $node = $tree->root('logging');
182
183
        $node
184 2
            ->canBeEnabled()
185 2
            ->addDefaultsIfNotSet()
186 2
            ->children()
187 2
                ->scalarNode('logger')->defaultValue('logger')->end()
188 2
                ->scalarNode('level')->defaultValue('info')->end()
189 2
            ->end();
190
191 2
        $this->normalizeEnabled($node);
192
193 2
        return $node;
194
    }
195
196
    /**
197
     * Configure the "cache.doctrine" section.
198
     *
199
     * @return ArrayNodeDefinition
200
     */
201 2
    private function addDoctrineSection()
202
    {
203 2
        $tree = new TreeBuilder();
204 2
        $node = $tree->root('doctrine');
205
206
        $node
207 2
            ->canBeEnabled()
208 2
            ->addDefaultsIfNotSet()
209 2
            ->children()
210 2
                ->booleanNode('use_tagging')
211 2
                    ->defaultTrue()
212 2
                ->end()
213 2
            ->end();
214
215 2
        $this->normalizeEnabled($node);
216
217 2
        $types = ['metadata', 'result', 'query'];
218 2
        foreach ($types as $type) {
219 2
            $node->children()
220 2
                    ->arrayNode($type)
221 2
                        ->canBeUnset()
222 2
                        ->children()
223 2
                            ->scalarNode('service_id')->isRequired()->end()
224 2
                            ->arrayNode('entity_managers')
225 2
                                ->defaultValue([])
226 2
                                ->beforeNormalization()
227 2
                                    ->ifString()
228 2
                                    ->then(
229 2
                                        function ($v) {
230
                                            return (array) $v;
231 2
                                        }
232
                                    )
233 2
                                    ->end()
234 2
                                    ->prototype('scalar')->end()
235 2
                                ->end()
236 2
                            ->arrayNode('document_managers')
237 2
                                ->defaultValue([])
238 2
                                ->beforeNormalization()
239 2
                                    ->ifString()
240 2
                                    ->then(
241 2
                                        function ($v) {
242
                                            return (array) $v;
243 2
                                        }
244
                                    )
245 2
                                ->end()
246 2
                                ->prototype('scalar')->end()
247 2
                            ->end()
248 2
                    ->end()
249 2
                ->end();
250
        }
251
252 2
        return $node;
253
    }
254
255
    /**
256
     * Configure the "cache.router" section.
257
     *
258
     * @return ArrayNodeDefinition
259
     */
260 2 View Code Duplication
    private function addRouterSection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262 2
        $tree = new TreeBuilder();
263 2
        $node = $tree->root('router');
264
265
        $node
266 2
            ->canBeEnabled()
267 2
            ->addDefaultsIfNotSet()
268 2
            ->children()
269 2
                ->integerNode('ttl')
270 2
                    ->defaultValue(604800)
271 2
                ->end()
272 2
                ->scalarNode('service_id')
273 2
                    ->isRequired()
274 2
                ->end()
275 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
276 2
                ->scalarNode('prefix')->defaultValue('')->end()
277 2
            ->end();
278
279 2
        $this->normalizeEnabled($node);
280
281 2
        return $node;
282
    }
283
284
    /**
285
     * @return ArrayNodeDefinition
286
     */
287 2
    private function addDataCollectorSection()
288
    {
289 2
        $tree = new TreeBuilder();
290 2
        $node = $tree->root('data_collector');
291
292
        $node
293 2
            ->canBeEnabled()
294 2
            ->addDefaultsIfNotSet()
295 2
            ->children()
296 2
                ->booleanNode('enabled')->end()
297 2
            ->end();
298
299 2
        $this->normalizeEnabled($node);
300
301 2
        return $node;
302
    }
303
}
304