RegisterVisitorPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 50
ccs 32
cts 32
cp 1
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 216
    public function process(ContainerBuilder $container)
28
    {
29
        $mapping = [
30 216
            'serialization'   => Direction::SERIALIZATION,
31 216
            'deserialization' => Direction::DESERIALIZATION,
32 168
        ];
33
34 216
        $typeRegistry = $container->getDefinition('ivory.serializer.registry.visitor');
35
36 216
        foreach ($container->findTaggedServiceIds($tag = 'ivory.serializer.visitor') as $id => $attributes) {
37 216
            foreach ($attributes as $attribute) {
38 216
                if (!isset($attribute['direction'])) {
39 9
                    throw new \RuntimeException(sprintf(
40 9
                        'No "direction" attribute found for the tag "%s" on the service "%s".',
41 7
                        $tag,
42
                        $id
43 7
                    ));
44
                }
45
46 207
                if (!isset($mapping[$attribute['direction']])) {
47 9
                    throw new \RuntimeException(sprintf(
48 9
                        'The "direction" attribute (%s) found for the tag "%s" on the service "%s" is not valid (Supported: %s).',
49 9
                        $attribute['direction'],
50 7
                        $tag,
51 7
                        $id,
52 9
                        implode(', ', array_keys($mapping))
53 7
                    ));
54
                }
55
56 198
                if (!isset($attribute['format'])) {
57 9
                    throw new \RuntimeException(sprintf(
58 9
                        'No "format" attribute found for the tag "%s" on the service "%s".',
59 7
                        $tag,
60
                        $id
61 7
                    ));
62
                }
63
64 189
                $typeRegistry->addMethodCall(
65 189
                    'registerVisitor',
66 189
                    [$mapping[$attribute['direction']], $attribute['format'], new Reference($id)]
67 147
                );
68 147
            }
69 147
        }
70 189
    }
71
}
72