Completed
Push — master ( 0215a9...dbc4c0 )
by Christophe
01:38
created

ConfigureTranslatorPass::process()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.3906

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 32
ccs 12
cts 16
cp 0.75
rs 8.439
c 2
b 1
f 0
cc 5
eloc 16
nc 6
nop 1
crap 5.3906
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