Passed
Pull Request — master (#3)
by Vincent
08:42
created

Configuration   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Test Coverage

Coverage 96.41%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 200
c 2
b 0
f 0
dl 0
loc 304
ccs 188
cts 195
cp 0.9641
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configureConfigurationNode() 0 15 2
A getMigrationNode() 0 19 1
A getTypesNode() 0 18 1
A getCacheNode() 0 21 1
A getConnectionsNode() 0 54 1
A __construct() 0 3 1
A getConfigTreeBuilder() 0 19 1
B configureDbalDriverNode() 0 105 3
1
<?php
2
3
namespace Bdf\PrimeBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
/**
11
 * Configuration
12
 */
13
class Configuration implements ConfigurationInterface
14
{
15
    /**
16
     * @var bool
17
     */
18
    private $debug;
19
20
    /**
21
     * @param bool $debug Whether to use the debug mode
22
     */
23 6
    public function __construct($debug = false)
24
    {
25 6
        $this->debug = (bool) $debug;
26 6
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 6
    public function getConfigTreeBuilder()
32
    {
33 6
        $treeBuilder = new TreeBuilder('prime');
34 6
        $node = $treeBuilder->getRootNode();
35
        $node
36 6
            ->children()
37 6
                ->booleanNode('activerecord')->defaultFalse()->end()
38 6
                ->scalarNode('hydrators')->defaultValue('%kernel.cache_dir%/prime/hydrators/loader.php')->end()
39 6
                ->scalarNode('serializer')->info('The bdf serializer service id.')->end()
40 6
                ->scalarNode('default_connection')->defaultNull()->end()
41 6
                ->append($this->getConnectionsNode())
42 6
                ->append($this->getMigrationNode())
43 6
                ->append($this->getCacheNode())
44 6
            ->end()
45
        ;
46
47 6
        $this->configureConfigurationNode($node, true);
48
49 6
        return $treeBuilder;
50
    }
51
52
    /**
53
     * Adds the configuration node of the connection
54
     * Could be the global config or a connection config
55
     *
56
     * @param ArrayNodeDefinition $node
57
     */
58 6
    private function configureConfigurationNode(ArrayNodeDefinition $node, bool $addDefault): void
59
    {
60 6
        $parametersNode = $node->children();
61
62 6
        $loggingNode = $parametersNode->booleanNode('logging');
63 6
        $profilingNode = $parametersNode->booleanNode('profiling');
64 6
        $autoCommitNode = $parametersNode->booleanNode('auto_commit');
65
66 6
        if ($addDefault === true) {
67 6
            $loggingNode->defaultValue($this->debug);
68 6
            $profilingNode->defaultValue($this->debug);
69 6
            $autoCommitNode->defaultNull();
70
        }
71
72 6
        $parametersNode->append($this->getTypesNode());
73 6
    }
74
75
    /**
76
     * @return NodeDefinition
77
     */
78 6
    private function getConnectionsNode()
79
    {
80 6
        $root = (new TreeBuilder('connections'))->getRootNode();
81
82
        $connectionNode = $root
83 6
            ->requiresAtLeastOneElement()
0 ignored issues
show
Bug introduced by
The method requiresAtLeastOneElement() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

83
            ->/** @scrutinizer ignore-call */ requiresAtLeastOneElement()
Loading history...
84 6
            ->useAttributeAsKey('name')
85 6
            ->arrayPrototype()
86 6
            ->isRequired()
87 6
            ->beforeNormalization()
88 6
                ->ifString()
89
                ->then(static function ($v) {
90 1
                    return ['url' => $v];
91 6
                })
92 6
            ->end()
93
        ;
94
95 6
        $this->configureDbalDriverNode($connectionNode);
96 6
        $this->configureConfigurationNode($connectionNode, false);
97
98
        $connectionNode
99 6
            ->children()
100 6
                ->scalarNode('driver')->end()
101 6
                ->scalarNode('platform_service')->end()
102 6
                ->scalarNode('server_version')->end()
103 6
                ->scalarNode('driver_class')->end()
104 6
                ->scalarNode('wrapper_class')->end()
105 6
                ->arrayNode('options')
106 6
                    ->useAttributeAsKey('key')
107 6
                    ->variablePrototype()->end()
108 6
                ->end()
109 6
                ->arrayNode('default_table_options')
110 6
                    ->info("This option is used by the schema-tool and affects generated SQL. Possible keys include 'charset','collate', and 'engine'.")
111 6
                    ->useAttributeAsKey('name')
112 6
                    ->scalarPrototype()->end()
113 6
                ->end()
114 6
            ->end();
115
116 6
        $slaveNode = $connectionNode->children()->arrayNode('read');
117
118 6
        $this->configureDbalDriverNode($slaveNode);
119
120
        $shardNode = $connectionNode
121 6
            ->children()
122 6
                ->scalarNode('shard_choser')->end()
123 6
                ->scalarNode('distribution_key')->end()
124 6
                ->arrayNode('shards')
125 6
                    ->requiresAtLeastOneElement()
126 6
                    ->useAttributeAsKey('name')
127 6
                    ->arrayPrototype();
128
129 6
        $this->configureDbalDriverNode($shardNode);
130
131 6
        return $root;
132
    }
133
134
    /**
135
     * Adds config keys related to params processed by the DBAL drivers
136
     *
137
     * These keys are available for slave configurations too.
138
     */
139 6
    private function configureDbalDriverNode(ArrayNodeDefinition $node): void
140
    {
141
        $node
142 6
            ->children()
143 6
                ->scalarNode('url')->info('A URL with connection information; any parameter value parsed from this string will override explicitly set parameters')->end()
144 6
                ->scalarNode('dbname')->end()
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

144
                ->/** @scrutinizer ignore-call */ scalarNode('dbname')->end()
Loading history...
145 6
                ->scalarNode('host')->end()
146 6
                ->scalarNode('port')->end()
147 6
                ->scalarNode('user')->end()
148 6
                ->scalarNode('password')->end()
149 6
                ->scalarNode('application_name')->end()
150 6
                ->scalarNode('charset')->end()
151 6
                ->scalarNode('path')->end()
152 6
                ->booleanNode('memory')->end()
153 6
                ->scalarNode('unix_socket')->info('The unix socket to use for MySQL')->end()
154 6
                ->booleanNode('persistent')->info('True to use as persistent connection for the ibm_db2 driver')->end()
155 6
                ->scalarNode('protocol')->info('The protocol to use for the ibm_db2 driver (default to TCPIP if omitted)')->end()
156 6
                ->booleanNode('service')
157 6
                    ->info('True to use SERVICE_NAME as connection parameter instead of SID for Oracle')
158 6
                ->end()
159 6
                ->scalarNode('servicename')
160 6
                    ->info(
161
                        'Overrules dbname parameter if given and used as SERVICE_NAME or SID connection parameter ' .
162 6
                        'for Oracle depending on the service parameter.'
163
                    )
164 6
                ->end()
165 6
                ->scalarNode('sessionMode')
166 6
                    ->info('The session mode to use for the oci8 driver')
167 6
                ->end()
168 6
                ->scalarNode('server')
169 6
                    ->info('The name of a running database server to connect to for SQL Anywhere.')
170 6
                ->end()
171 6
                ->scalarNode('default_dbname')
172 6
                    ->info(
173 6
                        'Override the default database (postgres) to connect to for PostgreSQL connexion.'
174
                    )
175 6
                ->end()
176 6
                ->scalarNode('sslmode')
177 6
                    ->info(
178
                        'Determines whether or with what priority a SSL TCP/IP connection will be negotiated with ' .
179 6
                        'the server for PostgreSQL.'
180
                    )
181 6
                ->end()
182 6
                ->scalarNode('sslrootcert')
183 6
                    ->info(
184
                        'The name of a file containing SSL certificate authority (CA) certificate(s). ' .
185 6
                        'If the file exists, the server\'s certificate will be verified to be signed by one of these authorities.'
186
                    )
187 6
                ->end()
188 6
                ->scalarNode('sslcert')
189 6
                    ->info(
190 6
                        'The path to the SSL client certificate file for PostgreSQL.'
191
                    )
192 6
                ->end()
193 6
                ->scalarNode('sslkey')
194 6
                    ->info(
195 6
                        'The path to the SSL client key file for PostgreSQL.'
196
                    )
197 6
                ->end()
198 6
                ->scalarNode('sslcrl')
199 6
                    ->info(
200 6
                        'The file name of the SSL certificate revocation list for PostgreSQL.'
201
                    )
202 6
                ->end()
203 6
                ->booleanNode('pooled')->info('True to use a pooled server with the oci8/pdo_oracle driver')->end()
204 6
                ->booleanNode('MultipleActiveResultSets')->info('Configuring MultipleActiveResultSets for the pdo_sqlsrv driver')->end()
205
//                ->booleanNode('use_savepoints')->info('Use savepoints for nested transactions')->end()
206 6
                ->scalarNode('instancename')
207 6
                    ->info(
208
                        'Optional parameter, complete whether to add the INSTANCE_NAME parameter in the connection.' .
209
                        ' It is generally used to connect to an Oracle RAC server to select the name' .
210 6
                        ' of a particular instance.'
211
                    )
212 6
                ->end()
213 6
                ->scalarNode('connectstring')
214 6
                    ->info(
215
                        'Complete Easy Connect connection descriptor, see https://docs.oracle.com/database/121/NETAG/naming.htm.' .
216
                        'When using this option, you will still need to provide the user and password parameters, but the other ' .
217
                        'parameters will no longer be used. Note that when using this parameter, the getHost and getPort methods' .
218 6
                        ' from Doctrine\DBAL\Connection will no longer function as expected.'
219
                    )
220 6
                ->end()
221 6
            ->end()
222 6
            ->beforeNormalization()
223
                ->ifTrue(static function ($v) {
224 6
                    return ! isset($v['sessionMode']) && isset($v['session_mode']);
225 6
                })
226
                ->then(static function ($v) {
227
                    $v['sessionMode'] = $v['session_mode'];
228
                    unset($v['session_mode']);
229
230
                    return $v;
231 6
                })
232 6
            ->end()
233 6
            ->beforeNormalization()
234
                ->ifTrue(static function ($v) {
235 6
                    return ! isset($v['MultipleActiveResultSets']) && isset($v['multiple_active_result_sets']);
236 6
                })
237
                ->then(static function ($v) {
238
                    $v['MultipleActiveResultSets'] = $v['multiple_active_result_sets'];
239
                    unset($v['multiple_active_result_sets']);
240
241
                    return $v;
242 6
                })
243 6
            ->end();
244 6
    }
245
246
    /**
247
     * @return NodeDefinition
248
     */
249 6
    private function getTypesNode()
250
    {
251 6
        $root = (new TreeBuilder('types'))->getRootNode();
252
        $root
253 6
            ->useAttributeAsKey('name')
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

253
            ->/** @scrutinizer ignore-call */ 
254
              useAttributeAsKey('name')
Loading history...
254 6
            ->arrayPrototype()
255 6
            ->beforeNormalization()
256 6
            ->ifString()
257
            ->then(static function($v) {
258
                return ['class' => $v];
259 6
            })
260 6
            ->end()
261 6
            ->children()
262 6
                ->scalarNode('class')->isRequired()->end()
263 6
            ->end()
264
        ;
265
266 6
        return $root;
267
    }
268
269
    /**
270
     * @return NodeDefinition
271
     */
272 6
    private function getMigrationNode()
273
    {
274 6
        $root = (new TreeBuilder('migration'))->getRootNode();
275 6
        $root->children()
276 6
            ->scalarNode('path')
277 6
                ->info('The directory path where the migration file will be stored.')
278 6
                ->isRequired()
279 6
            ->end()
280 6
            ->scalarNode('connection')
281 6
                ->info('The prime connection name to use to access to the version of migrations.')
282 6
                ->defaultNull()
283 6
            ->end()
284 6
            ->scalarNode('table')
285 6
                ->info('The table name to store the version of migrations. The default name is "migrations".')
286 6
                ->defaultValue('migrations')
287 6
            ->end()
288
        ;
289
290 6
        return $root;
291
    }
292
293
    /**
294
     * @return NodeDefinition
295
     */
296 6
    private function getCacheNode()
297
    {
298 6
        $root = (new TreeBuilder('cache'))->getRootNode();
299 6
        $root->children()
300 6
            ->arrayNode('query')
301 6
                ->info('The result cache service. Should implement Bdf\Prime\Cache\CacheInterface.')
302 6
                ->children()
303 6
                    ->scalarNode('pool')->end()
304 6
                    ->scalarNode('service')->end()
305 6
                ->end()
306 6
            ->end()
307 6
            ->arrayNode('metadata')
308 6
                ->info('The metadata cache service. Should implement Psr\SimpleCache\CacheInterface.')
309 6
                ->children()
310 6
                    ->scalarNode('pool')->end()
311 6
                    ->scalarNode('service')->end()
312 6
                ->end()
313 6
            ->end()
314
        ;
315
316 6
        return $root;
317
    }
318
}
319