Passed
Push — master ( 31fd3d...9d78ee )
by Agaletskiy
52s queued 14s
created

createDateTimeNormalizer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\OmsClient\Impl\Serializer;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Lamoda\OmsClient\Serializer\SerializerInterface;
9
use Symfony\Component\Serializer\Encoder\JsonEncoder;
10
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
11
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
12
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
13
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14
use Symfony\Component\Serializer\Serializer;
15
16
final class SymfonySerializerAdapterFactory
17
{
18
    public const SYMFONY_VERSION_LESS_THAN_42 = 'symfony_version_less_42';
19
    public const SYMFONY_VERSION_GREATER_OR_EQUAL_42 = 'symfony_version_greater_or_equal_42';
20
21 1
    public static function create(string $symfonyVersion = self::SYMFONY_VERSION_LESS_THAN_42): SerializerInterface
22
    {
23 1
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
24
25 1
        $symfonySerializer = new Serializer([
26 1
            self::createDateTimeNormalizer($symfonyVersion),
27 1
            new ObjectNormalizer($classMetadataFactory),
28
        ], [
29 1
            new JsonEncoder(),
30
        ]);
31
32 1
        return new SymfonySerializerAdapter($symfonySerializer);
33
    }
34
35
    private static function createDateTimeNormalizer(string $symfonyVersion): DateTimeNormalizer
36
    {
37
        // @codeCoverageIgnoreStart
38
        switch ($symfonyVersion) {
39
            case self::SYMFONY_VERSION_LESS_THAN_42:
40
                return new DateTimeNormalizer('Y-m-d');
0 ignored issues
show
Documentation introduced by
'Y-m-d' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            case self::SYMFONY_VERSION_GREATER_OR_EQUAL_42:
42
                return new DateTimeNormalizer([
43
                    'datetime_format' => 'Y-m-d'
44
                ]);
45
        }
46
47
        throw new \InvalidArgumentException(sprintf(
48
            'Given $symfonyVersion is invalid - "%s"',
49
            $symfonyVersion
50
        ));
51
        // @codeCoverageIgnoreEnd
52
    }
53
}
54