Completed
Push — 2.1 ( 63ded9...b14dac )
by Paweł
23s queued 11s
created

AppleNewsFormatSerializer   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 8
dl 0
loc 14
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2020 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\AppleNews\Serializer;
18
19
use DateTime;
20
use Doctrine\Common\Annotations\AnnotationReader;
21
use Symfony\Component\Serializer\Encoder\JsonEncoder;
22
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
23
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
24
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
25
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
26
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
27
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
28
use Symfony\Component\Serializer\Serializer;
29
30
final class AppleNewsFormatSerializer
31
{
32
    public function serialize($data): string
33
    {
34
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
35
        $metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
36
        $normalizer = new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter);
37
        $serializer = new Serializer([new DateTimeNormalizer(DateTime::ATOM), $normalizer], [new JsonEncoder()]);
38
39
        return $serializer->serialize($data, 'json', [
40
            AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
41
        ]);
42
    }
43
}
44