RegisterClassMetadataLoaderPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 33 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\Mapping\Loader\ChainClassMetadataLoader;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class RegisterClassMetadataLoaderPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        $loaders = [];
31
32
        foreach ($container->findTaggedServiceIds($tag = 'ivory.serializer.loader') as $id => $attributes) {
33
            foreach ($attributes as $attribute) {
34
                $priority = isset($attribute['priority']) ? $attribute['priority'] : 0;
35
                $loaders[$priority][] = new Reference($id);
36
            }
37
        }
38
39
        if (empty($loaders)) {
40
            throw new \RuntimeException(sprintf(
41
                'You must define at least one class metadata loader by enabling the reflection loader in your '.
42
                'configuration or by registering a loader in the container with the tag "%s".',
43
                $tag
44
            ));
45
        }
46
47
        $loader = 'ivory.serializer.mapping.loader';
48
49
        ksort($loaders);
50
        $loaders = call_user_func_array('array_merge', $loaders);
51
52
        if (count($loaders) > 1) {
53
            $container->setDefinition($loader, new Definition(ChainClassMetadataLoader::class, [
54
                $loaders,
55
                new Reference('ivory.serializer.type.parser'),
56
            ]));
57
        } else {
58
            $container->setAlias($loader, (string) array_shift($loaders));
59
        }
60
    }
61
}
62