Test Failed
Pull Request — master (#11)
by Pavel
07:46
created

RelationHandlerPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Adapters\JMS\Compiler;
4
5
use Bankiru\Api\JsonRpc\Adapters\JMS\RelationsHandler;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
final class RelationHandlerPass implements CompilerPassInterface
11
{
12
    /** {@inheritdoc} */
13
    public function process(ContainerBuilder $container)
14
    {
15
        if (!$container->hasParameter('jsonrpc_server.jms.handlers')) {
16
            return;
17
        }
18
19
        foreach ($container->getParameter('jsonrpc_server.jms.handlers') as $handler => $emid) {
20
            $this->configureRelationHandler($container, $handler, $emid);
21
        }
22
    }
23
24
    private function configureRelationHandler(ContainerBuilder $builder, $handler, $emid)
25
    {
26
        if (!$builder->has($emid)) {
27
            return;
28
        }
29
30
        $builder->register('jms_serializer.handler.relation.' . $handler, RelationsHandler::class)
31
                ->setArguments([new Reference($emid)])
32
                ->addTag(
33
                    'jms_serializer.handler',
34
                    [
35
                        'type'      => $handler,
36
                        'direction' => 'serialization',
37
                        'format'    => 'json',
38
                        'method'    => 'serializeRelation',
39
                    ]
40
                )
41
                ->addTag(
42
                    'jms_serializer.handler',
43
                    [
44
                        'type'      => $handler,
45
                        'direction' => 'deserialization',
46
                        'format'    => 'json',
47
                        'method'    => 'deserializeRelation',
48
                    ]
49
                )
50
                ->addTag(
51
                    'jms_serializer.handler',
52
                    [
53
                        'type'      => $handler . '<?>',
54
                        'direction' => 'serialization',
55
                        'format'    => 'json',
56
                        'method'    => 'serializeRelation',
57
                    ]
58
                )
59
                ->addTag(
60
                    'jms_serializer.handler',
61
                    [
62
                        'type'      => $handler . '<?>',
63
                        'direction' => 'deserialization',
64
                        'format'    => 'json',
65
                        'method'    => 'deserializeRelation',
66
                    ]
67
                );
68
    }
69
}
70