Completed
Push — master ( 946e3c...11aaa6 )
by Sullivan
03:10
created

TranslationPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 21 4
1
<?php
2
3
namespace SLLH\IsoCodesValidator\Bridge\Symfony\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * Register IsoCodesValidator translations files.
11
 *
12
 * Files resources register method based on
13
 * Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension::registerTranslatorConfiguration
14
 *
15
 * @author Sullivan Senechal <[email protected]>
16
 */
17
class TranslationPass implements CompilerPassInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function process(ContainerBuilder $container)
23
    {
24
        if (!$container->hasDefinition('translator.default')) {
25
            return;
26
        }
27
28
        $translator = $container->findDefinition('translator.default');
29
30
        $finder = Finder::create()
31
            ->files()
32
            ->filter(function (\SplFileInfo $file) {
33
                return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename());
34
            })
35
            ->in(__DIR__.'/../../../../Resources/translations')
36
        ;
37
38
        foreach ($finder as $file) {
39
            list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
40
            $translator->addMethodCall('addResource', [$format, (string) $file, $locale, $domain]);
41
        }
42
    }
43
}
44