Completed
Push — master ( 740372...dc5b6e )
by Tobias
10:09
created

Configuration::configsNode()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 89
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 6.1261

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 67
cts 79
cp 0.8481
rs 8.2842
c 0
b 0
f 0
cc 6
eloc 76
nc 1
nop 1
crap 6.1261

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
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
19
/**
20
 * This is the class that validates and merges configuration from your app/config files.
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    private $container;
25
26 17
    public function __construct(ContainerBuilder $container)
27
    {
28 17
        $this->container = $container;
29 17
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 17
    public function getConfigTreeBuilder()
35
    {
36 17
        $treeBuilder = new TreeBuilder();
37 17
        $root = $treeBuilder->root('translation');
38
39 17
        $this->configsNode($root);
40 17
        $this->addAutoTranslateNode($root);
41 17
        $this->addEditInPlaceNode($root);
42 17
        $this->addWebUINode($root);
43
44 17
        $debug = $this->container->getParameter('kernel.debug');
45
        $root
46 17
            ->children()
47 17
                ->arrayNode('locales')
48 17
                    ->prototype('scalar')->end()
49 17
                ->end()
50 17
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
51 17
                ->arrayNode('symfony_profiler')
52 17
                    ->addDefaultsIfNotSet()
53 17
                    ->treatFalseLike(['enabled' => false])
54 17
                    ->treatTrueLike(['enabled' => true])
55 17
                    ->treatNullLike(['enabled' => $debug])
56 17
                    ->info('Extend the debug profiler with information about requests.')
57 17
                    ->children()
58 17
                        ->booleanNode('enabled')
59 17
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
60 17
                            ->defaultValue($debug)
61 17
                        ->end()
62 17
                        ->scalarNode('formatter')->defaultNull()->end()
63 17
                        ->integerNode('captured_body_length')
64 17
                            ->defaultValue(0)
65 17
                            ->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. Only available with the default formatter (FullHttpMessageFormatter).')
66 17
                        ->end()
67 17
                    ->end()
68 17
                    ->children()
69 17
                        ->booleanNode('allow_edit')->defaultTrue()->end()
70 17
                    ->end()
71 17
                ->end()
72 17
                ->arrayNode('auto_add_missing_translations')
73 17
                    ->canBeEnabled()
74 17
                    ->children()
75 17
                        ->scalarNode('config_name')->defaultValue('default')->end()
76 17
                    ->end()
77 17
                ->end()
78 17
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
79 17
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
80 17
            ->end();
81
82 17
        return $treeBuilder;
83
    }
84
85
    /**
86
     * @param ArrayNodeDefinition $root
87
     */
88 17
    private function configsNode(ArrayNodeDefinition $root)
89
    {
90 17
        $container = $this->container;
91 17
        $root->children()
92 17
            ->arrayNode('configs')
93 17
            ->addDefaultChildrenIfNoneSet('default')
94 17
                ->useAttributeAsKey('name')
95 17
                ->prototype('array')
96 17
                    ->fixXmlConfig('dir', 'dirs')
97 17
                    ->fixXmlConfig('excluded_dir')
98 17
                    ->fixXmlConfig('excluded_name')
99 17
                    ->fixXmlConfig('blacklist_domain')
100 17
                    ->fixXmlConfig('external_translations_dir')
101 17
                    ->fixXmlConfig('whitelist_domain')
102 17
                    ->children()
103 17
                        ->arrayNode('dirs')
104 17
                            ->info('Directories we should scan for translations')
105 17
                            ->prototype('scalar')
106 17
                                ->validate()
107
                                    ->always(function ($value) use ($container) {
108 5
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
109
110 5
                                        if ('@' === $value[0]) {
111
                                            if (false === $pos = strpos($value, '/')) {
112
                                                $bundleName = substr($value, 1);
113
                                            } else {
114
                                                $bundleName = substr($value, 1, $pos - 2);
115
                                            }
116
117
                                            $bundles = $container->getParameter('kernel.bundles');
118
                                            if (!isset($bundles[$bundleName])) {
119
                                                throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles)));
120
                                            }
121
122
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
123
                                            $value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos);
124
                                        }
125
126 5
                                        if (!is_dir($value)) {
127
                                            throw new \Exception(sprintf('The directory "%s" does not exist.', $value));
128
                                        }
129
130 5
                                        return $value;
131 17
                                    })
132 17
                                ->end()
133 17
                            ->end()
134 17
                        ->end()
135 17
                        ->arrayNode('excluded_dirs')
136 17
                            ->prototype('scalar')->end()
137 17
                        ->end()
138 17
                        ->arrayNode('excluded_names')
139 17
                            ->prototype('scalar')->end()
140 17
                        ->end()
141 17
                        ->arrayNode('external_translations_dirs')
142 17
                            ->prototype('scalar')->end()
143 17
                        ->end()
144 17
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end()
145 17
                        ->arrayNode('blacklist_domains')
146 17
                            ->prototype('scalar')->end()
147 17
                        ->end()
148 17
                        ->arrayNode('whitelist_domains')
149 17
                            ->prototype('scalar')->end()
150 17
                        ->end()
151 17
                        ->arrayNode('remote_storage')
152 17
                            ->info('Service ids with to classes that supports remote storage of translations.')
153 17
                            ->prototype('scalar')->end()
154 17
                        ->end()
155 17
                        ->arrayNode('local_storage')
156 17
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
157 17
                            ->info('Service ids with to classes that supports local storage of translations.')
158 17
                            ->prototype('scalar')->end()
159 17
                        ->end()
160 17
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
161 17
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
162 17
                        ->variableNode('local_file_storage_options')
163 17
                            ->info('Options passed to the local file storage\'s dumper.')
164 17
                            ->defaultValue([])
165 17
                            ->validate()
166 17
                                ->ifTrue(function ($value) {
167
                                    return !is_array($value);
168 17
                                })
169 17
                                ->thenInvalid('"local_file_storage_options" must be an array.')
170 17
                            ->end()
171 17
                        ->end()
172 17
                    ->end()
173 17
                ->end()
174 17
            ->end()
175 17
        ->end();
176 17
    }
177
178 17
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
179
    {
180 17
        $root->children()
181 17
            ->arrayNode('fallback_translation')
182 17
                ->canBeEnabled()
183 17
                ->children()
184 17
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
185 17
                    ->scalarNode('api_key')->defaultNull()->end()
186 17
                ->end()
187 17
            ->end()
188 17
        ->end();
189 17
    }
190
191 17
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
192
    {
193 17
        $root->children()
194 17
            ->arrayNode('edit_in_place')
195 17
                ->canBeEnabled()
196 17
                ->children()
197 17
                    ->scalarNode('config_name')->defaultValue('default')->end()
198 17
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
199 17
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
200 17
                ->end()
201 17
            ->end()
202 17
        ->end();
203 17
    }
204
205 17
    private function addWebUINode(ArrayNodeDefinition $root)
206
    {
207 17
        $root->children()
208 17
            ->arrayNode('webui')
209 17
                ->canBeEnabled()
210 17
                ->children()
211 17
                    ->booleanNode('allow_create')->defaultTrue()->end()
212 17
                    ->booleanNode('allow_delete')->defaultTrue()->end()
213 17
                ->end()
214 17
            ->end()
215 17
        ->end();
216 17
    }
217
}
218