Passed
Push — master ( d2e487...b38026 )
by Tobias
05:31
created

ExternalTranslatorPass::process()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 6
rs 9.2222
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\DependencyInjection\CompilerPass;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
class ExternalTranslatorPass implements CompilerPassInterface
22
{
23 2
    public function process(ContainerBuilder $container): void
24
    {
25 2
        if (!$container->hasDefinition('php_translation.translator_service.external_translator')) {
26 1
            return;
27
        }
28
29 1
        $services = $container->findTaggedServiceIds('php_translation.external_translator');
30 1
        $translators = [];
31 1
        foreach ($services as $id => $tags) {
32 1
            foreach ($tags as $tag) {
33 1
                if (!isset($tag['priority'])) {
34 1
                    $tag['priority'] = 0;
35
                }
36 1
                $translators[$id] = $tag['priority'];
37
            }
38
        }
39
40
        // Sort by priority
41 1
        \asort($translators);
42
43 1
        $def = $container->getDefinition('php_translation.translator_service.external_translator');
44 1
        foreach ($translators as $id => $prio) {
45 1
            $def->addMethodCall('addTranslatorService', [new Reference($id)]);
46
        }
47 1
    }
48
}
49