Completed
Push — master ( 85ee03...d3810b )
by Asmir
04:47
created

Configuration::addVisitorsSection()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 68
Code Lines 55

Duplication

Lines 14
Ratio 20.59 %

Code Coverage

Tests 51
CRAP Score 7.0348

Importance

Changes 0
Metric Value
dl 14
loc 68
rs 6.9654
c 0
b 0
f 0
ccs 51
cts 56
cp 0.9107
cc 7
eloc 55
nc 1
nop 1
crap 7.0348

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\SerializerBundle\DependencyInjection;
20
21
use JMS\Serializer\Exception\InvalidArgumentException;
22
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
26
27
class Configuration implements ConfigurationInterface
28
{
29
    private $debug;
30
31
    /**
32
     * @param boolean $debug
33
     */
34 52
    public function __construct($debug = false)
35
    {
36 52
        $this->debug = $debug;
37 52
    }
38
39 52
    public function getConfigTreeBuilder()
40 1
    {
41 52
        $tb = new TreeBuilder();
42
43
        $root = $tb
44 52
            ->root('jms_serializer', 'array')
45 52
                ->children()
46 52
        ;
47
48 52
        $this->addHandlersSection($root);
49 52
        $this->addSubscribersSection($root);
50 52
        $this->addObjectConstructorsSection($root);
51 52
        $this->addSerializersSection($root);
52 52
        $this->addMetadataSection($root);
53 52
        $this->addVisitorsSection($root);
54 52
        $this->addContextSection($root);
55
56 52
        return $tb;
57
    }
58
59 52
    private function addHandlersSection(NodeBuilder $builder)
60
    {
61
        $builder
62 52
            ->arrayNode('handlers')
63 52
                ->addDefaultsIfNotSet()
64 52
                ->children()
65 52
                    ->arrayNode('datetime')
66 52
                        ->addDefaultsIfNotSet()
67 52
                        ->children()
68 52
                            ->scalarNode('default_format')->defaultValue(\DateTime::RFC3339)->end()
69 52
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
70 52
                            ->scalarNode('cdata')->defaultTrue()->end()
71 52
                        ->end()
72 52
                    ->end()
73 52
                    ->arrayNode('array_collection')
74 52
                        ->addDefaultsIfNotSet()
75 52
                        ->children()
76 52
                            ->booleanNode('initialize_excluded')->defaultFalse()->end()
77 52
                        ->end()
78 52
                    ->end()
79 52
                ->end()
80 52
            ->end()
81
        ;
82 52
    }
83
84 52
    private function addSubscribersSection(NodeBuilder $builder)
85
    {
86
        $builder
87 52
            ->arrayNode('subscribers')
88 52
                ->addDefaultsIfNotSet()
89 52
                    ->children()
90 52
                        ->arrayNode('doctrine_proxy')
91 52
                        ->addDefaultsIfNotSet()
92 52
                        ->children()
93 52
                            ->booleanNode('initialize_excluded')->defaultFalse()->end()
94 52
                            ->booleanNode('initialize_virtual_types')->defaultFalse()->end()
95 52
                        ->end()
96 52
                    ->end()
97 52
                ->end()
98 52
            ->end()
99
        ;
100 52
    }
101
102 52
    private function addObjectConstructorsSection(NodeBuilder $builder)
103
    {
104
        $builder
105 52
            ->arrayNode('object_constructors')
106 52
                ->addDefaultsIfNotSet()
107 52
                    ->children()
108 52
                        ->arrayNode('doctrine')
109 52
                        ->addDefaultsIfNotSet()
110 52
                        ->children()
111 52
                            ->enumNode('fallback_strategy')
112 52
                                ->defaultValue("null")
113 52
                                ->values(["null", "exception", "fallback"])
114 52
                            ->end()
115 52
                        ->end()
116 52
                    ->end()
117 52
                ->end()
118 52
            ->end()
119
        ;
120 52
    }
121
122 52
    private function addSerializersSection(NodeBuilder $builder)
123
    {
124
        $builder
125 52
            ->arrayNode('property_naming')
126 52
                ->addDefaultsIfNotSet()
127 52
                ->beforeNormalization()
128 52
                    ->ifString()
129
                    ->then(function ($id) {
130 1
                        return array('id' => $id);
131 52
                    })
132 52
                ->end()
133 52
                ->children()
134 52
                    ->scalarNode('id')->cannotBeEmpty()->end()
135 52
                    ->scalarNode('separator')->defaultValue('_')->end()
136 52
                    ->booleanNode('lower_case')->defaultTrue()->end()
137 52
                    ->booleanNode('enable_cache')->defaultTrue()->end()
138 52
                ->end()
139 52
            ->end()
140 52
            ->arrayNode('expression_evaluator')
141 52
                ->addDefaultsIfNotSet()
142 52
                ->beforeNormalization()
143 52
                    ->ifString()
144
                    ->then(function ($id) {
145 1
                        return array('id' => $id);
146 52
                    })
147 52
                ->end()
148 52
                ->children()
149 52
                    ->scalarNode('id')
150
                        ->defaultValue(function () {
151 48
                            if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
152 48
                                return 'jms_serializer.expression_evaluator';
153
                            }
154
                            return null;
155 52
                        })
156 52
                        ->validate()
157
                            ->always(function($v) {
158 3
                                if (!empty($v) && !interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
159
                                    throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features');
160
                                }
161 3
                                return $v;
162 52
                            })
163 52
                        ->end()
164 52
                ->end()
165 52
            ->end()
166
        ;
167 52
    }
