Completed
Push — master ( 516597...0aeec3 )
by Dominik
04:41
created

SerializationCompilerPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 42
dl 0
loc 71
ccs 34
cts 34
cp 1
rs 10
c 1
b 0
f 1
wmc 4

1 Method

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