Completed
Push — master ( d71bb8...1594d5 )
by Alessandro
9s
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 41
cts 41
cp 1
rs 9.0303
c 0
b 0
f 0
cc 1
eloc 43
nc 1
nop 0
crap 1
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
 * @internal
11
 */
12
final class Configuration implements ConfigurationInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 11
    public function getConfigTreeBuilder()
18
    {
19 11
        $readPreferenceValidOptions = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
20
21 11
        $treeBuilder = new TreeBuilder();
22 11
        $rootNode = $treeBuilder->root('mongo_db_bundle');
23
        $rootNode
24 11
            ->children()
25 11
            ->booleanNode('data_collection')->defaultTrue()->info('Use to disable Data Collection if needed')
26 11
            ->end()
27 11
            ->arrayNode('clients')->isRequired()->requiresAtLeastOneElement()
28 11
                ->useAttributeAsKey('name')
29 11
                ->prototype('array')
30 11
                ->children()
31 11
                    ->arrayNode('hosts')->info('Your MongoDB hosts addresses and ports')
32 11
                        ->prototype('array')
33 11
                            ->children()
34 11
                                ->scalarNode('host')->isRequired()->end()
35 11
                                ->integerNode('port')->defaultValue(27017)->end()
36 11
                            ->end()
37 11
                        ->end()
38 11
                    ->end()
39 11
                    ->scalarNode('username')->defaultValue('')->end()
40 11
                    ->scalarNode('password')->defaultValue('')->end()
41 11
                    ->scalarNode('readPreference')
42 11
                        ->defaultValue('primaryPreferred')
43 11
                        ->validate()
44 11
                            ->ifNotInArray($readPreferenceValidOptions)
45 11
                            ->thenInvalid('Invalid readPreference option %s, must be one of ['.implode(", ", $readPreferenceValidOptions).']')
46 11
                        ->end()
47 11
                    ->end()
48 11
                    ->scalarNode('replicaSet')->defaultValue(null)->end()
49 11
                    ->booleanNode('ssl')->defaultValue(false)->end()
50 11
                    ->integerNode('connectTimeoutMS')->defaultValue(null)->end();
51
        $rootNode
52 11
            ->children()
53 11
            ->arrayNode('connections')->isRequired()->requiresAtLeastOneElement()
54 11
                ->useAttributeAsKey('name')
55 11
                ->prototype('array')
56 11
                ->children()
57 11
                    ->scalarNode('client_name')->isRequired()->info('Your defined client name')->end()
58 11
                    ->scalarNode('database_name')->isRequired()->info('Your MongoDB database name')->end()
59 11
                ->end()
60 11
            ->end();
61
62 11
        return $treeBuilder;
63
    }
64
}
65