Completed
Pull Request — master (#1354)
by
unknown
11:08
created

Configuration::getPersistenceNode()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 95
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 95
rs 8.4117
cc 3
eloc 90
nc 1
nop 0

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
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
class Configuration implements ConfigurationInterface
19
{
20
    const SUPPORTED_DRIVERS = ['orm', 'mongodb', 'phpcr'];
21
22
    /**
23
     * If the kernel is running in debug mode.
24
     *
25
     * @var bool
26
     */
27
    private $debug;
28
29
    public function __construct($debug)
30
    {
31
        $this->debug = $debug;
32
    }
33
34
    /**
35
     * Generates the configuration tree.
36
     *
37
     * @return TreeBuilder
38
     */
39
    public function getConfigTreeBuilder()
40
    {
41
        $treeBuilder = new TreeBuilder();
42
        $rootNode = $treeBuilder->root('fos_elastica', 'array');
43
44
        $this->addClientsSection($rootNode);
45
        $this->addIndexesSection($rootNode);
46
47
        $rootNode
48
            ->children()
49
                ->scalarNode('default_client')
50
                    ->info('Defaults to the first client defined')
51
                ->end()
52
                ->scalarNode('default_index')
53
                    ->info('Defaults to the first index defined')
54
                ->end()
55
                ->scalarNode('default_manager')->defaultValue('orm')->end()
56
                ->arrayNode('serializer')
57
                    ->treatNullLike([])
58
                    ->children()
59
                        ->scalarNode('callback_class')->defaultValue('FOS\ElasticaBundle\Serializer\Callback')->end()
60
                        ->scalarNode('serializer')->defaultValue('serializer')->end()
61
                    ->end()
62
                ->end()
63
            ->end()
64
        ;
65
66
        return $treeBuilder;
67
    }
68
69
    /**
70
     * Returns the array node used for "dynamic_templates".
71
     */
72
    public function getDynamicTemplateNode()
73
    {
74
        $builder = new TreeBuilder();
75
        $node = $builder->root('dynamic_templates');
76
77
        $node
78
            ->prototype('array')
79
                ->prototype('array')
80
                    ->children()
81
                        ->scalarNode('match')->end()
82
                        ->scalarNode('unmatch')->end()
83
                        ->scalarNode('match_mapping_type')->end()
84
                        ->scalarNode('path_match')->end()
85
                        ->scalarNode('path_unmatch')->end()
86
                        ->scalarNode('match_pattern')->end()
87
                        ->arrayNode('mapping')
88
                            ->prototype('variable')
89
                                ->treatNullLike([])
90
                            ->end()
91
                        ->end()
92
                    ->end()
93
                ->end()
94
            ->end()
95
        ;
96
97
        return $node;
98
    }
99
100
    /**
101
     * Returns the array node used for "types".
102
     */
103
    protected function getTypesNode()
104
    {
105
        $builder = new TreeBuilder();
106
        $node = $builder->root('types');
107
108
        $node
109
            ->useAttributeAsKey('name')
110
            ->prototype('array')
111
                ->treatNullLike([])
112
                ->beforeNormalization()
113
                ->ifNull()
114
                    ->thenEmptyArray()
115
                ->end()
116
                // Support multiple dynamic_template formats to match the old bundle style
117
                // and the way ElasticSearch expects them
118
                ->beforeNormalization()
119
                ->ifTrue(function ($v) {
120
                    return isset($v['dynamic_templates']);
121
                })
122
                ->then(function ($v) {
123
                    $dt = [];
124
                    foreach ($v['dynamic_templates'] as $key => $type) {
125
                        if (is_int($key)) {
126
                            $dt[] = $type;
127
                        } else {
128
                            $dt[][$key] = $type;
129
                        }
130
                    }
131
132
                    $v['dynamic_templates'] = $dt;
133
134
                    return $v;
135
                })
136
                ->end()
137
                ->children()
138
                    ->booleanNode('date_detection')->end()
139
                    ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end()
140
                    ->scalarNode('analyzer')->end()
141
                    ->booleanNode('numeric_detection')->end()
142
                    ->scalarNode('dynamic')->end()
143
                    ->variableNode('indexable_callback')->end()
144
                    ->append($this->getPersistenceNode())
145
                    ->append($this->getSerializerNode())
146
                ->end()
147
                ->append($this->getIdNode())
148
                ->append($this->getPropertiesNode())
149
                ->append($this->getDynamicTemplateNode())
150
                ->append($this->getSourceNode())
151
                ->append($this->getRoutingNode())
152
                ->append($this->getParentNode())
153
                ->append($this->getAllNode())
154
            ->end()
155
        ;
156
157
        return $node;
158
    }
159
160
    /**
161
     * Returns the array node used for "properties".
162
     */
163
    protected function getPropertiesNode()
164
    {
165
        $builder = new TreeBuilder();
166
        $node = $builder->root('properties');
167
168
        $node
169
            ->useAttributeAsKey('name')
170
            ->prototype('variable')
171
                ->treatNullLike([]);
172
173
        return $node;
174
    }
175
176
    /**
177
     * Returns the array node used for "_id".
178
     */
179 View Code Duplication
    protected function getIdNode()
180
    {
181
        $builder = new TreeBuilder();
182
        $node = $builder->root('_id');
183
184
        $node
185
            ->children()
186
            ->scalarNode('path')->end()
187
            ->end()
188
        ;
189
190
        return $node;
191
    }
192
193
    /**
194
     * Returns the array node used for "_source".
195
     */
196
    protected function getSourceNode()
197
    {
198
        $builder = new TreeBuilder();
199
        $node = $builder->root('_source');
200
201
        $node
202
            ->children()
203
                ->arrayNode('excludes')
204
                    ->useAttributeAsKey('name')
205
                    ->prototype('scalar')->end()
206
                ->end()
207
                ->arrayNode('includes')
208
                    ->useAttributeAsKey('name')
209
                    ->prototype('scalar')->end()
210
                ->end()
211
                ->scalarNode('compress')->end()
212
                ->scalarNode('compress_threshold')->end()
213
                ->scalarNode('enabled')->defaultTrue()->end()
214
            ->end()
215
        ;
216
217
        return $node;
218
    }
219
220
    /**
221
     * Returns the array node used for "_routing".
222
     */
223 View Code Duplication
    protected function getRoutingNode()
224
    {
225
        $builder = new TreeBuilder();
226
        $node = $builder->root('_routing');
227
228
        $node
229
            ->children()
230
                ->scalarNode('required')->end()
231
                ->scalarNode('path')->end()
232
            ->end()
233
        ;
234
235
        return $node;
236
    }
237
238
    /**
239
     * Returns the array node used for "_parent".
240
     */
241
    protected function getParentNode()
242
    {
243
        $builder = new TreeBuilder();
244
        $node = $builder->root('_parent');
245
246
        $node
247
            ->children()
248
                ->scalarNode('type')->end()
249
                ->scalarNode('property')->defaultValue(null)->end()
250
                ->scalarNode('identifier')->defaultValue('id')->end()
251
            ->end()
252
        ;
253
254
        return $node;
255
    }
256
257
    /**
258
     * Returns the array node used for "_all".
259
     */
260
    protected function getAllNode()
261
    {
262
        $builder = new TreeBuilder();
263
        $node = $builder->root('_all');
264
265
        $node
266
            ->children()
267
            ->scalarNode('enabled')->defaultValue(true)->end()
268
            ->scalarNode('analyzer')->end()
269
            ->end()
270
        ;
271
272
        return $node;
273
    }
274
275
    /**
276
     * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
277
     */
278
    protected function getPersistenceNode()
279
    {
280
        $builder = new TreeBuilder();
281
        $node = $builder->root('persistence');
282
283
        $node
284
            ->validate()
285
                ->ifTrue(function ($v) {
286
                    return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']);
287
                })
288
                    ->thenInvalid('Hints are only supported by the "orm" driver')
289
            ->end()
290
            ->children()
291
                ->scalarNode('driver')
292
                    ->defaultValue('orm')
293
                    ->validate()
294
                    ->ifNotInArray(self::SUPPORTED_DRIVERS)
295
                        ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode(self::SUPPORTED_DRIVERS))
296
                    ->end()
297
                ->end()
298
                ->scalarNode('model')->defaultValue(null)->end()
299
                ->scalarNode('repository')->end()
300
                ->scalarNode('identifier')->defaultValue('id')->end()
301
                ->arrayNode('provider')
302
                    ->addDefaultsIfNotSet()
303
                    ->children()
304
                        ->scalarNode('batch_size')->defaultValue(100)->end()
305
                        ->scalarNode('clear_object_manager')->defaultTrue()->end()
306
                        ->scalarNode('debug_logging')
307
                            ->defaultValue($this->debug)
308
                            ->treatNullLike(true)
309
                        ->end()
310
                        ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
311
                        ->scalarNode('service')->end()
312
                    ->end()
313
                ->end()
314
                ->arrayNode('listener')
315
                    ->addDefaultsIfNotSet()
316
                    ->children()
317
                        ->booleanNode('enabled')->defaultTrue()->end()
318
                        ->scalarNode('insert')->defaultTrue()->end()
319
                        ->scalarNode('update')->defaultTrue()->end()
320
                        ->scalarNode('delete')->defaultTrue()->end()
321
                        ->scalarNode('flush')->defaultTrue()->end()
322
                        ->booleanNode('defer')->defaultFalse()->end()
323
                        ->scalarNode('logger')
324
                            ->defaultFalse()
325
                            ->treatNullLike('fos_elastica.logger')
326
                            ->treatTrueLike('fos_elastica.logger')
327
                        ->end()
328
                        ->scalarNode('service')->end()
329
                    ->end()
330
                ->end()
331
                ->arrayNode('finder')
332
                    ->addDefaultsIfNotSet()
333
                    ->children()
334
                        ->scalarNode('service')->end()
335
                    ->end()
336
                ->end()
337
                ->arrayNode('elastica_to_model_transformer')
338
                    ->addDefaultsIfNotSet()
339
                    ->children()
340
                        ->arrayNode('hints')
341
                            ->prototype('array')
342
                                ->children()
343
                                    ->scalarNode('name')->end()
344
                                    ->scalarNode('value')->end()
345
                                ->end()
346
                            ->end()
347
                        ->end()
348
                        ->booleanNode('hydrate')->defaultTrue()->end()
349
                        ->booleanNode('ignore_missing')
350
                            ->defaultFalse()
351
                            ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.')
352
                        ->end()
353
                        ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
354
                        ->scalarNode('service')->end()
355
                    ->end()
356
                ->end()
357
                ->arrayNode('model_to_elastica_transformer')
358
                    ->addDefaultsIfNotSet()
359
                    ->children()
360
                        ->scalarNode('service')->end()
361
                    ->end()
362
                ->end()
363
                ->arrayNode('persister')
364
                    ->addDefaultsIfNotSet()
365
                    ->children()
366
                        ->scalarNode('service')->end()
367
                    ->end()
368
                ->end()
369
            ->end();
370
371
        return $node;
372
    }
373
374
    /**
375
     * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
376
     */
377
    protected function getSerializerNode()
378
    {
379
        $builder = new TreeBuilder();
380
        $node = $builder->root('serializer');
381
382
        $node
0 ignored issues
show
Bug introduced by
The method scalarNode() does not seem to exist on object<Symfony\Component...odeDefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
383
            ->addDefaultsIfNotSet()
384
            ->children()
385
                ->arrayNode('groups')
386
                    ->treatNullLike([])
387
                    ->prototype('scalar')->end()
388
                ->end()
389
                ->scalarNode('version')->end()
390
                ->booleanNode('serialize_null')
391
                    ->defaultFalse()
392
                ->end()
393
            ->end();
394
395
        return $node;
396
    }
397
398
    /**
399
     * Adds the configuration for the "clients" key.
400
     */
401
    private function addClientsSection(ArrayNodeDefinition $rootNode)
402
    {
403
        $rootNode
404
            ->fixXmlConfig('client')
405
            ->children()
406
                ->arrayNode('clients')
407
                    ->useAttributeAsKey('id')
408
                    ->prototype('array')
409
                        ->performNoDeepMerging()
410
                        // Elastica names its properties with camel case, support both
411
                        ->beforeNormalization()
412
                        ->ifTrue(function ($v) {
413
                            return isset($v['connection_strategy']);
414
                        })
415
                        ->then(function ($v) {
416
                            $v['connectionStrategy'] = $v['connection_strategy'];
417
                            unset($v['connection_strategy']);
418
419
                            return $v;
420
                        })
421
                        ->end()
422
                        // If there is no connections array key defined, assume a single connection.
423
                        ->beforeNormalization()
424
                        ->ifTrue(function ($v) {
425
                            return is_array($v) && !array_key_exists('connections', $v);
426
                        })
427
                        ->then(function ($v) {
428
                            return [
429
                                'connections' => [$v],
430
                            ];
431
                        })
432
                        ->end()
433
                        ->children()
434
                            ->arrayNode('connections')
435
                                ->requiresAtLeastOneElement()
436
                                ->prototype('array')
437
                                    ->fixXmlConfig('header')
438
                                    ->children()
439
                                        ->scalarNode('url')
440
                                            ->validate()
441
                                                ->ifTrue(function ($url) {
442
                                                    return $url && '/' !== substr($url, -1);
443
                                                })
444
                                                ->then(function ($url) {
445
                                                    return $url.'/';
446
                                                })
447
                                            ->end()
448
                                        ->end()
449
                                        ->scalarNode('username')->end()
450
                                        ->scalarNode('password')->end()
451
                                        ->scalarNode('host')->end()
452
                                        ->scalarNode('port')->end()
453
                                        ->scalarNode('proxy')->end()
454
                                        ->scalarNode('aws_access_key_id')->end()
455
                                        ->scalarNode('aws_secret_access_key')->end()
456
                                        ->scalarNode('aws_region')->end()
457
                                        ->scalarNode('aws_session_token')->end()
458
                                        ->booleanNode('ssl')->defaultValue(false)->end()
459
                                        ->scalarNode('logger')
460
                                            ->defaultValue($this->debug ? 'fos_elastica.logger' : false)
461
                                            ->treatNullLike('fos_elastica.logger')
462
                                            ->treatTrueLike('fos_elastica.logger')
463
                                        ->end()
464
                                        ->booleanNode('compression')->defaultValue(false)->end()
465
                                        ->arrayNode('headers')
466
                                            ->normalizeKeys(false)
467
                                            ->useAttributeAsKey('name')
468
                                            ->prototype('scalar')->end()
469
                                        ->end()
470
                                        ->arrayNode('curl')
471
                                            ->useAttributeAsKey(CURLOPT_SSL_VERIFYPEER)
472
                                            ->prototype('boolean')->end()
473
                                        ->end()
474
                                        ->scalarNode('transport')->end()
475
                                        ->scalarNode('timeout')->end()
476
                                        ->scalarNode('connectTimeout')->end()
477
                                        ->scalarNode('retryOnConflict')
478
                                            ->defaultValue(0)
479
                                        ->end()
480
                                    ->end()
481
                                ->end()
482
                            ->end()
483
                            ->scalarNode('timeout')->end()
484
                            ->scalarNode('connectTimeout')->end()
485
                            ->scalarNode('headers')->end()
486
                            ->scalarNode('connectionStrategy')->defaultValue('Simple')->end()
487
                        ->end()
488
                    ->end()
489
                ->end()
490
            ->end()
491
        ;
492
    }
493
494
    /**
495
     * Adds the configuration for the "indexes" key.
496
     */
497
    private function addIndexesSection(ArrayNodeDefinition $rootNode)
498
    {
499
        $rootNode
500
            ->fixXmlConfig('index')
501
            ->children()
502
                ->arrayNode('indexes')
503
                    ->useAttributeAsKey('name')
504
                    ->prototype('array')
505
                        ->children()
506
                            ->scalarNode('index_name')
507
                                ->info('Defaults to the name of the index, but can be modified if the index name is different in ElasticSearch')
508
                            ->end()
509
                            ->booleanNode('use_alias')->defaultValue(false)->end()
510
                            ->scalarNode('client')->end()
511
                            ->scalarNode('finder')
512
                                ->treatNullLike(true)
513
                                ->defaultFalse()
514
                            ->end()
515
                            ->arrayNode('type_prototype')
516
                                ->children()
517
                                    ->scalarNode('analyzer')->end()
518
                                    ->append($this->getPersistenceNode())
519
                                    ->append($this->getSerializerNode())
520
                                ->end()
521
                            ->end()
522
                            ->variableNode('settings')->defaultValue([])->end()
523
                        ->end()
524
                        ->append($this->getTypesNode())
525
                    ->end()
526
                ->end()
527
            ->end()
528
        ;
529
    }
530
}
531