Completed
Push — master ( fef1f2...3eb154 )
by Tim
7s
created

TransformerPass::process()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.7021
Metric Value
dl 0
loc 27
ccs 14
cts 18
cp 0.7778
rs 5.3846
cc 8
eloc 14
nc 11
nop 1
crap 8.7021
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 12
    public function process(ContainerBuilder $container)
21
    {
22 12
        if (!$container->hasDefinition('fos_elastica.elastica_to_model_transformer.collection')) {
23
            return;
24
        }
25
26 12
        $transformers = array();
27
28 12
        foreach ($container->findTaggedServiceIds('fos_elastica.elastica_to_model_transformer') as $id => $tags) {
29 8
            foreach ($tags as $tag) {
30 8
                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 8
                $transformers[$tag['index']][$tag['type']] = new Reference($id);
35 8
            }
36 12
        }
37
38 12
        foreach ($transformers as $index => $indexTransformers) {
39 8
            if (!$container->hasDefinition(sprintf('fos_elastica.elastica_to_model_transformer.collection.%s', $index))) {
40 8
                continue;
41
            }
42
43
            $index = $container->getDefinition(sprintf('fos_elastica.elastica_to_model_transformer.collection.%s', $index));
44
            $index->replaceArgument(0, $indexTransformers);
45 12
        }
46 12
    }
47
}
48