Completed
Pull Request — master (#2)
by Agaletskiy
03:05
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 InvalidArgumentException;
9
use Lamoda\OmsClient\Serializer\SerializerInterface;
10
use Symfony\Component\Serializer\Encoder\JsonEncoder;
11
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
12
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
13
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
14
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
15
use Symfony\Component\Serializer\Serializer;
16
17
final class SymfonySerializerAdapterFactory
18
{
19
    public const SYMFONY_VERSION_LESS_THAN_42 = 'symfony_version_less_42';
20
    public const SYMFONY_VERSION_GREATER_OR_EQUAL_42 = 'symfony_version_greater_or_equal_42';
21
22 1
    public static function create(string $symfonyVersion = self::SYMFONY_VERSION_LESS_THAN_42): SerializerInterface
23
    {
24 1
        @trigger_error('Method SymfonySerializerAdapterFactory::create() is deprecated and left only for compatibility with Symfony versions prior 4.2. Use ',
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
25 1
            E_USER_DEPRECATED);
26
27 1
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
28
29 1
        $symfonySerializer = new Serializer([
30 1
            self::createDateTimeNormalizer($symfonyVersion),
31 1
            new ObjectNormalizer($classMetadataFactory),
32
        ], [
33 1
            new JsonEncoder(),
34
        ]);
35
36 1
        return new SymfonySerializerAdapter($symfonySerializer);
37
    }
38
39
    private static function createDateTimeNormalizer(string $symfonyVersion): DateTimeNormalizer
40
    {
41
        // @codeCoverageIgnoreStart
42
        switch ($symfonyVersion) {
43
            case self::SYMFONY_VERSION_LESS_THAN_42:
44
                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...
45
            case self::SYMFONY_VERSION_GREATER_OR_EQUAL_42:
46
                new DateTimeNormalizer([
47
                    'datetime_format' => 'Y-m-d',
48
                ]);
49
        }
50
51
        throw new InvalidArgumentException(sprintf(
52
            'Given $symfonyVersion is invalid - "%s"',
53
            $symfonyVersion
54
        ));
55
        // @codeCoverageIgnoreEnd
56
    }
57
}
58