Completed
Push — master ( 2fa6a4...c473ed )
by
unknown
12:50
created

DateHandler::deserializeDateTimeFromJson()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 8.8571
cc 5
eloc 7
nc 3
nop 3
crap 30
1
<?php
2
3
namespace Speicher210\Fastbill\Api\Serializer\Handler;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\Handler\DateHandler as JMSDateHandler;
7
use JMS\Serializer\JsonDeserializationVisitor;
8
use JMS\Serializer\VisitorInterface;
9
10
class DateHandler extends JMSDateHandler
11
{
12
    /**
13 24
     * {@inheritdoc}
14
     */
15 24
    public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type, Context $context)
16 3
    {
17
        // All dates send to Fastbill should be in Europe/Berlin timezone.
18
        $date->setTimezone(new \DateTimeZone('Europe/Berlin'));
19 21
20
        return parent::serializeDateTime($visitor, $date, $type, $context);
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
27
    {
28
        if ('' === $data || null === $data || '0000-00-00 00:00:00' === $data) {
29
            return null;
30
        }
31
32
        // We want to always show the response in the UTC timezone.
33
        $dateTime = parent::deserializeDateTimeFromJson($visitor, $data, $type);
34
35
        if ($dateTime instanceof \DateTime) {
36
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
37
        }
38
39
        return $dateTime;
40
    }
41
}
42