SonataTranslationExtension::load()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 69

Duplication

Lines 33
Ratio 47.83 %

Importance

Changes 0
Metric Value
dl 33
loc 69
rs 7.743
c 0
b 0
f 0
cc 7
nc 64
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\DependencyInjection;
15
16
use Symfony\Component\Config\Definition\Processor;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * @author Nicolas Bastien <[email protected]>
24
 */
25
class SonataTranslationExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $configs, ContainerBuilder $container): void
31
    {
32
        $processor = new Processor();
33
        $configuration = new Configuration();
34
        $config = $processor->processConfiguration($configuration, $configs);
35
36
        $container->setParameter('sonata_translation.locales', $config['locales']);
37
        $container->setParameter('sonata_translation.default_filter_mode', $config['default_filter_mode']);
38
        $container->setParameter('sonata_translation.default_locale', $config['default_locale']);
39
        $container->setParameter('sonata_translation.locale_switcher_show_country_flags', $config['locale_switcher_show_country_flags']);
40
41
        $isEnabled = false;
42
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
43
        $loader->load('twig_intl.xml');
44
45
        if ($config['locale_switcher']) {
46
            $loader->load('service_locale_switcher.xml');
47
        }
48
49
        $bundles = $container->getParameter('kernel.bundles');
50
        if (\array_key_exists('SonataDoctrineORMAdminBundle', $bundles)) {
51
            $loader->load('service_orm.xml');
52
        }
53
54
        $translationTargets = [];
55 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...
56
            $isEnabled = true;
57
            $loader->load('service_gedmo.xml');
58
59
            $translationTargets['gedmo']['implements'] = array_merge(
60
                ['Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface'],
61
                $config['gedmo']['implements']
62
            );
63
            $translationTargets['gedmo']['instanceof'] = $config['gedmo']['instanceof'];
64
        }
65 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...
66
            $isEnabled = true;
67
            $loader->load('service_knplabs.xml');
68
69
            $translationTargets['knplabs']['implements'] = array_merge(
70
                ['Sonata\TranslationBundle\Model\TranslatableInterface'],
71
                $config['knplabs']['implements']
72
            );
73
            $translationTargets['knplabs']['instanceof'] = $config['knplabs']['instanceof'];
74
        }
75 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...
76
            $isEnabled = true;
77
            $loader->load('service_phpcr.xml');
78
79
            $translationTargets['phpcr']['implements'] = array_merge(
80
                [
81
                    'Sonata\TranslationBundle\Model\Phpcr\TranslatableInterface',
82
                    'Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface',
83
                ],
84
                $config['phpcr']['implements']
85
            );
86
            $translationTargets['phpcr']['instanceof'] = $config['phpcr']['instanceof'];
87
        }
88
89
        if (true === $isEnabled) {
90
            $loader->load('block.xml');
91
            $loader->load('listener.xml');
92
            $loader->load('twig.xml');
93
        }
94
95
        $container->setParameter('sonata_translation.targets', $translationTargets);
96
97
        $this->configureChecker($container, $translationTargets);
98
    }
99
100
    /**
101
     * @param array $translationTargets
102
     */
103
    protected function configureChecker(ContainerBuilder $container, $translationTargets): void
104
    {
105
        if (!$container->hasDefinition('sonata_translation.checker.translatable')) {
106
            return;
107
        }
108
        $translatableCheckerDefinition = $container->getDefinition('sonata_translation.checker.translatable');
109
110
        $supportedInterfaces = [];
111
        $supportedModels = [];
112
        foreach ($translationTargets as $targets) {
113
            $supportedInterfaces = array_merge($supportedInterfaces, $targets['implements']);
114
            $supportedModels = array_merge($supportedModels, $targets['instanceof']);
115
        }
116
117
        $translatableCheckerDefinition->addMethodCall('setSupportedInterfaces', [$supportedInterfaces]);
118
        $translatableCheckerDefinition->addMethodCall('setSupportedModels', [$supportedModels]);
119
    }
120
}
121