Completed
Push — master ( c62646...583a7d )
by Eric
02:58
created

RegisterClassMetadataLoaderPass   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 25 3
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 = array_keys($container->findTaggedServiceIds($tag = 'ivory.serializer.loader'));
31
32
        if (empty($loaders)) {
33
            throw new \RuntimeException(sprintf(
34
                'You must define at least one class metadata loader by enabling the reflection loader in your '.
35
                'configuration or by registering a loader in the container with the tag "%s".',
36
                $tag
37
            ));
38
        }
39
40
        $loader = 'ivory.serializer.mapping.loader';
41
42
        if (count($loaders) > 1) {
43
            $container->setDefinition($loader, new Definition(ChainClassMetadataLoader::class, [
44
                array_map(function ($service) {
45
                    return new Reference($service);
46
                }, $loaders),
47
                new Reference('ivory.serializer.type.parser'),
48
            ]));
49
        } else {
50
            $container->setAlias($loader, array_shift($loaders));
51
        }
52
    }
53
}
54