Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 123
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 102
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 8.1463
c 0
b 0
f 0
ccs 102
cts 102
cp 1
cc 5
eloc 99
nc 1
nop 0
crap 5

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 Asm\TranslationLoaderBundle\DependencyInjection
4
 */
5
namespace Asm\TranslationLoaderBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
/**
11
 * This is the class that validates and merges configuration from your app/config files
12
 *
13
 * @package Asm\TranslationLoaderBundle\DependencyInjection
14
 * @author marc aschmann <[email protected]>
15
 * @uses Symfony\Component\Config\Definition\Builder\TreeBuilder
16
 * @uses Symfony\Component\Config\Definition\ConfigurationInterface
17
 */
18
class Configuration implements ConfigurationInterface
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 21
    public function getConfigTreeBuilder()
24
    {
25
        /** @var \Symfony\Component\Config\Definition\Builder\TreeBuilder $treeBuilder */
26 21
        $treeBuilder = new TreeBuilder();
27 21
        $rootNode = $treeBuilder->root('asm_translation_loader');
28
29 21
        $supportedDrivers = array('orm');
30
31
        $rootNode
32 21
            ->fixXmlConfig('resource')
33 21
            ->fixXmlConfig('loader')
34 21
            ->children()
35 21
                ->arrayNode('resources')
36 21
                    ->useAttributeAsKey('locale')
37 21
                    ->prototype('array')
38 21
                        ->treatNullLike(array(null))
39 21
                        ->beforeNormalization()
40 21
                            ->ifTrue(
41
                                function ($v) {
42 6
                                    return is_array($v) && count($v) == 0;
43
                                }
44 21
                            )
45 21
                            ->then(
46
                                function () {
47 1
                                    return array(null);
48
                                }
49 21
                            )
50 21
                        ->end()
51 21
                        ->beforeNormalization()
52 21
                            ->ifTrue(
53
                                function ($v) {
54 6
                                    return is_array($v) && isset($v['domain']);
55
                                }
56 21
                            )
57 21
                            ->then(
58
                                function ($v) {
59 2
                                    return $v['domain'];
60
                                }
61 21
                            )
62 21
                        ->end()
63 21
                        ->beforeNormalization()
64 21
                            ->ifString()
65 21
                                ->then(
66
                                    function ($v) {
67 2
                                        return array($v);
68
                                    }
69 21
                                )
70 21
                        ->end()
71 21
                        ->prototype('scalar')->end()
72 21
                    ->end()
73 21
                ->end()
74 21
                ->scalarNode('driver')
75 21
                    ->validate()
76 21
                        ->ifNotInArray($supportedDrivers)
77 21
                        ->thenInvalid(
78 21
                            'The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers)
79 21
                        )
80 21
                    ->end()
81 21
                    ->defaultValue('orm')
82 21
                    ->cannotBeEmpty()
83 21
                ->end()
84 21
                ->arrayNode('loaders')
85 21
                    ->beforeNormalization()
86 21
                        ->ifNull()
87 21
                            ->then(
88
                                function () {
89 1
                                    return array();
90
                                }
91 21
                            )
92 21
                    ->end()
93 21
                    ->beforeNormalization()
94 21
                        ->always(function ($values) {
95 7
                            foreach ($values as $key => $value) {
96 6
                                if (is_array($value)) {
97 2
                                    $values[$value['extension']] = $value['value'];
98 2
                                    unset($values[$key]);
99 2
                                }
100 7
                            }
101
102 7
                            return array_merge(array(
103 7
                                'xlf' => 'translation.loader.xliff',
104 7
                                'yaml' => 'translation.loader.yml',
105 7
                            ), $values);
106 21
                        })
107 21
                    ->end()
108 21
                    ->useAttributeAsKey('extension')
109 21
                    ->prototype('scalar')->end()
110 21
                ->end()
111 21
                ->arrayNode('database')
112 21
                    ->children()
113 21
                        ->scalarNode('entity_manager')
114 21
                            ->defaultValue('default')
115 21
                            ->info('Optional entity manager for separate translations handling.')
116 21
                        ->end()
117 21
                    ->end()
118 21
                ->end()
119 21
                ->arrayNode('history')
120 21
                    ->canBeEnabled()
121 21
                    ->children()
122 21
                        ->booleanNode('enabled')
123 21
                            ->defaultFalse()
124 21
                            ->info(
125
                                'Enables historytracking for translation changes. Uses user id from registered users as reference'
126 21
                            )
127 21
                        ->end()
128 21
                    ->end()
129 21
                ->end()
130 21
                ->scalarNode('translation_manager')
131 21
                    ->defaultValue('Asm\TranslationLoaderBundle\Doctrine\TranslationManager')
132 21
                ->end()
133 21
                ->scalarNode('translation_history_manager')
134 21
                    ->defaultValue('Asm\TranslationLoaderBundle\Doctrine\TranslationHistoryManager')
135 21
                ->end()
136 21
                ->scalarNode('translation_class')
137 21
                    ->defaultValue('Asm\TranslationLoaderBundle\Entity\Translation')
138 21
                ->end()
139 21
                ->scalarNode('translation_history_class')
140 21
                    ->defaultValue('Asm\TranslationLoaderBundle\Entity\TranslationHistory')
141 21
                ->end()
142 21
            ->end();
143
144 21
        return $treeBuilder;
145
    }
146
}
147