Completed
Push — master ( 3db674...2e3aab )
by Remy
03:46
created

Configuration::addConnectionsSection()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 81
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 2.0092

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 81
ccs 59
cts 68
cp 0.8676
rs 8.8076
cc 2
eloc 66
nc 1
nop 1
crap 2.0092

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 Pouzor\MongoDBBundle\DependencyInjection;
4
5
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
class Configuration implements ConfigurationInterface
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 1
    public function getConfigTreeBuilder()
16
    {
17 1
        $treeBuilder = new TreeBuilder();
18 1
        $rootNode = $treeBuilder->root('mongodb');
19
20
        $rootNode
21 1
            ->children()
22 1
                ->scalarNode('default_connection')->end()
23 1
            ->end();
24
25 1
        $this->addConnectionsSection($rootNode);
26 1
        $this->addDbIndexesSection($rootNode);
27
28 1
        return $treeBuilder;
29
    }
30
31
32
    /**
33
     * add Mongo Connections section
34
     *
35
     * @param ArrayNodeDefinition $rootNode
36
     */
37 1
    private function addConnectionsSection(ArrayNodeDefinition $rootNode)
38
    {
39
        $rootNode
40 1
            ->children()
41 1
                ->scalarNode('manager')
42 1
                    ->defaultValue('document.manager')
43 1
                ->end()
44 1
            ->end()
45 1
            ->fixXmlConfig('connection')
46 1
            ->children()
47 1
                ->arrayNode('connections')
48 1
                    ->useAttributeAsKey('id')
49 1
                    ->prototype('array')
50 1
                    ->performNoDeepMerging()
51 1
                    ->children()
52 1
                        ->scalarNode('host')->defaultValue('127.0.0.1:27017')->end()
53 1
                        ->scalarNode('db')->end()
54 1
                        ->scalarNode('password')->end()
55 1
                        ->scalarNode('username')->end()
56 1
                        ->scalarNode('schema')->end()
57 1
                        ->arrayNode('options')
58 1
                            ->performNoDeepMerging()
59 1
                            ->children()
60 1
                                ->booleanNode('connect')->end()
61 1
                                ->integerNode('wtimeoutMS')->defaultValue(0)->end()
62 1
                                ->scalarNode('connectTimeoutMS')->end()
63 1
                                ->booleanNode('journal')->end()
64 1
                                ->enumNode('readPreference')
65 1
                                    ->values(array('primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'))
66 1
                                ->end()
67 1
                                ->arrayNode('readPreferenceTags')
68 1
                                    ->performNoDeepMerging()
69 1
                                    ->prototype('array')
70 1
                                    ->beforeNormalization()
71
                                    // Handle readPreferenceTag XML nodes
72 1
                                    ->ifTrue(
73
                                        function ($v) {
74
                                            return isset($v['readPreferenceTag']);
75
                                        }
76 1
                                    )
77 1
                                    ->then(
78
                                        function ($v) {
79
                                            // Equivalent of fixXmlConfig() for inner node
80
                                            if (isset($v['readPreferenceTag']['name'])) {
81
                                                $v['readPreferenceTag'] = array($v['readPreferenceTag']);
82
                                            }
83
84
                                            return $v['readPreferenceTag'];
85
                                        }
86 1
                                    )
87 1
                                ->end()
88 1
                                ->useAttributeAsKey('name')
89 1
                                ->prototype('scalar')->end()
90 1
                            ->end()
91 1
                        ->end()
92 1
                        ->scalarNode('replicaSet')->end()
93 1
                        ->scalarNode('socketTimeoutMS')->end()
94 1
                        ->booleanNode('ssl')->end()
95 1
                        ->scalarNode('w')->end()
96 1
                        ->scalarNode('wTimeoutMS')->end()
97 1
                    ->end()
98 1
                    ->validate()
99 1
                    ->ifTrue(
100
                        function ($v) {
101
                            return count($v['readPreferenceTags']) === 0;
102
                        }
103 1
                    )
104 1
                    ->then(
105
                        function ($v) {
106
                            unset($v['readPreferenceTags']);
107
108
                            return $v;
109
                        }
110 1
                    )
111 1
                    ->end()
112 1
                ->end()
113 1
            ->end()
114 1
            ->end()
115 1
            ->end()
116 1
            ->end();
117 1
    }
118
119
    /**
120
     * add Mongo Connections section
121
     *
122
     * @param ArrayNodeDefinition $rootNode
123
     */
124 1
    private function addDbIndexesSection(ArrayNodeDefinition $rootNode)
125
    {
126
        $rootNode
127 1
            ->fixXmlConfig('database')
128 1
            ->children()
129 1
            ->arrayNode('databases')
130 1
            ->useAttributeAsKey('id')
131 1
            ->prototype('array')
132 1
            ->children()
133 1
            ->scalarNode('connection')
134 1
            ->isRequired()
135 1
            ->cannotBeEmpty()
136 1
            ->end()
137 1
            ->scalarNode('db')
138 1
            ->isRequired()
139 1
            ->cannotBeEmpty()
140 1
            ->end()
141 1
            ->scalarNode('configFile')
142 1
            ->isRequired()
143 1
            ->cannotBeEmpty()
144 1
            ->end()
145 1
            ->end()
146 1
            ->end()
147 1
            ->end()
148 1
            ->end();
149 1
    }
150
}
151