Completed
Push — master ( d17154...1fc820 )
by Sullivan
11:02 queued 08:59
created

SonataTranslationExtension::configureChecker()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 9
c 2
b 0
f 2
nc 2
nop 2
dl 0
loc 14
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\TranslationBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Processor;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
20
/**
21
 * @author Nicolas Bastien <[email protected]>
22
 */
23
class SonataTranslationExtension extends Extension
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function load(array $configs, ContainerBuilder $container)
29
    {
30
        $processor = new Processor();
31
        $configuration = new Configuration();
32
        $config = $processor->processConfiguration($configuration, $configs);
33
34
        $container->setParameter('sonata_translation.locales', $config['locales']);
35
        $container->setParameter('sonata_translation.default_locale', $config['default_locale']);
36
37
        $isEnabled = false;
38
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39
        $loader->load('service.xml');
40
41
        $translationTargets = array();
42 View Code Duplication
        if ($config['gedmo']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $isEnabled = true;
44
            $loader->load('service_gedmo.xml');
45
46
            $translationTargets['gedmo']['implements'] = array_merge(
47
                array('Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface'),
48
                $config['gedmo']['implements']
49
            );
50
            $translationTargets['gedmo']['instanceof'] = $config['gedmo']['instanceof'];
51
        }
52 View Code Duplication
        if ($config['knplabs']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $isEnabled = true;
54
            $loader->load('service_knplabs.xml');
55
56
            $translationTargets['knplabs']['implements'] = array_merge(
57
                array('Sonata\TranslationBundle\Model\TranslatableInterface'),
58
                $config['knplabs']['implements']
59
            );
60
            $translationTargets['knplabs']['instanceof'] = $config['knplabs']['instanceof'];
61
        }
62 View Code Duplication
        if ($config['phpcr']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $isEnabled = true;
64
            $loader->load('service_phpcr.xml');
65
66
            $translationTargets['phpcr']['implements'] = array_merge(
67
                array(
68
                    'Sonata\TranslationBundle\Model\Phpcr\TranslatableInterface',
69
                    'Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface',
70
                ),
71
                $config['phpcr']['implements']
72
            );
73
            $translationTargets['phpcr']['instanceof'] = $config['phpcr']['instanceof'];
74
        }
75
76
        if ($isEnabled === true) {
77
            $loader->load('block.xml');
78
            $loader->load('listener.xml');
79
            $loader->load('twig.xml');
80
        }
81
82
        $container->setParameter('sonata_translation.targets', $translationTargets);
83
84
        $this->configureChecker($container, $translationTargets);
85
    }
86
87
    /**
88
     * @param ContainerBuilder $container
89
     * @param array            $translationTargets
90
     */
91
    protected function configureChecker(ContainerBuilder $container, $translationTargets)
92
    {
93
        $translatableCheckerDefinition = $container->getDefinition('sonata_translation.checker.translatable');
94
95
        $supportedInterfaces = array();
96
        $supportedModels = array();
97
        foreach ($translationTargets as $targets) {
98
            $supportedInterfaces = array_merge($supportedInterfaces, $targets['implements']);
99
            $supportedModels = array_merge($supportedModels, $targets['instanceof']);
100
        }
101
102
        $translatableCheckerDefinition->addMethodCall('setSupportedInterfaces', array($supportedInterfaces));
103
        $translatableCheckerDefinition->addMethodCall('setSupportedModels', array($supportedModels));
104
    }
105
}
106