168
169 52
    private function addMetadataSection(NodeBuilder $builder)
170
    {
171
        $builder
172 52
            ->arrayNode('metadata')
173 52
                ->addDefaultsIfNotSet()
174 52
                ->fixXmlConfig('directory', 'directories')
175 52
                ->children()
176 52
                    ->scalarNode('cache')->defaultValue('file')->end()
177 52
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
178 52
                    ->arrayNode('file_cache')
179 52
                        ->addDefaultsIfNotSet()
180 52
                        ->children()
181 52
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
182 52
                        ->end()
183 52
                    ->end()
184 52
                    ->booleanNode('auto_detection')->defaultTrue()->end()
185 52
                    ->booleanNode('infer_types_from_doctrine_metadata')
186 52
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
187 52
                        ->defaultTrue()
188 52
                    ->end()
189 52
                    ->arrayNode('directories')
190 52
                        ->useAttributeAsKey('name')
191 52
                        ->prototype('array')
192 52
                            ->children()
193 52
                                ->scalarNode('path')->isRequired()->end()
194 52
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
195 52
                            ->end()
196 52
                        ->end()
197 52
                    ->end()
198 52
                ->end()
199 52
            ->end()
200
        ;
201 52
    }
202
203 52
    private function addVisitorsSection(NodeBuilder $builder)
