Completed
Push — master ( e9f87f...f4d1f5 )
by Tobias
23:44 queued 12:01
created

TranslationExtension::handleConfigNode()   C

Complexity

Conditions 10
Paths 75

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 10.1953

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 35
cts 40
cp 0.875
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 32
nc 75
nop 2
crap 10.1953

How to fix   Long Method    Complexity   

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\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\DependencyInjection\Loader;
21
use Symfony\Component\HttpKernel\Kernel;
22
use Translation\Bundle\Model\Configuration as ConfigurationModel;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 *
27
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
28
 */
29
class TranslationExtension extends Extension
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 12
    public function load(array $configs, ContainerBuilder $container)
35
    {
36 12
        $configuration = new Configuration($container);
37 12
        $config = $this->processConfiguration($configuration, $configs);
38 12
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39
40 12
        $loader->load('services.yml');
41 12
        $loader->load('extractors.yml');
42
43
        // Add major version to extractor
44 12
        $container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices')
45 12
            ->addMethodCall('setSymfonyMajorVersion', [Kernel::MAJOR_VERSION]);
46
47 12
        $container->setParameter('php_translation.locales', $config['locales']);
48 12
        $container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale'));
49 12
        $this->handleConfigNode($container, $config);
50
51 12
        if ($config['webui']['enabled']) {
52 5
            $this->enableWebUi($container, $config);
53 5
        } else {
54 7
            $container->setParameter('php_translation.webui.enabled', false);
55
        }
56
57 12
        if ($config['symfony_profiler']['enabled']) {
58 12
            $loader->load('symfony_profiler.yml');
59 12
            $this->enableSymfonyProfiler($container, $config);
60 12
        }
61
62 12
        if ($config['edit_in_place']['enabled']) {
63 5
            $loader->load('edit_in_place.yml');
64 5
            $this->enableEditInPlace($container, $config);
65 5
        }
66
67 12
        if ($config['auto_add_missing_translations']['enabled']) {
68 1
            $loader->load('auto_add.yml');
69 1
            $container->getDefinition('php_translator.auto_adder')
70 1
                ->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name']));
71 1
        }
72
73 12
        if ($config['fallback_translation']['enabled']) {
74 1
            $loader->load('auto_translation.yml');
75 1
            $this->enableFallbackAutoTranslator($container, $config);
76 1
        }
77 12
    }
78
79
    /**
80
     * Handle the config node to prepare the config manager.
81
     *
82
     * @param ContainerBuilder $container
83
     * @param array            $config
84
     */
85 12
    private function handleConfigNode(ContainerBuilder $container, array $config)
86
    {
87 12
        $configurationManager = $container->getDefinition('php_translation.configuration_manager');
88
        // $first will be the "default" configuration.
89 12
        $first = null;
90 12
        foreach ($config['configs'] as $name => &$c) {
91 12
            if ($first === null || $name === 'default') {
92 12
                $first = $name;
93 12
            }
94 12
            if (empty($c['project_root'])) {
95
                // Add a project root of none is set.
96 12
                $c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
97 12
            }
98 12
            $c['name'] = $name;
99 12
            $c['locales'] = $config['locales'];
100 12
            $configurationServiceId = 'php_translation.configuration.'.$name;
101 12
            $configDef = $container->register($configurationServiceId, ConfigurationModel::class);
102 12
            $configDef->setPublic(false)->addArgument($c);
103 12
            $configurationManager->addMethodCall('addConfiguration', [$name, new Reference($configurationServiceId)]);
104
105
            /*
106
             * Configure storage chain service
107
             */
108 12
            $storageDefinition = new DefinitionDecorator('php_translation.storage.abstract');
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
109 12
            $storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
110 12
            $container->setDefinition('php_translation.storage.'.$name, $storageDefinition);
111
112
            // Add storages
113 12
            foreach ($c['remote_storage'] as $serviceId) {
114
                $storageDefinition->addMethodCall('addRemoteStorage', [new Reference($serviceId)]);
115 12
            }
116
117 12
            foreach ($c['local_storage'] as $serviceId) {
118 12
                if ($serviceId !== 'php_translation.local_file_storage.abstract') {
119
                    $storageDefinition->addMethodCall('addLocalStorage', [new Reference($serviceId)]);
120
121
                    continue;
122
                }
123
124 12
                $def = new DefinitionDecorator($serviceId);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
125 12
                $def->replaceArgument(2, [$c['output_dir']])
126 12
                    ->replaceArgument(3, [$c['local_file_storage_options']])
127 12
                    ->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
128 12
                $container->setDefinition('php_translation.single_storage.file.'.$name, $def);
129 12
            }
130 12
        }
131
132 12
        if ($first !== null) {
133
            // Create some aliases for the default storage
134 12
            $container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
135 12
            if ($first !== 'default') {
136
                $container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
137
            }
138 12
        }
139 12
    }
140
141
    /**
142
     * Handle config for WebUI.
143
     *
144
     * @param ContainerBuilder $container
145
     * @param array            $config
146
     */
147 5
    private function enableWebUi(ContainerBuilder $container, array $config)
148
    {
149 5
        $container->setParameter('php_translation.webui.enabled', true);
150 5
        $container->setParameter('php_translation.webui.allow_create', $config['webui']['allow_create']);
151 5
        $container->setParameter('php_translation.webui.allow_delete', $config['webui']['allow_delete']);
152 5
    }
153
154
    /**
155
     * Handle config for EditInPlace.
156
     *
157
     * @param ContainerBuilder $container
158
     * @param array            $config
159
     */
160 5
    private function enableEditInPlace(ContainerBuilder $container, array $config)
161
    {
162 5
        $name = $config['edit_in_place']['config_name'];
163
164 5
        if ($name !== 'default' && !isset($config['configs'][$name])) {
165
            throw new InvalidArgumentException(sprintf('There is no config named "%s".', $name));
166
        }
167
168 5
        $activatorRef = new Reference($config['edit_in_place']['activator']);
169
170 5
        $def = $container->getDefinition('php_translation.edit_in_place.response_listener');
171 5
        $def->replaceArgument(0, $activatorRef);
172 5
        $def->replaceArgument(3, $name);
173 5
        $def->replaceArgument(4, $config['edit_in_place']['show_untranslatable']);
174
175 5
        $def = $container->getDefinition('php_translator.edit_in_place.xtrans_html_translator');
176 5
        $def->replaceArgument(1, $activatorRef);
177 5
    }
178
179
    /**
180
     * Handle config for Symfony Profiler.
181
     *
182
     * @param ContainerBuilder $container
183
     * @param array            $config
184
     */
185 12
    private function enableSymfonyProfiler(ContainerBuilder $container, array $config)
186
    {
187 12
        $container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
188 12
    }
189
190
    /**
191
     * Handle config for fallback auto translate.
192
     *
193
     * @param ContainerBuilder $container
194
     * @param array            $config
195
     */
196 1
    private function enableFallbackAutoTranslator(ContainerBuilder $container, array $config)
197
    {
198 1
        $externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
199 1
        $externalTranslatorDef = $container->getDefinition($externalTranslatorId);
200 1
        $externalTranslatorDef->addTag('php_translation.external_translator');
201 1
        $externalTranslatorDef->addArgument(new Reference($config['http_client']));
202 1
        $externalTranslatorDef->addArgument(new Reference($config['message_factory']));
203
204 1
        $container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']);
205 1
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 12
    public function getAlias()
211
    {
212 12
        return 'translation';
213
    }
214
}
215