Completed
Push — master ( 1bb946...de9431 )
by Tobias
26:34
created

Configuration   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 180
ccs 128
cts 144
cp 0.8889
rs 10
c 0
b 0
f 0

6 Methods

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