ExtractorPass   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 70.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 50
ccs 19
cts 27
cp 0.7037
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addVisitors() 0 15 6
A process() 0 4 1
A getExtractors() 0 22 5
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\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Translation\Extractor\Extractor;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class ExtractorPass implements CompilerPassInterface
24
{
25 2
    public function process(ContainerBuilder $container): void
26
    {
27 2
        $extractors = $this->getExtractors($container);
28 2
        $this->addVisitors($container, $extractors);
29 2
    }
30
31
    /**
32
     * @return array type => Definition[]
33
     */
34 2
    private function getExtractors(ContainerBuilder $container): array
35
    {
36 2
        if (!$container->hasDefinition(Extractor::class)) {
37 1
            return [];
38
        }
39
40
        /** @var Definition $def */
41 1
        $def = $container->getDefinition(Extractor::class);
42 1
        $services = $container->findTaggedServiceIds('php_translation.extractor');
43 1
        $extractors = [];
44 1
        foreach ($services as $id => $tags) {
45 1
            foreach ($tags as $tag) {
46 1
                if (!isset($tag['type'])) {
47
                    throw new \LogicException('The tag "php_translation.extractor" must have a "type".');
48
                }
49
50 1
                $extractors[$tag['type']][] = $container->getDefinition($id);
51 1
                $def->addMethodCall('addFileExtractor', [new Reference($id)]);
52
            }
53
        }
54
55 1
        return $extractors;
56
    }
57
58 2
    private function addVisitors(ContainerBuilder $container, array $extractors): void
59
    {
60 2
        $services = $container->findTaggedServiceIds('php_translation.visitor');
61 2
        foreach ($services as $id => $tags) {
62
            foreach ($tags as $tag) {
63
                if (!isset($tag['type'])) {
64
                    throw new \LogicException('The tag "php_translation.visitor" must have a "type".');
65
                }
66
                if (!isset($extractors[$tag['type']])) {
67
                    continue;
68
                }
69
70
                /** @var Definition $extractor */
71
                foreach ($extractors[$tag['type']] as $extractor) {
72
                    $extractor->addMethodCall('addVisitor', [new Reference($id)]);
73
                }
74
            }
75
        }
76 2
    }
77
}
78