Completed
Pull Request — master (#993)
by David
16:22
created

TransformerPass::process()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.2141
Metric Value
dl 0
loc 27
ccs 11
cts 15
cp 0.7333
rs 5.3846
cc 8
eloc 14
nc 11
nop 1
crap 9.2141
1
<?php
2
3
namespace FOS\ElasticaBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Reference;
8
use InvalidArgumentException;
9
10
/**
11
 * Registers Transformer implementations into the TransformerCollection.
12
 *
13
 * @author Tim Nagel <[email protected]>
14
 */
15
class TransformerPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 11
    public function process(ContainerBuilder $container)
21
    {
22 11
        if (!$container->hasDefinition('fos_elastica.elastica_to_model_transformer.collection')) {
23
            return;
24
        }
25
26 11
        $transformers = array();
27
28 11
        foreach ($container->findTaggedServiceIds('fos_elastica.elastica_to_model_transformer') as $id => $tags) {
29 7
            foreach ($tags as $tag) {
30 7
                if (empty($tag['index']) || empty($tag['type'])) {
31
                    throw new InvalidArgumentException('The Transformer must have both a type and an index defined.');
32
                }
33
34 7
                $transformers[$tag['index']][$tag['type']] = new Reference($id);
35
            }
36
        }
37
38 11
        foreach ($transformers as $index => $indexTransformers) {
39 7
            if (!$container->hasDefinition(sprintf('fos_elastica.elastica_to_model_transformer.collection.%s', $index))) {
40 7
                continue;
41
            }
42
43
            $index = $container->getDefinition(sprintf('fos_elastica.elastica_to_model_transformer.collection.%s', $index));
44
            $index->replaceArgument(0, $indexTransformers);
45
        }
46 11
    }
47
}
48