Completed
Pull Request — master (#64)
by .
09:24 queued 01:52
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 41
cts 41
cp 1
rs 8.9599
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1

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
namespace Facile\MongoDbBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
8
/**
9
 * Class Configuration.
10
 *
11
 * @internal
12
 */
13
final class Configuration implements ConfigurationInterface
14
{
15
    /**
16
     * {@inheritdoc}
17 12
     */
18
    public function getConfigTreeBuilder()
19 12
    {
20
        $readPreferenceValidOptions = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
21 12
22 12
        $treeBuilder = new TreeBuilder();
23
        $rootNode = $treeBuilder->root('mongo_db_bundle');
24 12
        $rootNode
25 12
            ->children()
26 12
                ->booleanNode('data_collection')->defaultTrue()->info('Disables Data Collection if needed')->end()
27 12
                ->arrayNode('clients')
28 12
                    ->isRequired()
29 12
                    ->requiresAtLeastOneElement()
30 12
                    ->useAttributeAsKey('name')
31 12
                    ->prototype('array')
32 12
                        ->children()
33 12
                            ->arrayNode('hosts')->info('Hosts addresses and ports')
34 12
                                ->prototype('array')
35 12
                                    ->children()
36 12
                                        ->scalarNode('host')->isRequired()->end()
37 12
                                        ->integerNode('port')->defaultValue(27017)->end()
38 12
                                    ->end()
39 12
                                ->end()
40 12
                            ->end()
41 12
                            ->scalarNode('uri')->defaultNull()->info('Overrides hosts configuration')->end()
42 12
                            ->scalarNode('username')->defaultValue('')->end()
43 12
                            ->scalarNode('password')->defaultValue('')->end()
44 12
                            ->scalarNode('authSource')->defaultNull()->info('Database name associated with the user’s credentials')->end()
45 12
                            ->scalarNode('readPreference')
46 12
                                ->defaultValue('primaryPreferred')
47 12
                                ->validate()
48 12
                                    ->ifNotInArray($readPreferenceValidOptions)
49 12
                                    ->thenInvalid('Invalid readPreference option %s, must be one of ['.implode(', ', $readPreferenceValidOptions).']')
50 12
                                ->end()
51 12
                            ->end()
52
                            ->scalarNode('replicaSet')->defaultNull()->end()
53 12
                            ->booleanNode('ssl')->defaultFalse()->end()
54 12
                            ->integerNode('connectTimeoutMS')->defaultNull()->end()
55 12
                        ->end()
56 12
                    ->end()
57 12
                ->end()
58 12
            ->end();
59 12
        $rootNode
60 12
            ->children()
61 12
                ->arrayNode('connections')->isRequired()->requiresAtLeastOneElement()
62
                    ->useAttributeAsKey('name')
63 12
                    ->prototype('array')
64
                        ->children()
65
                            ->scalarNode('client_name')->isRequired()->info('Desired client name')->end()
66
                            ->scalarNode('database_name')->isRequired()->info('Database name')->end()
67
                        ->end()
68
                    ->end()
69
                ->end()
70
            ->end();
71
72
        return $treeBuilder;
73
    }
74
}
75