Completed
Push — master ( 2a1a5c...73e6e2 )
by Dominik
14:26 queued 10:32
created

DateFieldDenormalizer::denormalizeField()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 3
nop 5
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
final class DateFieldDenormalizer implements FieldDenormalizerInterface
10
{
11
    /**
12
     * @var FieldDenormalizerInterface
13
     */
14
    private $fieldDenormalizer;
15
16
    /**
17
     * @param FieldDenormalizerInterface $fieldDenormalizer
18
     */
19 10
    public function __construct(FieldDenormalizerInterface $fieldDenormalizer)
20
    {
21 10
        $this->fieldDenormalizer = $fieldDenormalizer;
22 10
    }
23
24
    /**
25
     * @param string                       $path
26
     * @param object                       $object
27
     * @param mixed                        $value
28
     * @param DenormalizerContextInterface $context
29
     * @param DenormalizerInterface|null   $denormalizer
30
     *
31
     * @throws DeserializerRuntimeException
32
     */
33 10
    public function denormalizeField(
34
        string $path,
35
        $object,
36
        $value,
37
        DenormalizerContextInterface $context,
38
        DenormalizerInterface $denormalizer = null
39
    ) {
40 10
        if (null === $value || !is_scalar($value) || '' === $trimmedValue = trim((string) $value)) {
41 5
            $this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer);
42
43 5
            return;
44
        }
45
46
        try {
47 5
            $value = new \DateTime($trimmedValue);
48 4
        } catch (\Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
49
        }
50
51 5
        $this->fieldDenormalizer->denormalizeField($path, $object, $value, $context, $denormalizer);
52 5
    }
53
}
54