Passed
Push — master ( 3880dd...1a3c5e )
by Ilario
04:06
created

Configuration::addConnections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 19
ccs 0
cts 0
cp 0
crap 2
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
namespace Facile\MongoDbBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
/**
10
 * Class Configuration.
11
 *
12
 * @internal
13
 */
14
final class Configuration implements ConfigurationInterface
15
{
16
    const READ_PREFERENCE_VALID_OPTIONS = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest'];
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 13
    public function getConfigTreeBuilder()
22
    {
23 13
        $treeBuilder = new TreeBuilder('mongo_db_bundle');
24 13
        $rootBuilder = \method_exists(TreeBuilder::class, 'getRootNode')
25
            ? $treeBuilder->getRootNode()
26 13
            : $treeBuilder->root('mongo_db_bundle');
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

26
            : /** @scrutinizer ignore-deprecated */ $treeBuilder->root('mongo_db_bundle');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
27 13
28 13
        $this->addDataCollection($rootBuilder->children());
29
        $this->addClients($rootBuilder->children());
30 13
        $this->addConnections($rootBuilder->children());
31
        $this->addDriversOptionFactory($rootBuilder->children());
32
33 13
        return $treeBuilder;
34
    }
35
36 13
    private function addDataCollection(NodeBuilder $builder)
37 13
    {
38 13
        $builder
39 13
            ->booleanNode('data_collection')
40
            ->defaultTrue()
41 13
            ->info('Disables Data Collection if needed');
42
    }
43
44 13
    private function addClients(NodeBuilder $builder)
45 13
    {
46 13
        $clientsBuilder = $builder
47 13
            ->arrayNode('clients')
48 13
            ->isRequired()
49 13
            ->requiresAtLeastOneElement()
50
            ->useAttributeAsKey('name')
51 13
            ->prototype('array')
52
            ->children();
53
54 13
        $this->addClientsHosts($clientsBuilder);
55 13
56 13
        $clientsBuilder
57
            ->scalarNode('uri')
58
            ->defaultNull()
59 13
            ->info('Overrides hosts configuration');
60 13
61
        $clientsBuilder
62
            ->scalarNode('username')
63 13
            ->defaultValue('');
64 13
65
        $clientsBuilder
66
            ->scalarNode('password')
67 13
            ->defaultValue('');
68 13
69 13
        $clientsBuilder
70
            ->scalarNode('authSource')
71
            ->defaultNull()
72 13
            ->info('Database name associated with the user’s credentials');
73 13
74 13
        $clientsBuilder
75 13
            ->scalarNode('readPreference')
76 13
            ->defaultValue('primaryPreferred')
77
            ->validate()
78
            ->ifNotInArray(self::READ_PREFERENCE_VALID_OPTIONS)
79 13
            ->thenInvalid('Invalid readPreference option %s, must be one of [' . implode(', ', self::READ_PREFERENCE_VALID_OPTIONS) . ']');
80 13
81
        $clientsBuilder
82
            ->scalarNode('replicaSet')
83 13
            ->defaultNull();
84 13
85
        $clientsBuilder
86
            ->booleanNode('ssl')
87 13
            ->defaultFalse();
88 13
89 13
        $clientsBuilder
90
            ->integerNode('connectTimeoutMS')
91 13
            ->defaultNull();
92
    }
93
94 13
    private function addClientsHosts(NodeBuilder $builder)
95 13
    {
96 13
        $hostsBuilder = $builder
97 13
            ->arrayNode('hosts')
98
            ->info('Hosts addresses and ports')
99
            ->prototype('array')
100 13
            ->children();
101 13
102
        $hostsBuilder
103
            ->scalarNode('host')
104 13
            ->isRequired();
105 13
106 13
        $hostsBuilder
107
            ->integerNode('port')
108 13
            ->defaultValue(27017);
109
    }
110
111 13
    private function addDriverOptions(NodeBuilder $builder)
0 ignored issues
show
Unused Code introduced by
The method addDriverOptions() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
112 13
    {
113 13
        $driverOptionsBuilder = $builder
114 13
            ->arrayNode('driverOptions')
115 13
            ->info('Available options for driver')
116 13
            ->children();
117
118
        $driverOptionsBuilder
119 13
            ->variableNode('context')
120 13
            ->defaultValue([])
121 13
            ->info('Service id name');
122
    }
123
124 13
    private function addDriversOptionFactory(NodeBuilder $builder)
125 13
    {
126 13
        $connectionBuilder = $builder
0 ignored issues
show
Unused Code introduced by
The assignment to $connectionBuilder is dead and can be removed.
Loading history...
127 13
            ->scalarNode('driverOptions');
128
    }
129
130
    private function addConnections(NodeBuilder $builder)
131
    {
132
        $connectionBuilder = $builder
133
            ->arrayNode('connections')
134
            ->isRequired()
135
            ->requiresAtLeastOneElement()
136
            ->useAttributeAsKey('name')
137
            ->prototype('array')
138
            ->children();
139
140
        $connectionBuilder
141
            ->scalarNode('client_name')
142
            ->isRequired()
143
            ->info('Desired client name');
144
145
        $connectionBuilder
146
            ->scalarNode('database_name')
147
            ->isRequired()
148
            ->info('Database name');
149
    }
150
}
151