Completed
Pull Request — master (#7)
by Igor
07:53 queued 03:25
created

SerializerFactory   A

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 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 4
    public static function createSerializer(array $normalizers = []): SerializerInterface
43
    {
44 4
        $encoders = [new JsonEncoder()];
45
46 4
        $classMetadataFactory = self::createClassMetadataFactory();
47 4
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
48 4
        $propertyInfo = self::createPropertyInfo();
49
50 4
        $normalizers = array_merge(
51 4
            $normalizers,
52
            [
53 4
                new DelegatingResponseDenormalizer(),
54 4
                new ResponseObjectDenormalizer(),
55 4
                new DateTimeNormalizer(),
56 4
                new DateTimeZoneNormalizer(),
57 4
                new DateIntervalNormalizer(),
58 4
                new JsonSerializableNormalizer($classMetadataFactory),
59 4
                new ArrayDenormalizer(),
60 4
                new ObjectNormalizer($classMetadataFactory, null, $propertyAccessor, $propertyInfo),
61
            ]
62
        );
63
64 4
        return new Serializer($normalizers, $encoders);
65
    }
66
67 4
    private static function createClassMetadataFactory(): ?ClassMetadataFactoryInterface
68
    {
69 4
        $classMetadataFactory = null;
70
71 4
        if (class_exists(AnnotationReader::class)) {
72 4
            $annotationReader = new AnnotationReader();
73 4
            $annotationLoader = new AnnotationLoader($annotationReader);
74 4
            $classMetadataFactory = new ClassMetadataFactory($annotationLoader);
75
        }
76
77 4
        return $classMetadataFactory;
78
    }
79
80 4
    private static function createPropertyInfo(): ?PropertyInfoExtractorInterface
81
    {
82 4
        $propertyInfo = null;
83
84 4
        if (class_exists(PropertyInfoExtractor::class)) {
85 4
            $phpDocExtractor = new PhpDocExtractor();
86 4
            $reflectionExtractor = new ReflectionExtractor();
87
88 4
            $listExtractors = [$reflectionExtractor];
89 4
            $typeExtractors = [$phpDocExtractor, $reflectionExtractor];
90 4
            $descriptionExtractors = [$phpDocExtractor];
91 4
            $accessExtractors = [$reflectionExtractor];
92 4
            $propertyInitializableExtractors = [$reflectionExtractor];
93
94 4
            $propertyInfo = new PropertyInfoExtractor(
95 4
                $listExtractors,
96 4
                $typeExtractors,
97 4
                $descriptionExtractors,
98 4
                $accessExtractors,
99 4
                $propertyInitializableExtractors
100
            );
101
        }
102
103 4
        return $propertyInfo;
104
    }
105
}
106