Completed
Push — master ( c473ed...e142c8 )
by Dragos
14:56
created

DateHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * Constructor.
14
     */
15 24
    public function __construct()
16 3
    {
17
        parent::__construct('Y-m-d H:i:s', 'Europe/Berlin', true);
18
    }
19 21
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type, Context $context)
24
    {
25
        // All dates send to Fastbill should be in Europe/Berlin timezone.
26
        $date->setTimezone(new \DateTimeZone('Europe/Berlin'));
27
28
        return parent::serializeDateTime($visitor, $date, $type, $context);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
35
    {
36
        // Handle empty or invalid date / datetime values.
37
        if ('' === $data || null === $data || strpos($data, '0000-00-00') !== false) {
38
            return null;
39
        }
40
41
        // We want to always show the response in the UTC timezone.
42
        $dateTime = parent::deserializeDateTimeFromJson($visitor, $data, $type);
43
44
        if ($dateTime instanceof \DateTime) {
45
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
46
        }
47
48
        return $dateTime;
49
    }
50
}
51