SerializerFactory::__invoke()   A
last analyzed

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
 *
18
 * @version 1.0
19
 */
20
final class SerializerFactory
21
{
22 39
    public function __invoke(): SerializerInterface
23
    {
24
        try {
25 39
            return Serializer\SerializerBuilder::create()
26 39
                ->setPropertyNamingStrategy(
27 39
                    new Serializer\Naming\SerializedNameAnnotationStrategy(
28 39
                        new Serializer\Naming\IdenticalPropertyNamingStrategy()
29 39
                    )
30 39
                )
31 39
                ->setSerializationContextFactory(function () {
32 8
                    return SerializationContext::create()
33 8
                        ->setSerializeNull(true);
34 39
                })
35 39
                ->build()
36 39
            ;
37
            // @codeCoverageIgnoreStart
38
        } catch (\Throwable $e) {
39
            throw new ServiceNotCreatedException();
40
        }
41
        // @codeCoverageIgnoreEnd
42
    }
43
}
44