RegisterVisitorPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 50
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 44 6
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer bundle package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\SerializerBundle\DependencyInjection\Compiler;
13
14
use Ivory\Serializer\Direction;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class RegisterVisitorPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $mapping = [
30
            'serialization'   => Direction::SERIALIZATION,
31
            'deserialization' => Direction::DESERIALIZATION,
32
        ];
33
34
        $typeRegistry = $container->getDefinition('ivory.serializer.registry.visitor');
35
36
        foreach ($container->findTaggedServiceIds($tag = 'ivory.serializer.visitor') as $id => $attributes) {
37
            foreach ($attributes as $attribute) {
38
                if (!isset($attribute['direction'])) {
39
                    throw new \RuntimeException(sprintf(
40
                        'No "direction" attribute found for the tag "%s" on the service "%s".',
41
                        $tag,
42
                        $id
43
                    ));
44
                }
45
46
                if (!isset($mapping[$attribute['direction']])) {
47
                    throw new \RuntimeException(sprintf(
48
                        'The "direction" attribute (%s) found for the tag "%s" on the service "%s" is not valid (Supported: %s).',
49
                        $attribute['direction'],
50
                        $tag,
51
                        $id,
52
                        implode(', ', array_keys($mapping))
53
                    ));
54
                }
55
56
                if (!isset($attribute['format'])) {
57
                    throw new \RuntimeException(sprintf(
58
                        'No "format" attribute found for the tag "%s" on the service "%s".',
59
                        $tag,
60
                        $id
61
                    ));
62
                }
63
64
                $typeRegistry->addMethodCall(
65
                    'registerVisitor',
66
                    [$mapping[$attribute['direction']], $attribute['format'], new Reference($id)]
67
                );
68
            }
69
        }
70
    }
71
}
72