Completed
Push — master ( 4e8bc9...f46df8 )
by Tobias
13:01 queued 05:03
created

Configuration::configsNode()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 90
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 68
CRAP Score 6.1215

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 68
cts 80
cp 0.85
rs 8.2713
c 0
b 0
f 0
cc 6
eloc 77
nc 1
nop 1
crap 6.1215

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 21
    public function __construct(ContainerBuilder $container)
27
    {
28 21
        $this->container = $container;
29 21
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 21
    public function getConfigTreeBuilder()
35
    {
36 21
        $treeBuilder = new TreeBuilder();
37 21
        $root = $treeBuilder->root('translation');
38
39 21
        $this->configsNode($root);
40 21
        $this->addAutoTranslateNode($root);
41 21
        $this->addEditInPlaceNode($root);
42 21
        $this->addWebUINode($root);
43
44 21
        $debug = $this->container->getParameter('kernel.debug');
45
        $root
46 21
            ->children()
47 21
                ->arrayNode('locales')
48 21
                    ->prototype('scalar')->end()
49 21
                ->end()
50 21
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
51 21
                ->arrayNode('symfony_profiler')
52 21
                    ->addDefaultsIfNotSet()
53 21
                    ->treatFalseLike(['enabled' => false])
54 21
                    ->treatTrueLike(['enabled' => true])
55 21
                    ->treatNullLike(['enabled' => $debug])
56 21
                    ->info('Extend the debug profiler with information about requests.')
57 21
                    ->children()
58 21
                        ->booleanNode('enabled')
59 21
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
60 21
                            ->defaultValue($debug)
61 21
                        ->end()
62 21
                        ->scalarNode('formatter')->defaultNull()->end()
63 21
                        ->integerNode('captured_body_length')
64 21
                            ->defaultValue(0)
65 21
                            ->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 21
                        ->end()
67 21
                    ->end()
68 21
                    ->children()
69 21
                        ->booleanNode('allow_edit')->defaultTrue()->end()
70 21
                    ->end()
71 21
                ->end()
72 21
                ->arrayNode('auto_add_missing_translations')
73 21
                    ->canBeEnabled()
74 21
                    ->children()
75 21
                        ->scalarNode('config_name')->defaultValue('default')->end()
76 21
                    ->end()
77 21
                ->end()
78 21
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
79 21
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
80 21
            ->end();
81
82 21
        return $treeBuilder;
83
    }
84
85
    /**
86
     * @param ArrayNodeDefinition $root
87
     */
88 21
    private function configsNode(ArrayNodeDefinition $root)
89
    {
90 21
        $container = $this->container;
91 21
        $root->children()
92 21
            ->arrayNode('configs')
93 21
            ->addDefaultChildrenIfNoneSet('default')
94 21
                ->useAttributeAsKey('name')
95 21
                ->prototype('array')
96 21
                    ->fixXmlConfig('dir', 'dirs')
97 21
                    ->fixXmlConfig('excluded_dir')
98 21
                    ->fixXmlConfig('excluded_name')
99 21
                    ->fixXmlConfig('blacklist_domain')
100 21
                    ->fixXmlConfig('external_translations_dir')
101 21
                    ->fixXmlConfig('whitelist_domain')
102 21
                    ->children()
103 21
                        ->arrayNode('dirs')
104 21
                            ->info('Directories we should scan for translations')
105 21
                            ->prototype('scalar')
106 21
                                ->validate()
107
                                    ->always(function ($value) use ($container) {
108 9
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
109
110 9
                                        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 9
                                        if (!is_dir($value)) {
127
                                            throw new \Exception(sprintf('The directory "%s" does not exist.', $value));
128
                                        }
129
130 9
                                        return $value;
131 21
                                    })
132 21
                                ->end()
133 21
                            ->end()
134 21
                        ->end()
135 21
                        ->arrayNode('excluded_dirs')
136 21
                            ->prototype('scalar')->end()
137 21
                        ->end()
138 21
                        ->arrayNode('excluded_names')
139 21
                            ->prototype('scalar')->end()
140 21
                        ->end()
141 21
                        ->arrayNode('external_translations_dirs')
142 21
                            ->prototype('scalar')->end()
143 21
                        ->end()
144 21
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end()
145 21
                        ->arrayNode('blacklist_domains')
146 21
                            ->prototype('scalar')->end()
147 21
                        ->end()
148 21
                        ->arrayNode('whitelist_domains')
149 21
                            ->prototype('scalar')->end()
150 21
                        ->end()
151 21
                        ->arrayNode('remote_storage')
152 21
                            ->info('Service ids with to classes that supports remote storage of translations.')
153 21
                            ->prototype('scalar')->end()
154 21
                        ->end()
155 21
                        ->arrayNode('local_storage')
156 21
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
157 21
                            ->info('Service ids with to classes that supports local storage of translations.')
158 21
                            ->prototype('scalar')->end()
159 21
                        ->end()
160 21
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
161 21
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
162 21
                        ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
163 21
                        ->variableNode('local_file_storage_options')
164 21
                            ->info('Options passed to the local file storage\'s dumper.')
165 21
                            ->defaultValue([])
166 21
                            ->validate()
167 21
                                ->ifTrue(function ($value) {
168
                                    return !is_array($value);
169 21
                                })
170 21
                                ->thenInvalid('"local_file_storage_options" must be an array.')
171 21
                            ->end()
172 21
                        ->end()
173 21
                    ->end()
174 21
                ->end()
175 21
            ->end()
176 21
        ->end();
177 21
    }
178
179 21
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
180
    {
181 21
        $root->children()
182 21
            ->arrayNode('fallback_translation')
183 21
                ->canBeEnabled()
184 21
                ->children()
185 21
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
186 21
                    ->scalarNode('api_key')->defaultNull()->end()
187 21
                ->end()
188 21
            ->end()
189 21
        ->end();
190 21
    }
191
192 21
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
193
    {
194 21
        $root->children()
195 21
            ->arrayNode('edit_in_place')
196 21
                ->canBeEnabled()
197 21
                ->children()
198 21
                    ->scalarNode('config_name')->defaultValue('default')->end()
199 21
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
200 21
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
201 21
                ->end()
202 21
            ->end()
203 21
        ->end();
204 21
    }
205
206 21
    private function addWebUINode(ArrayNodeDefinition $root)
207
    {
208 21
        $root->children()
209 21
            ->arrayNode('webui')
210 21
                ->canBeEnabled()
211 21
                ->children()
212 21
                    ->booleanNode('allow_create')->defaultTrue()->end()
213 21
                    ->booleanNode('allow_delete')->defaultTrue()->end()
214 21
                    ->scalarNode('file_base_path')->defaultNull()->info('Base path for SourceLocation\'s. Defaults to "%kernel.project_dir%".')->end()
215 21
                ->end()
216 21
            ->end()
217 21
        ->end();
218 21
    }
219
}
220