Completed
Push — master ( 7a44f1...fb4ce1 )
by Aaron
08:12
created

Configuration::normalizeEnabled()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 13
cp 0.8462
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 1
nop 1
crap 3.0327
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
     * {@inheritdoc}
52
     */
53 2
    private function normalizeEnabled(NodeDefinition $node)
54
    {
55 2
        $node->beforeNormalization()
56 2
                ->always()
57 2
                ->then(
58 2
                    function ($v) {
59 2
                        if (is_string($v['enabled'])) {
60
                            $v['enabled'] = $v['enabled'] === 'true';
61
                        }
62 2
                        if (is_int($v['enabled'])) {
63
                            $v['enabled'] = $v['enabled'] === 1;
64
                        }
65
66 2
                        return $v;
67 2
                    }
68
                )
69 2
            ->end();
70
71 2
        return $this;
72
    }
73
74
    /**
75
     * Configure the "cache.session" section.
76
     *
77
     * @return ArrayNodeDefinition
78
     */
79 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...
80
    {
81 2
        $tree = new TreeBuilder();
82 2
        $node = $tree->root('session');
83
84
        $node
85 2
            ->canBeEnabled()
86 2
            ->addDefaultsIfNotSet()
87 2
            ->children()
88 2
                ->scalarNode('service_id')->isRequired()->end()
89 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
90 2
                ->scalarNode('prefix')->defaultValue('session_')->end()
91 2
                ->scalarNode('ttl')->end()
92 2
            ->end();
93
94 2
        $this->normalizeEnabled($node);
95
96 2
        return $node;
97
    }
98
99
    /**
100
     * Configure the "cache.serializer" section.
101
     *
102
     * @return ArrayNodeDefinition
103
     */
104 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...
105
    {
106 2
        $tree = new TreeBuilder();
107 2
        $node = $tree->root('serializer');
108
109
        $node
110 2
            ->canBeEnabled()
111 2
            ->addDefaultsIfNotSet()
112 2
            ->children()
113 2
            ->scalarNode('service_id')->isRequired()->end()
114 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
115 2
                ->scalarNode('prefix')->defaultValue('')->end()
116 2
            ->end();
117
118 2
        $this->normalizeEnabled($node);
119
120 2
        return $node;
121
    }
122
123
    /**
124
     * Configure the "cache.serializer" section.
125
     *
126
     * @return ArrayNodeDefinition
127
     */
128 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...
129
    {
130 2
        $tree = new TreeBuilder();
131 2
        $node = $tree->root('validation');
132
133
        $node
134 2
            ->canBeEnabled()
135 2
            ->addDefaultsIfNotSet()
136 2
            ->children()
137 2
                ->scalarNode('service_id')->isRequired()->end()
138 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
139 2
                ->scalarNode('prefix')->defaultValue('')->end()
140 2
            ->end();
141
142 2
        $this->normalizeEnabled($node);
143
144 2
        return $node;
145
    }
146
147
    /**
148
     * Configure the "cache.annotation" section.
149
     *
150
     * @return ArrayNodeDefinition
151
     */
152 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...
153
    {
154 2
        $tree = new TreeBuilder();
155 2
        $node = $tree->root('annotation');
156
157
        $node
158 2
            ->canBeEnabled()
159 2
            ->addDefaultsIfNotSet()
160 2
            ->children()
161 2
                ->scalarNode('service_id')->isRequired()->end()
162 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
163 2
                ->scalarNode('prefix')->defaultValue('')->end()
164 2
            ->end();
165
166 2
        $this->normalizeEnabled($node);
167
168 2
        return $node;
169
    }
170
171
    /**
172
     * @return ArrayNodeDefinition
173
     */
174 2
    private function addLoggingSection()
175
    {
176 2
        $tree = new TreeBuilder();
177 2
        $node = $tree->root('logging');
178
179
        $node
180 2
            ->canBeEnabled()
181 2
            ->addDefaultsIfNotSet()
182 2
            ->children()
183 2
                ->scalarNode('logger')->defaultValue('logger')->end()
184 2
                ->scalarNode('level')->defaultValue('info')->end()
185 2
            ->end();
186
187 2
        $this->normalizeEnabled($node);
188
189 2
        return $node;
190
    }
191
192
    /**
193
     * Configure the "cache.doctrine" section.
194
     *
195
     * @return ArrayNodeDefinition
196
     */
197 2
    private function addDoctrineSection()
198
    {
199 2
        $tree = new TreeBuilder();
200 2
        $node = $tree->root('doctrine');
201
202
        $node
203 2
            ->canBeEnabled()
204 2
            ->addDefaultsIfNotSet()
205 2
            ->children()
206 2
                ->booleanNode('use_tagging')
207 2
                    ->defaultTrue()
208 2
                ->end()
209 2
            ->end();
210
211 2
        $this->normalizeEnabled($node);
212
213 2
        $types = ['metadata', 'result', 'query'];
214 2
        foreach ($types as $type) {
215 2
            $node->children()
216 2
                    ->arrayNode($type)
217 2
                        ->canBeUnset()
218 2
                        ->children()
219 2
                            ->scalarNode('service_id')->isRequired()->end()
220 2
                            ->arrayNode('entity_managers')
221 2
                                ->defaultValue([])
222 2
                                ->beforeNormalization()
223 2
                                    ->ifString()
224 2
                                    ->then(
225 2
                                        function ($v) {
226
                                            return (array) $v;
227 2
                                        }
228
                                    )
229 2
                                    ->end()
230 2
                                    ->prototype('scalar')->end()
231 2
                                ->end()
232 2
                            ->arrayNode('document_managers')
233 2
                                ->defaultValue([])
234 2
                                ->beforeNormalization()
235 2
                                    ->ifString()
236 2
                                    ->then(
237 2
                                        function ($v) {
238
                                            return (array) $v;
239 2
                                        }
240
                                    )
241 2
                                ->end()
242 2
                                ->prototype('scalar')->end()
243 2
                            ->end()
244 2
                    ->end()
245 2
                ->end();
246
        }
247
248 2
        return $node;
249
    }
250
251
    /**
252
     * Configure the "cache.router" section.
253
     *
254
     * @return ArrayNodeDefinition
255
     */
256 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...
257
    {
258 2
        $tree = new TreeBuilder();
259 2
        $node = $tree->root('router');
260
261
        $node
262 2
            ->canBeEnabled()
263 2
            ->addDefaultsIfNotSet()
264 2
            ->children()
265 2
                ->integerNode('ttl')
266 2
                    ->defaultValue(604800)
267 2
                ->end()
268 2
                ->scalarNode('service_id')
269 2
                    ->isRequired()
270 2
                ->end()
271 2
                ->booleanNode('use_tagging')->defaultTrue()->end()
272 2
                ->scalarNode('prefix')->defaultValue('')->end()
273 2
            ->end();
274
275 2
        $this->normalizeEnabled($node);
276
277 2
        return $node;
278
    }
279
280
    /**
281
     * @return ArrayNodeDefinition
282
     */
283 2
    private function addDataCollectorSection()
284
    {
285 2
        $tree = new TreeBuilder();
286 2
        $node = $tree->root('data_collector');
287
288
        $node
289 2
            ->canBeEnabled()
290 2
            ->addDefaultsIfNotSet()
291 2
            ->children()
292 2
                ->booleanNode('enabled')->end()
293 2
            ->end();
294
295 2
        $this->normalizeEnabled($node);
296
297 2
        return $node;
298
    }
299
}
300