Completed
Push — feature/EVO-7964_fundInfo-exte... ( 9219e4...25799b )
by Bastian
252:34 queued 245:35
created

TransformerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 13.64%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 49
ccs 3
cts 22
cp 0.1364
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A findTaggedTransformer() 0 17 3
1
<?php
2
/**
3
 * TransformerPass
4
 */
5
6
namespace Graviton\ProxyBundle\DependencyInjection\Compiler;
7
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\DependencyInjection\Reference;
12
13
/**
14
 * Class TransformerPass
15
 *
16
 * @package Graviton\ProxyBundle\Definition\Loader
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class TransformerPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @param ContainerBuilder $container Symfony Service container
28
     *
29
     * @return void
30
     */
31 2
    public function process(ContainerBuilder $container)
32
    {
33
        // always first check if the primary service is defined
34 2
        if (!$container->has('graviton.proxy.service.transformationhandler')) {
35 2
            return;
36
        }
37
38
        $definition = $container->findDefinition('graviton.proxy.service.transformationhandler');
39
        $this->findTaggedTransformer($container, $definition, 'graviton.proxy.transformer.request', 'addRequestTransformation');
40
        $this->findTaggedTransformer($container, $definition, 'graviton.proxy.transformer.response', 'addResponseTransformation');
41
        $this->findTaggedTransformer($container, $definition, 'graviton.proxy.transformer.schema', 'addSchemaTransformation');
42
    }
43
44
    /**
45
     * Adds the found services to the TransformationHandler
46
     *
47
     * @param ContainerBuilder $container  Symfony Service Container
48
     * @param Definition       $definition Service the services shall be add to.
49
     * @param string           $tag        Tag identifying the service to be added
50
     * @param string           $callable   Name of the method to call to add the tagged service.
51
     *
52
     */
53
    private function findTaggedTransformer(ContainerBuilder $container, Definition $definition, $tag, $callable)
54
    {
55
        $taggedServices = $container->findTaggedServiceIds($tag);
56
57
        foreach ($taggedServices as $id => $tags) {
58
            foreach ($tags as $attributes) {
59
                $definition->addMethodCall(
60
                    $callable,
61
                    array(
62
                        $attributes["alias"],
63
                        $attributes["endpoint"],
64
                        new Reference($id)
65
                    )
66
                );
67
            }
68
        }
69
    }
70
}
71