Completed
Pull Request — master (#14)
by Christophe
01:56
created

ConfigureTranslatorPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 35
ccs 12
cts 16
cp 0.75
rs 10
c 2
b 1
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 32 5
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
class ConfigureTranslatorPass implements CompilerPassInterface
9
{
10 1
    public function process(ContainerBuilder $container)
11
    {
12 1
        if (!$container->hasDefinition('incenteev_translation_checker.exposing_translator')) {
13
            return;
14
        }
15
16 1
        if (!$container->has('translator.default')) {
17
            return;
18
        }
19
20 1
        $translatorDef = $container->findDefinition('translator.default');
21
22
        // Symfony 3.3 added the default locale as third argument, before the loader ids and the options
23 1
        if (is_string($translatorDef->getArgument(2))) {
24 1
            $optionsArgumentIndex = 4;
25
        } else {
26
            $optionsArgumentIndex = 3;
27
        }
28
29 1
        $options = $translatorDef->getArgument($optionsArgumentIndex);
30
31 1
        if (!is_array($options)) {
32
            // Weird setup. Reset all options
33
            $options = array();
34
        }
35
36
        // use a separate cache as we have no fallback locales
37 1
        $options['cache_dir'] = '%kernel.cache_dir%/incenteev_translations';
38
39 1
        $container->findDefinition('incenteev_translation_checker.exposing_translator')
40 1
            ->replaceArgument($optionsArgumentIndex, $options);
41 1
    }
42
}
43