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

DateHandler::serializeDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 4
crap 1
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