Completed
Push — l10n_master ( 9bc9d9...7bcb56 )
by Ruud
340:21 queued 321:33
created

registerTranslatorConfiguration()   C

Complexity

Conditions 11
Paths 200

Size

Total Lines 53

Duplication

Lines 10
Ratio 18.87 %

Importance

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