RegisterClassMetadataLoaderPass::process()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
ccs 23
cts 23
cp 1
rs 8.439
cc 6
eloc 20
nc 12
nop 1
crap 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 234
    public function process(ContainerBuilder $container)
29
    {
30 234
        $loaders = [];
31
32 234
        foreach ($container->findTaggedServiceIds($tag = 'ivory.serializer.loader') as $id => $attributes) {
33 225
            foreach ($attributes as $attribute) {
34 225
                $priority = isset($attribute['priority']) ? $attribute['priority'] : 0;
35 225
                $loaders[$priority][] = new Reference($id);
36 175
            }
37 182
        }
38
39 234
        if (empty($loaders)) {
40 9
            throw new \RuntimeException(sprintf(
41
                'You must define at least one class metadata loader by enabling the reflection loader in your '.
42 9
                'configuration or by registering a loader in the container with the tag "%s".',
43
                $tag
44 7
            ));
45
        }
46
47 225
        $loader = 'ivory.serializer.mapping.loader';
48
49 225
        ksort($loaders);
50 225
        $loaders = call_user_func_array('array_merge', $loaders);
51
52 225
        if (count($loaders) > 1) {
53 216
            $container->setDefinition($loader, new Definition(ChainClassMetadataLoader::class, [
54 216
                $loaders,
55 216
                new Reference('ivory.serializer.type.parser'),
56 168
            ]));
57 168
        } else {
58 9
            $container->setAlias($loader, (string) array_shift($loaders));
59
        }
60 225
    }
61
}
62