Completed
Pull Request — master (#290)
by Leny
08:38
created

ViewReferenceTransformerCompilerPass::process()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 6
eloc 16
nc 6
nop 1
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Class ViewReferenceTransformerCompilerPass.
12
 */
13
class ViewReferenceTransformerCompilerPass implements CompilerPassInterface
14
{
15
    public function process(ContainerBuilder $container)
16
    {
17
        if (!$container->hasDefinition('victoire_view_reference.transformer_chain')) {
18
            return;
19
        }
20
        $definition = $container->getDefinition(
21
            'victoire_view_reference.transformer_chain'
22
        );
23
        $taggedServices = $container->findTaggedServiceIds(
24
            'victoire_view_reference.transformer'
25
        );
26
        foreach ($taggedServices as $id => $tagAttributes) {
27
            foreach ($tagAttributes as $attributes) {
28
                if (!array_key_exists('viewNamespace', $attributes)) {
29
                    throw new InvalidConfigurationException('ViewNamespace class attribute is not defined for service '.$id);
30
                }
31
                if (!array_key_exists('outputFormat', $attributes)) {
32
                    throw new InvalidConfigurationException('OutputFormat (xml|array) attribute is not defined for service '.$id);
33
                }
34
                $definition->addMethodCall(
35
                    'addTransformer',
36
                    [new Reference($id), $attributes['viewNamespace'], $attributes['outputFormat']]
37
                );
38
            }
39
        }
40
    }
41
}
42