Completed
Push — master ( 7b0e41...fcb8a8 )
by Christophe
19s queued 11s
created

loadInternal()   B

Complexity

Conditions 8
Paths 52

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 1
b 0
f 0
nc 52
nop 2
dl 0
loc 48
rs 8.4444
ccs 28
cts 28
cp 1
crap 8
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\DependencyInjection;
4
5
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
6
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
class IncenteevTranslationCheckerExtension extends ConfigurableExtension
11
{
12 8
    public function loadInternal(array $config, ContainerBuilder $container)
13
    {
14 8
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
15 8
        $loader->load('services.xml');
16
17
        // supports autoconfiguration of extractors in Symfony 3.3+
18 8
        if (method_exists($container, 'registerForAutoconfiguration')) {
19 8
            $container->registerForAutoconfiguration('Incenteev\TranslationCheckerBundle\Translator\Extractor\ExtractorInterface')
20 8
                ->addTag('incenteev_translation_checker.extractor');
21
        }
22
23 8
        $dirs = array();
24 8
        $overridePathPatterns = array();
25
26 8
        if ($container->hasParameter('kernel.root_dir')) {
27 3
            $overridePathPatterns[] = '%%kernel.root_dir%%/Resources/%s/views';
28
        }
29 8
        $overridePathPatterns[] = '%%kernel.project_dir%%/templates/bundles/%s';
30
31 8
        $registeredBundles = $container->getParameter('kernel.bundles');
32
33 8
        foreach ($config['extraction']['bundles'] as $bundle) {
34 3
            if (!isset($registeredBundles[$bundle])) {
35 1
                throw new \InvalidArgumentException(sprintf('The bundle %s is not registered in the kernel.', $bundle));
36
            }
37
38 2
            $reflection = new \ReflectionClass($registeredBundles[$bundle]);
39 2
            $dirs[] = dirname($reflection->getFilename()).'/Resources/views';
40
41 2
            foreach ($overridePathPatterns as $overridePath) {
42 2
                $dirs[] = sprintf($overridePath, $bundle);
43
            }
44
        }
45
46 7
        if ($container->hasParameter('kernel.root_dir')) {
47 3
            $dirs[] = '%kernel.root_dir%/Resources/views';
48
        }
49
50 7
        $dirs[] = '%kernel.project_dir%/templates';
51
52 7
        $container->setParameter('incenteev_translation_checker.extractor.symfony.paths', $dirs);
53
54 7
        if (empty($config['extraction']['js']['paths'])) {
55 5
            $container->removeDefinition('incenteev_translation_checker.extractor.js');
56
        } else {
57 2
            $container->getDefinition('incenteev_translation_checker.extractor.js')
58 2
                ->replaceArgument(0, $config['extraction']['js']['paths'])
59 2
                ->replaceArgument(1, $config['extraction']['js']['default_domain']);
60
        }
61 7
    }
62
}
63