Passed
Push — main ( 5b7f1c...98c11d )
by Vasil
03:17
created

SerializerFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 18
ccs 13
cts 13
cp 1
crap 2
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Speedy\Serializer;
6
7
use JMS\Serializer;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\SerializerInterface;
10
use VasilDakov\Speedy\Exception\ServiceNotCreatedException;
11
12
/**
13
 * Class SerializerFactory
14
 *
15
 * @author Vasil Dakov <[email protected]>
16
 * @copyright 2009-2022 Neutrino.bg
17
 * @version 1.0
18
 */
19
final class SerializerFactory
20
{
21
    /**
22
     * @return SerializerInterface
23
     */
24 27
    public function __invoke(): SerializerInterface
25
    {
26
        try {
27 27
            return Serializer\SerializerBuilder::create()
28 27
                ->setPropertyNamingStrategy(
29 27
                    new Serializer\Naming\SerializedNameAnnotationStrategy(
30 27
                        new Serializer\Naming\IdenticalPropertyNamingStrategy()
31 27
                    )
32 27
                )
33 27
                ->setSerializationContextFactory(function () {
34 8
                    return SerializationContext::create()
35 8
                        ->setSerializeNull(true);
36 27
                })
37 27
                ->build()
38 27
            ;
39
            // @codeCoverageIgnoreStart
40
        } catch (\Throwable $e) {
41
            throw new ServiceNotCreatedException();
42
        }
43
        // @codeCoverageIgnoreEnd
44
    }
45
}
46