SerializerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 38
c 1
b 0
f 0
dl 0
loc 64
ccs 40
cts 40
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createSerializer() 0 23 1
A createClassMetadataFactory() 0 11 2
A createPropertyInfo() 0 24 2
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Bridge\Symfony\DependencyInjection\Factory;
12
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use Strider2038\JsonRpcClient\Bridge\Symfony\Serialization\DelegatingResponseDenormalizer;
15
use Strider2038\JsonRpcClient\Bridge\Symfony\Serialization\ResponseObjectDenormalizer;
16
use Symfony\Component\PropertyAccess\PropertyAccess;
17
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
18
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
19
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
20
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
21
use Symfony\Component\Serializer\Encoder\JsonEncoder;
22
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
23
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
24
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
25
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
26
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
27
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
28
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
29
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
30
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
31
use Symfony\Component\Serializer\Serializer;
32
use Symfony\Component\Serializer\SerializerInterface;
33
34
/**
35
 * Can be used to instantiate Symfony serializer as a standalone component. For better user experience it
36
 * is recommended to use library as Symfony bundle.
37
 *
38
 * @author Igor Lazarev <[email protected]>
39
 */
40
class SerializerFactory
41
{
42 5
    public static function createSerializer(array $normalizers = []): SerializerInterface
43
    {
44 5
        $encoders = [new JsonEncoder()];
45
46 5
        $classMetadataFactory = self::createClassMetadataFactory();
47 5
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
48 5
        $propertyInfo = self::createPropertyInfo();
49
50 5
        $normalizers = array_merge(
51 5
            $normalizers,
52
            [
53 5
                new DelegatingResponseDenormalizer(),
54 5
                new ResponseObjectDenormalizer(),
55 5
                new DateTimeNormalizer(),
56 5
                new DateTimeZoneNormalizer(),
57 5
                new DateIntervalNormalizer(),
58 5
                new JsonSerializableNormalizer($classMetadataFactory),
59 5
                new ArrayDenormalizer(),
60 5
                new ObjectNormalizer($classMetadataFactory, null, $propertyAccessor, $propertyInfo),
61
            ]
62
        );
63
64 5
        return new Serializer($normalizers, $encoders);
65
    }
66
67 5
    private static function createClassMetadataFactory(): ?ClassMetadataFactoryInterface
68
    {
69 5
        $classMetadataFactory = null;
70
71 5
        if (class_exists(AnnotationReader::class)) {
72 5
            $annotationReader = new AnnotationReader();
73 5
            $annotationLoader = new AnnotationLoader($annotationReader);
74 5
            $classMetadataFactory = new ClassMetadataFactory($annotationLoader);
75
        }
76
77 5
        return $classMetadataFactory;
78
    }
79
80 5
    private static function createPropertyInfo(): ?PropertyInfoExtractorInterface
81
    {
82 5
        $propertyInfo = null;
83
84 5
        if (class_exists(PropertyInfoExtractor::class)) {
85 5
            $phpDocExtractor = new PhpDocExtractor();
86 5
            $reflectionExtractor = new ReflectionExtractor();
87
88 5
            $listExtractors = [$reflectionExtractor];
89 5
            $typeExtractors = [$phpDocExtractor, $reflectionExtractor];
90 5
            $descriptionExtractors = [$phpDocExtractor];
91 5
            $accessExtractors = [$reflectionExtractor];
92 5
            $propertyInitializableExtractors = [$reflectionExtractor];
93
94 5
            $propertyInfo = new PropertyInfoExtractor(
95 5
                $listExtractors,
96 5
                $typeExtractors,
97 5
                $descriptionExtractors,
98 5
                $accessExtractors,
99 5
                $propertyInitializableExtractors
100
            );
101
        }
102
103 5
        return $propertyInfo;
104
    }
105
}
106