Completed
Push — master ( ffca13...6343e7 )
by Tobias
38:45 queued 28:47
created

Configuration::addAutoTranslateNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 8
    public function __construct(ContainerBuilder $container)
27
    {
28 8
        $this->container = $container;
29 8
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 8
    public function getConfigTreeBuilder()
35
    {
36 8
        $treeBuilder = new TreeBuilder('translation');
37
        // Keep compatibility with symfony/config < 4.2
38 8
        if (!method_exists($treeBuilder, 'getRootNode')) {
39
            $root = $treeBuilder->root('translation');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
        } else {
41 8
            $root = $treeBuilder->getRootNode();
42
        }
43
44 8
        $this->configsNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
45 8
        $this->addAutoTranslateNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46 8
        $this->addEditInPlaceNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
47 8
        $this->addWebUINode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
49 8
        $debug = $this->container->getParameter('kernel.debug');
50
        $root
51 8
            ->children()
52 8
                ->arrayNode('locales')
53 8
                    ->prototype('scalar')->end()
54 8
                ->end()
55 8
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
56 8
                ->arrayNode('symfony_profiler')
57 8
                    ->addDefaultsIfNotSet()
58 8
                    ->treatFalseLike(['enabled' => false])
59 8
                    ->treatTrueLike(['enabled' => true])
60 8
                    ->treatNullLike(['enabled' => $debug])
61 8
                    ->info('Extend the debug profiler with information about requests.')
62 8
                    ->children()
63 8
                        ->booleanNode('enabled')
64 8
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
65 8
                            ->defaultValue($debug)
66 8
                        ->end()
67 8
                        ->scalarNode('formatter')->defaultNull()->end()
68 8
                        ->integerNode('captured_body_length')
69 8
                            ->defaultValue(0)
70 8
                            ->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).')
71 8
                        ->end()
72 8
                    ->end()
73 8
                    ->children()
74 8
                        ->booleanNode('allow_edit')->defaultTrue()->end()
75 8
                    ->end()
76 8
                ->end()
77 8
                ->arrayNode('auto_add_missing_translations')
78 8
                    ->canBeEnabled()
79 8
                    ->children()
80 8
                        ->scalarNode('config_name')->defaultValue('default')->end()
81 8
                    ->end()
82 8
                ->end()
83 8
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
84 8
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
85 8
            ->end();
86
87 8
        return $treeBuilder;
88
    }
89
90
    /**
91
     * @param ArrayNodeDefinition $root
92
     */
93 8
    private function configsNode(ArrayNodeDefinition $root)
94
    {
95 8
        $container = $this->container;
96 8
        $root->children()
97 8
            ->arrayNode('configs')
98 8
            ->addDefaultChildrenIfNoneSet('default')
99 8
                ->useAttributeAsKey('name')
100 8
                ->prototype('array')
101 8
                    ->fixXmlConfig('dir', 'dirs')
102 8
                    ->fixXmlConfig('excluded_dir')
103 8
                    ->fixXmlConfig('excluded_name')
104 8
                    ->fixXmlConfig('blacklist_domain')
105 8
                    ->fixXmlConfig('external_translations_dir')
106 8
                    ->fixXmlConfig('whitelist_domain')
107 8
                    ->children()
108 8
                        ->arrayNode('dirs')
109 8
                            ->info('Directories we should scan for translations')
110 8
                            ->prototype('scalar')
111 8
                                ->validate()
112 8
                                    ->always(function ($value) use ($container) {
113
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
114
115
                                        if ('@' === $value[0]) {
116 View Code Duplication
                                            if (false === $pos = strpos($value, '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
                                                $bundleName = substr($value, 1);
118
                                            } else {
119
                                                $bundleName = substr($value, 1, $pos - 2);
120
                                            }
121
122
                                            $bundles = $container->getParameter('kernel.bundles');
123
                                            if (!isset($bundles[$bundleName])) {
124
                                                throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles)));
125
                                            }
126
127
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
128
                                            $value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos);
129
                                        }
130
131
                                        if (!is_dir($value)) {
132
                                            throw new \Exception(sprintf('The directory "%s" does not exist.', $value));
133
                                        }
134
135
                                        return $value;
136 8
                                    })
137 8
                                ->end()
138 8
                            ->end()
139 8
                        ->end()
140 8
                        ->arrayNode('excluded_dirs')
141 8
                            ->prototype('scalar')->end()
142 8
                        ->end()
143 8
                        ->arrayNode('excluded_names')
144 8
                            ->prototype('scalar')->end()
145 8
                        ->end()
146 8
                        ->arrayNode('external_translations_dirs')
147 8
                            ->prototype('scalar')->end()
148 8
                        ->end()
149 8
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf', 'po'])->defaultValue('xlf')->end()
150 8
                        ->arrayNode('blacklist_domains')
151 8
                            ->prototype('scalar')->end()
152 8
                        ->end()
153 8
                        ->arrayNode('whitelist_domains')
154 8
                            ->prototype('scalar')->end()
155 8
                        ->end()
156 8
                        ->arrayNode('remote_storage')
157 8
                            ->info('Service ids with to classes that supports remote storage of translations.')
158 8
                            ->prototype('scalar')->end()
159 8
                        ->end()
160 8
                        ->arrayNode('local_storage')
161 8
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
162 8
                            ->info('Service ids with to classes that supports local storage of translations.')
163 8
                            ->prototype('scalar')->end()
164 8
                        ->end()
165 8
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
166 8
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
167 8
                        ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
168 8
                        ->variableNode('local_file_storage_options')
169 8
                            ->info('Options passed to the local file storage\'s dumper.')
170 8
                            ->defaultValue([])
171 8
                            ->validate()
172 8
                                ->ifTrue(function ($value) {
173
                                    return !is_array($value);
174 8
                                })
175 8
                                ->thenInvalid('"local_file_storage_options" must be an array.')
176 8
                            ->end()
177 8
                        ->end()
178 8
                    ->end()
179 8
                ->end()
180 8
            ->end()
181 8
        ->end();
182 8
    }
183
184 8
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
185
    {
186 8
        $root->children()
187 8
            ->arrayNode('fallback_translation')
188 8
                ->canBeEnabled()
189 8
                ->children()
190 8
                    ->enumNode('service')->values(['google', 'yandex'])->defaultValue('google')->end()
191 8
                    ->scalarNode('api_key')->defaultNull()->end()
192 8
                ->end()
193 8
            ->end()
194 8
        ->end();
195 8
    }
196
197 8
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
198
    {
199 8
        $root->children()
200 8
            ->arrayNode('edit_in_place')
201 8
                ->canBeEnabled()
202 8
                ->children()
203 8
                    ->scalarNode('config_name')->defaultValue('default')->end()
204 8
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
205 8
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
206 8
                ->end()
207 8
            ->end()
208 8
        ->end();
209 8
    }
210
211 8
    private function addWebUINode(ArrayNodeDefinition $root)
212
    {
213 8
        $root->children()
214 8
            ->arrayNode('webui')
215 8
                ->canBeEnabled()
216 8
                ->children()
217 8
                    ->booleanNode('allow_create')->defaultTrue()->end()
218 8
                    ->booleanNode('allow_delete')->defaultTrue()->end()
219 8
                    ->scalarNode('file_base_path')->defaultNull()->info('Base path for SourceLocation\'s. Defaults to "%kernel.project_dir%".')->end()
220 8
                ->end()
221 8
            ->end()
222 8
        ->end();
223 8
    }
224
}
225