Completed
Push — master ( 4cac96...5c184e )
by Jeroen
11:16
created

registerTranslatorConfiguration()   F

Complexity

Conditions 13
Paths 800

Size

Total Lines 66

Duplication

Lines 20
Ratio 30.3 %

Importance

Changes 0
Metric Value
dl 20
loc 66
rs 2.924
c 0
b 0
f 0
cc 13
nc 800
nop 2

How to fix   Long Method    Complexity   

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
namespace Kunstmaan\TranslatorBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Config\Resource\DirectoryResource;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Symfony\Component\HttpKernel\Kernel;
12
13
/**
14
 * This is the class that loads and manages your bundle configuration
15
 *
16
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
17
 */
18
class KunstmaanTranslatorExtension extends Extension
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function load(array $configs, ContainerBuilder $container)
24
    {
25
        $configuration = new Configuration();
26
        $config = $this->processConfiguration($configuration, $configs);
27
28
        if ($config['enabled'] === false) {
29
            return;
30
        }
31
32
        if (!$container->hasParameter('requiredlocales')) {
33
            $container->setParameter('requiredlocales', ['nl', 'fr', 'en']);
34
        }
35
        $container->setParameter('kuma_translator.enabled', $config['enabled']);
36
        $container->setParameter('kuma_translator.default_bundle', $config['default_bundle']);
37
        $container->setParameter('kuma_translator.bundles', $config['bundles']);
38
        $container->setParameter('kuma_translator.cache_dir', $config['cache_dir']);
39
        if (empty($config['managed_locales']) && $container->hasParameter('requiredlocales')) {
40
            $config['managed_locales'] = explode('|', $container->getParameter('requiredlocales'));
41
        }
42
        $container->setParameter('kuma_translator.managed_locales', $config['managed_locales']);
43
        $container->setParameter('kuma_translator.file_formats', $config['file_formats']);
44
        $container->setParameter('kuma_translator.storage_engine.type', $config['storage_engine']['type']);
45
        $container->setParameter('kuma_translator.profiler', $container->getParameter('kernel.debug'));
46
        $container->setParameter('kuma_translator.debug', is_null($config['debug']) ? $container->getParameter('kernel.debug') : $config['debug']);
47
48
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
49
        $loader->load('services.yml');
50
        $loader->load('repositories.yml');
51
        $loader->load('commands.yml');
52
53
        $this->setTranslationConfiguration($config, $container);
54
        $container->getDefinition('kunstmaan_translator.datacollector')->setDecoratedService('translator');
55
    }
56
57
    public function setTranslationConfiguration($config, $container)
58
    {
59
        $container->setAlias('translator', 'kunstmaan_translator.service.translator.translator')->setPublic(true);
60
        $container->setAlias('translator.default', 'kunstmaan_translator.service.translator.translator')->setPublic(true);
61
        $translator = $container->getDefinition('kunstmaan_translator.service.translator.translator');
62
        $this->registerTranslatorConfiguration($config, $container);
63
64
        // overwrites everything
65
        $translator->addMethodCall('addDatabaseResources', []);
66
67
        $translator->addMethodCall('setFallbackLocales', [['en']]);
68
69
        if ($container->hasParameter('kunstmaan_admin.default_locale')) {
70
            $translator->addMethodCall('setFallbackLocales', [[$container->getParameter('kunstmaan_admin.default_locale')]]);
71
        }
72
    }
73
74
    /**
75
     * Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension
76
     * $this->registerTranslatorConfiguration($config['translator'], $container);
77
     * Used to load all other translation files
78
     */
79
    public function registerTranslatorConfiguration($config, $container)
80
    {
81
        $translator = $container->getDefinition('kunstmaan_translator.service.translator.translator');
82
83
        $dirs = [];
84 View Code Duplication
        if (class_exists('Symfony\Component\Validator\Validation')) {
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...
85
            $r = new \ReflectionClass('Symfony\Component\Validator\Validation');
86
87
            $dirs[] = dirname($r->getFilename()).'/Resources/translations';
88
        }
89 View Code Duplication
        if (class_exists('Symfony\Component\Form\Form')) {
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...
90
            $r = new \ReflectionClass('Symfony\Component\Form\Form');
91
92
            $dirs[] = dirname($r->getFilename()).'/Resources/translations';
93
        }
94
95 View Code Duplication
        if (Kernel::VERSION_ID < 4000) {
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...
96
            $overridePath = $container->getParameter('kernel.root_dir').'/Resources/%s/translations';
97
        } else {
98
            $overridePath = $container->getParameter('kernel.project_dir').'/translations';
99
        }
100
101
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
102
            $reflection = new \ReflectionClass($class);
103
            if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/translations')) {
104
                $dirs[] = $dir;
105
            }
106
            if (is_dir($dir = sprintf($overridePath, $bundle))) {
107
                $dirs[] = $dir;
108
            }
109
        }
110
111 View Code Duplication
        if (Kernel::VERSION_ID < 4000) {
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...
112
            $dir = $container->getParameter('kernel.root_dir').'/Resources/translations';
113
        } else {
114
            $dir = $container->getParameter('kernel.project_dir').'/translations';
115
        }
116
117
        if (is_dir($dir)) {
118
            $dirs[] = $dir;
119
        }
120
121
        // Register translation resources
122
        if (count($dirs) > 0) {
123
            foreach ($dirs as $dir) {
124
                $container->addResource(new DirectoryResource($dir));
125
            }
126
127
            $finder = Finder::create();
128
            $finder->files();
129
130
            $finder->filter(
131
                function (\SplFileInfo $file) {
132
                    return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
133
                }
134
            );
135
136
            $finder->in($dirs);
137
138
            foreach ($finder as $file) {
139
                // filename is domain.locale.format
140
                list($domain, $locale, $format) = explode('.', $file->getBasename());
141
                $translator->addMethodCall('addResource', [$format, (string) $file, $locale, $domain]);
142
            }
143
        }
144
    }
145
}
146