204
    {
205
        $builder
206 52
            ->arrayNode('visitors')
207 52
                ->addDefaultsIfNotSet()
208 52
                ->children()
209 52
                    ->arrayNode('json')
210 52
                        ->addDefaultsIfNotSet()
211 52
                        ->children()
212 52
                            ->scalarNode('options')
213 52
                                ->defaultValue(0)
214 52
                                ->beforeNormalization()
215
                                    ->ifArray()->then(function($v) {
216 1
                                        $options = 0;
217 1
                                        foreach ($v as $option) {
218 1 View Code Duplication
                                            if (is_numeric($option)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
219
                                                $options |= (int) $option;
220 1
                                            } elseif (defined($option)) {
221 1
                                                $options |= constant($option);
222 1
                                            } else {
223
                                                throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
224
                                            }
225 1
                                        }
226
227 1
                                        return $options;
228 52
                                    })
229 52
                                ->end()
230 52
                                ->beforeNormalization()
231
                                    ->ifString()->then(function($v) {
232 1 View Code Duplication
                                        if (is_numeric($v)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
233
                                            $value = (int) $v;
234 1
                                        } elseif (defined($v)) {
235 1
                                            $value = constant($v);
236 1
                                        } else {
237
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
238
                                        }
239
240 1
                                        return $value;
241 52
                                    })
242 52
                                ->end()
243 52
                                ->validate()
244
                                    ->always(function($v) {
245 3
                                        if (!is_int($v)) {
246
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
247
                                        }
248
249 3
                                        return $v;
250 52
                                    })
251 52
                                ->end()
252 52
                            ->end()
253 52
                        ->end()
254 52
                    ->end()
255 52
                    ->arrayNode('xml')
256 52
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
257 52
                        ->addDefaultsIfNotSet()
258 52
                        ->children()
259 52
                            ->arrayNode('doctype_whitelist')
260 52
                                ->prototype('scalar')->end()
261 52
                            ->end()
262 52
                            ->booleanNode('format_output')
263 52
                                ->defaultTrue()
264 52
                            ->end()
265 52
                        ->end()
266 52
                    ->end()
267 52
                ->end()
268 52
            ->end()
269
        ;
270 52
    }
271
272 52
    private function addContextSection(NodeBuilder $builder)
273
    {
274
        $root = $builder
275 52
                    ->arrayNode('default_context')
276 52
                    ->addDefaultsIfNotSet();
277
278 52
        $this->createContextNode($root->children(), 'serialization');
279 52
        $this->createContextNode($root->children(), 'deserialization');
280 52
    }
281
282 52
    private function createContextNode(NodeBuilder $builder, $name)
283
    {
284
        $builder
285 52
            ->arrayNode($name)
286 52
                ->addDefaultsIfNotSet()
287 52
                ->beforeNormalization()
288 52
                    ->ifString()
289
                    ->then(function ($id) {
290 1
                        return array('id' => $id);
291 52
                    })
292 52
                ->end()
293
                ->validate()->always(function ($v) {
294 6
                    if (!empty($v['id'])) {
295 2
                        return array('id' => $v['id']);
296
                    }
297 4
                    return $v;
298 52
                })->end()
299 52
                ->children()
300 52
                    ->scalarNode('id')->cannotBeEmpty()->end()
301 52
                    ->scalarNode('serialize_null')
302 52
                        ->validate()->always(function ($v) {
303
                            if (!in_array($v, array(true, false, NULL), true)){
304
                                throw new InvalidTypeException("Expected boolean or NULL for the serialize_null option");
305
                            }
306
                            return $v;
307 52
                        })
308 52
                        ->ifNull()->thenUnset()
309 52
                        ->end()
310 52
                        ->info('Flag if null values should be serialized')
311 52
                    ->end()
312 52
                    ->scalarNode('enable_max_depth_checks')
313 52
                        ->info('Flag to enable the max-depth exclusion strategy')
314 52
                    ->end()
315 52
                    ->arrayNode('attributes')
316 52
                        ->fixXmlConfig('attribute')
317 52
                        ->useAttributeAsKey('key')
318 52
                        ->prototype('scalar')->end()
319 52
                        ->info('Arbitrary key-value data for context')
320 52
                    ->end()
321 52
                    ->arrayNode('groups')
322 52
                        ->fixXmlConfig('group')
323 52
                        ->prototype('scalar')->end()
324 52
                        ->info('Default serialization groups')
325 52
                    ->end()
326 52
                    ->scalarNode('version')
327 52
                        ->validate()->ifNull()->thenUnset()->end()
328 52
                        ->info('Application version to use in exclusion strategies')
329 52
                    ->end()
330 52
                ->end()
331 52
            ->end();
332 52
    }
333
}
334