Completed
Push — master ( 660942...00951d )
by Tobias
06:51
created

ExtractorPass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 3
dl 0
loc 56
ccs 28
cts 31
cp 0.9032
rs 10
c 1
b 0
f 0

3 Methods

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