Completed
Push — master ( fbc769...4f234a )
by Tobias
10:09
created

ExtractorPass::getExtractors()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
rs 8.5906
cc 5
eloc 13
nc 5
nop 1
crap 5.0113
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
        if (!$container->hasDefinition('php_translation.extractor')) {
38 1
            return [];
39 1
        }
40 1
41 1
        /** @var Definition $def */
42 1
        $def = $container->getDefinition('php_translation.extractor');
43 1
        $services = $container->findTaggedServiceIds('php_translation.extractor');
44
        $extractors = [];
45
        foreach ($services as $id => $tags) {
46
            foreach ($tags as $tag) {
47 1
                if (!isset($tag['type'])) {
48 1
                    throw new \LogicException('The tag "php_translation.extractor" must have a "type".');
49 1
                }
50 1
51
                $extractors[$tag['type']][] = $container->getDefinition($id);
52 1
                $def->addMethodCall('addFileExtractor', [new Reference($id)]);
53
            }
54
        }
55
56
        return $extractors;
57
    }
58
59 1
    /**
60
     * @param ContainerBuilder $container
61 1
     * @param $extractors
62 1
     */
63 1
    private function addVisitors(ContainerBuilder $container, $extractors)
64 1
    {
65
        $services = $container->findTaggedServiceIds('php_translation.visitor');
66
        foreach ($services as $id => $tags) {
67 1
            foreach ($tags as $tag) {
68
                if (!isset($tag['type'])) {
69
                    throw new \LogicException('The tag "php_translation.visitor" must have a "type".');
70
                }
71 1
                if (!isset($extractors[$tag['type']])) {
72 1
                    continue;
73 1
                }
74 1
75 1
                foreach ($extractors[$tag['type']] as $extractor) {
76 1
                    $extractor->addMethodCall('addVisitor', [new Reference($id)]);
77
                }
78
            }
79
        }
80
    }
81
}
82