Passed
Push — master ( a02321...710a62 )
by Dominik
42s
created

SerializationCompilerPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 62
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 56 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\DependencyInjection;
6
7
use Chubbyphp\Serialization\Encoder\Encoder;
8
use Chubbyphp\Serialization\Encoder\JsonTypeEncoder;
9
use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder;
10
use Chubbyphp\Serialization\Encoder\XmlTypeEncoder;
11
use Chubbyphp\Serialization\Encoder\YamlTypeEncoder;
12
use Chubbyphp\Serialization\Normalizer\Normalizer;
13
use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry;
14
use Chubbyphp\Serialization\Serializer;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\Yaml\Yaml;
20
21
final class SerializationCompilerPass implements CompilerPassInterface
22
{
23
    /**
24
     * @param ContainerBuilder $container
25
     */
26 1
    public function process(ContainerBuilder $container)
27
    {
28 1
        $container->register('chubbyphp.serializer', Serializer::class)->setPublic(true)->setArguments([
29 1
            new Reference('chubbyphp.serializer.normalizer'),
30 1
            new Reference('chubbyphp.serializer.encoder'),
31
        ]);
32
33
        $container
34 1
            ->register('chubbyphp.serializer.normalizer', Normalizer::class)
35 1
            ->setPublic(true)
36 1
            ->setArguments([
37 1
                new Reference('chubbyphp.serializer.normalizer.objectmappingregistry'),
38 1
                new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
39
            ]);
40
41 1
        $normalizerObjectMappingReferences = [];
42 1
        foreach ($container->findTaggedServiceIds('serializer.normalizer.objectmapping') as $id => $tags) {
43 1
            $normalizerObjectMappingReferences[] = new Reference($id);
44
        }
45
46
        $container
47 1
            ->register(
48 1
                'chubbyphp.serializer.normalizer.objectmappingregistry',
49 1
                NormalizerObjectMappingRegistry::class
50
            )
51 1
            ->setPublic(true)
52 1
            ->setArguments([$normalizerObjectMappingReferences]);
53
54
        $container
55 1
            ->register('chubbyphp.serializer.encoder.type.json', JsonTypeEncoder::class)
56 1
            ->addTag('chubbyphp.serializer.encoder.type');
57
58
        $container
59 1
            ->register('chubbyphp.serializer.encoder.type.urlencoded', UrlEncodedTypeEncoder::class)
60 1
            ->addTag('chubbyphp.serializer.encoder.type');
61
62
        $container
63 1
            ->register('chubbyphp.serializer.encoder.type.xml', XmlTypeEncoder::class)
64 1
            ->addTag('chubbyphp.serializer.encoder.type');
65
66 1
        if (class_exists(Yaml::class)) {
67
            $container
68 1
            ->register('chubbyphp.serializer.encoder.type.yaml', YamlTypeEncoder::class)
69 1
            ->addTag('chubbyphp.serializer.encoder.type');
70
        }
71
72 1
        $encoderTypeReferences = [];
73 1
        foreach ($container->findTaggedServiceIds('chubbyphp.serializer.encoder.type') as $id => $tags) {
74 1
            $encoderTypeReferences[] = new Reference($id);
75
        }
76
77
        $container
78 1
            ->register('chubbyphp.serializer.encoder', Encoder::class)
79 1
            ->setPublic(true)
80 1
            ->setArguments([$encoderTypeReferences]);
81 1
    }
82
}
83