Completed
Push — master ( f78604...98e267 )
by Pavel
04:30
created

SymfonySerializerCompilerPass::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.246

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 26
ccs 11
cts 14
cp 0.7856
rs 8.439
cc 5
eloc 12
nc 5
nop 1
crap 5.246
1
<?php
2
3
namespace ScayTrase\Api\Cruds\DependencyInjection\Compiler;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\Serializer\SerializerInterface;
11
12
final class SymfonySerializerCompilerPass implements CompilerPassInterface
13
{
14
    /** {@inheritdoc} */
15 2
    public function process(ContainerBuilder $container)
16
    {
17 2
        if (!$container->has('serializer') || $container->hasAlias('serializer')) {
18
19 1
            return;
20
        }
21
22 1
        $class = $container->getDefinition('serializer')->getClass();
23
24 1
        if ('%serializer.class%' === $class) {
25
            // 2.x Definition
26
            $class = $container->getParameter($class);
27
        }
28
29 1
        if (!in_array(SerializerInterface::class, class_implements($class), true)) {
30
            return;
31
        }
32
33 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../Resources/config'));
34 1
        $loader->load('symfony_serializer.yml');
35
36
        /** @var Reference $converter */
37 1
        $converter = $container->getDefinition('serializer.normalizer.object')->getArgument(1);
38
39 1
        $container->setAlias('serializer.normalizer.object.name_converter', (string)$converter);
40 1
    }
41
}
42