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

DateHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 0
cbo 1
dl 0
loc 41
ccs 4
cts 4
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serializeDateTime() 0 7 1
B deserializeDateTimeFromJson() 0 16 5
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