Completed
Push — master ( 4d50ae...84674b )
by Tobias
06:42
created

TranslationExtension::enableSymfonyProfiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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\Reference;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
use Symfony\Component\DependencyInjection\Loader;
20
use Translation\Bundle\Service\StorageService;
21
22
/**
23
 * This is the class that loads and manages your bundle configuration.
24
 *
25
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
26
 */
27
class TranslationExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 1
        $configuration = new Configuration($container);
35 1
        $config = $this->processConfiguration($configuration, $configs);
36 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
37
38 1
        $loader->load('services.yml');
39 1
        $loader->load('extractors.yml');
40
41 1
        $container->setParameter('php_translation.locales', $config['locales']);
42 1
        $container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale'));
43
44 1
        if ($config['webui']['enabled']) {
45 1
            $this->enableWebUi($container, $config);
0 ignored issues
show
Unused Code introduced by
The call to the method Translation\Bundle\Depen...xtension::enableWebUi() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
46 1
        }
47
48 1
        if ($config['symfony_profiler']['enabled']) {
49
            $loader->load('symfony_profiler.yml');
50
            $this->enableSymfonyProfiler($container, $config);
51
        }
52
53 1
        if ($config['fallback_translation']['enabled']) {
54
            $loader->load('auto_translation.yml');
55
            $this->enableFallbackAutoTranslator($container, $config);
56
        }
57
58 1
        $first = null;
59 1
        foreach ($config['configs'] as $name => &$c) {
60
            if ($first === null || $name === 'default') {
61
                $first = $name;
62
            }
63
            if (empty($c['project_root'])) {
64
                $c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
65
            }
66
67
            $container->register('php_translation.storage.'.$name, StorageService::class);
68
69
            // Register a file storage
70
            $def = new DefinitionDecorator('php_translation.single_storage.file.abstract');
71
            $def->replaceArgument(2, $c['output_dir'])
72
                ->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
73
            $container->setDefinition('php_translation.single_storage.file.'.$name, $def);
74
        }
75
76
        if ($first !== null) {
77
            // Create some aliases for the default storage
78
            $container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
79
            $container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
80
        }
81
82
        $container->getDefinition('php_translation.configuration_manager')
83
            ->replaceArgument(0, $config['configs']);
84
    }
85
86
    private function enableWebUi(ContainerBuilder $container, $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
    }
89
90
    private function enableSymfonyProfiler(ContainerBuilder $container, $config)
91
    {
92
        $container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
93
    }
94
95
    private function enableFallbackAutoTranslator(ContainerBuilder $container, $config)
96
    {
97
        $externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
98
        $externalTranslatorDef = $container->getDefinition($externalTranslatorId);
99
        $externalTranslatorDef->addTag('php_translation.external_translator');
100
        $externalTranslatorDef->addArgument(new Reference($config['http_client']));
101
        $externalTranslatorDef->addArgument(new Reference($config['message_factory']));
102
103
        $container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']);
104
    }
105
106
    public function getAlias()
107
    {
108
        return 'translation';
109
    }
110
}
111