Completed
Push — master ( 09365e...33984b )
by Asmir
03:20
created

XmlSchemaDateHandler::deserializeDateIntervalXml()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 3
eloc 5
nc 2
nop 3
crap 12
1
<?php
2
namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler;
3
4
use JMS\Serializer\Context;
5
use JMS\Serializer\GraphNavigator;
6
use JMS\Serializer\Handler\SubscribingHandlerInterface;
7
use JMS\Serializer\XmlDeserializationVisitor;
8
use JMS\Serializer\XmlSerializationVisitor;
9
use RuntimeException;
10
11
class XmlSchemaDateHandler implements SubscribingHandlerInterface
12
{
13
14
    protected $defaultTimezone;
15
16
    public static function getSubscribingMethods()
17
    {
18
        return array(
19
            array(
20
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
21
                'format' => 'xml',
22
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime',
23
                'method' => 'deserializeDateTime'
24
            ),
25
            array(
26
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
27
                'format' => 'xml',
28
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\DateTime',
29
                'method' => 'serializeDateTime'
30
            ),
31
            array(
32
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
33
                'format' => 'xml',
34
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
35
                'method' => 'deserializeTime'
36
            ),
37
            array(
38
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
39
                'format' => 'xml',
40
                'type' => 'GoetasWebservices\Xsd\XsdToPhp\XMLSchema\Time',
41
                'method' => 'serializeTime'
42
            ),
43
            array(
44
                'type' => 'DateInterval',
45
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
46
                'format' => 'xml',
47
                'method' => 'deserializeDateIntervalXml',
48
            ),
49
        );
50
    }
51
52 5
    public function __construct($defaultTimezone = 'UTC')
53
    {
54 5
        $this->defaultTimezone = new \DateTimeZone($defaultTimezone);
55
56 5
    }
57
58
    public function deserializeDateIntervalXml(XmlDeserializationVisitor $visitor, $data, array $type){
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
        $attributes = $data->attributes('xsi', true);
60
        if (isset($attributes['nil'][0]) && (string) $attributes['nil'][0] === 'true') {
61
            return null;
62
        }
63
        return new \DateInterval((string)$data);
64
    }
65
66 5
    public function serializeDateTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
67
    {
68
69 5
        $v = $date->format(\DateTime::W3C);
70 5
        if (substr($v, -5) == "00:00") {
71 4
            $v = substr($v, 0, -6);
72 4
        }
73 5
        return $visitor->visitSimpleString($v, $type, $context);
74
    }
75
76 View Code Duplication
    public function deserializeDateTime(XmlDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $attributes = $data->attributes('xsi', true);
79
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
80
            return null;
81
        }
82
83
        return $this->parseDateTime($data, $type);
84
85
    }
86
87
    public function serializeTime(XmlSerializationVisitor $visitor, \DateTime $date, array $type, Context $context)
88
    {
89
        $v = $date->format('H:i:s');
90
        if ($date->getTimezone()->getOffset($date) !== $this->defaultTimezone->getOffset($date)) {
91
            $v .= $date->format('P');
92
        }
93
        return $visitor->visitSimpleString($v, $type, $context);
94
    }
95
96 View Code Duplication
    public function deserializeTime(XmlDeserializationVisitor $visitor, $data, array $type)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $attributes = $data->attributes('xsi', true);
99
        if (isset($attributes['nil'][0]) && (string)$attributes['nil'][0] === 'true') {
100
            return null;
101
        }
102
103
        $data = (string)$data;
104
105
        return new \DateTime($data, $this->defaultTimezone);
106
    }
107
108
    private function parseDateTime($data, array $type)
109
    {
110
        $timezone = isset($type['params'][1]) ? new \DateTimeZone($type['params'][1]) : $this->defaultTimezone;
111
        $datetime = new \DateTime((string)$data, $timezone);
112
        if (false === $datetime) {
113
            throw new RuntimeException(sprintf('Invalid datetime "%s", expected valid XML Schema dateTime string.', $data));
114
        }
115
116
        return $datetime;
117
    }
118
}
119
120