Completed
Push — master ( c29061...fba80d )
by Tobias
05:40
created

Configuration::configsNode()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 78
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 6.3698

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 54
cts 69
cp 0.7826
rs 8.4316
c 0
b 0
f 0
cc 6
eloc 66
nc 1
nop 1
crap 6.3698

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 1
    public function __construct(ContainerBuilder $container)
27
    {
28 1
        $this->container = $container;
29 1
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function getConfigTreeBuilder()
35
    {
36 1
        $treeBuilder = new TreeBuilder();
37 1
        $root = $treeBuilder->root('translation');
38
39 1
        $this->configsNode($root);
40 1
        $this->addAutoTranslateNode($root);
41 1
        $this->addTranslationServiceNode($root);
42
43
        $root
44 1
            ->children()
45 1
                ->arrayNode('locales')
46 1
                    ->prototype('scalar')->end()
47 1
                ->end()
48 1
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
49 1
                ->arrayNode('symfony_profiler')
50 1
                    ->canBeEnabled()
51 1
                    ->children()
52 1
                        ->booleanNode('allow_edit')->defaultTrue()->end()
53 1
                    ->end()
54 1
                ->end()
55 1
                ->arrayNode('webui')
56 1
                    ->canBeEnabled()
57 1
                    ->children()
58 1
                        ->booleanNode('allow_add')->defaultTrue()->end()
59 1
                    ->end()
60 1
                ->end()
61 1
                ->arrayNode('edit_in_place')
62 1
                    ->canBeEnabled()
63 1
                    ->children()
64 1
                        ->scalarNode('config_name')->defaultValue('default')->end()
65 1
                        ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
66 1
                    ->end()
67 1
                ->end()
68 1
                ->arrayNode('auto_add_missing_translations')
69 1
                    ->canBeEnabled()
70 1
                    ->children()
71 1
                        ->scalarNode('config_name')->defaultValue('default')->end()
72 1
                    ->end()
73 1
                ->end()
74 1
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
75 1
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
76 1
            ->end();
77
78 1
        return $treeBuilder;
79
    }
80
81
    /**
82
     * @param ArrayNodeDefinition $root
83
     */
84 1
    private function configsNode(ArrayNodeDefinition $root)
85
    {
86 1
        $container = $this->container;
87 1
        $root->children()
88 1
            ->arrayNode('configs')
89 1
                ->useAttributeAsKey('name')
90 1
                ->prototype('array')
91 1
                    ->fixXmlConfig('dir', 'dirs')
92 1
                    ->fixXmlConfig('excluded_dir')
93 1
                    ->fixXmlConfig('excluded_name')
94 1
                    ->fixXmlConfig('blacklist_domain')
95 1
                    ->fixXmlConfig('external_translations_dir')
96 1
                    ->fixXmlConfig('whitelist_domain')
97 1
                    ->children()
98 1
                        ->arrayNode('dirs')
99 1
                            ->info('Directories we should scan for translation files')
100 1
                            ->requiresAtLeastOneElement()
101 1
                            ->prototype('scalar')
102 1
                                ->validate()
103 1
                                    ->always(function ($value) use ($container) {
104
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
105
106
                                        if ('@' === $value[0]) {
107
                                            if (false === $pos = strpos($value, '/')) {
108
                                                $bundleName = substr($value, 1);
109
                                            } else {
110
                                                $bundleName = substr($value, 1, $pos - 2);
111
                                            }
112
113
                                            $bundles = $container->getParameter('kernel.bundles');
114
                                            if (!isset($bundles[$bundleName])) {
115
                                                throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles)));
116
                                            }
117
118
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
119
                                            $value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos);
120
                                        }
121
122
                                        if (!is_dir($value)) {
123
                                            throw new \Exception(sprintf('The directory "%s" does not exist.', $value));
124
                                        }
125
126
                                        return $value;
127 1
                                    })
128 1
                                ->end()
129 1
                            ->end()
130 1
                        ->end()
131 1
                        ->arrayNode('excluded_dirs')
132 1
                            ->prototype('scalar')->end()
133 1
                        ->end()
134 1
                        ->arrayNode('excluded_names')
135 1
                            ->prototype('scalar')->end()
136 1
                        ->end()
137 1
                        ->arrayNode('external_translations_dirs')
138 1
                            ->prototype('scalar')->end()
139 1
                        ->end()
140 1
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end()
141 1
                        ->arrayNode('blacklist_domains')
142 1
                            ->prototype('scalar')->end()
143 1
                        ->end()
144 1
                        ->arrayNode('whitelist_domains')
145 1
                            ->prototype('scalar')->end()
146 1
                        ->end()
147 1
                        ->arrayNode('remote_storage')
148 1
                            ->info('Service ids with to classes that supports remote storage of translations.')
149 1
                            ->prototype('scalar')->end()
150 1
                        ->end()
151 1
                        ->arrayNode('local_storage')
152 1
                            ->info('Service ids with to classes that supports local storage of translations.')
153 1
                            ->prototype('scalar')->end()
154 1
                        ->end()
155 1
                        ->scalarNode('output_dir')->isRequired()->cannotBeEmpty()->end()
156 1
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
157 1
                    ->end()
158 1
                ->end()
159 1
            ->end()
160 1
        ->end();
161 1
    }
162
163 1
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
164
    {
165 1
        $root->children()
166 1
            ->arrayNode('fallback_translation')
167 1
                ->canBeEnabled()
168 1
                ->children()
169 1
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
170 1
                    ->scalarNode('api_key')->defaultNull()->end()
171 1
                ->end()
172 1
            ->end()
173 1
        ->end();
174 1
    }
175
176 1
    private function addTranslationServiceNode(ArrayNodeDefinition $root)
177
    {
178
        $root
179 1
            ->children()
180 1
                ->enumNode('storage')
181 1
                    ->info('Where translations are stored.')
182 1
                    ->values(['blackhole', 'filesystem', 'loco'])
183 1
                    ->defaultValue('filesystem')
184 1
                ->end()
185 1
            ->end();
186 1
    }
187
}
188