DateHandler   A
last analyzed

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 14
cts 14
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
     * Constructor.
14
     */
15 120
    public function __construct()
16
    {
17 120
        parent::__construct('Y-m-d H:i:s', 'Europe/Berlin', true);
18 120
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 15
    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 15
        $date->setTimezone(new \DateTimeZone('Europe/Berlin'));
27
28 15
        return parent::serializeDateTime($visitor, $date, $type, $context);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 33
    public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type)
35
    {
36
        // Handle empty or invalid date / datetime values.
37 33
        if ('' === $data || null === $data || strpos($data, '0000-00-00') !== false) {
38 12
            return null;
39
        }
40
41
        // We want to always show the response in the UTC timezone.
42 27
        $dateTime = parent::deserializeDateTimeFromJson($visitor, $data, $type);
43
44 27
        if ($dateTime instanceof \DateTime) {
45 27
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
46 27
        }
47
48 27
        return $dateTime;
49
    }
50
}
51