sanitizeDateTime()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 9
cp 0.7778
rs 9.9666
cc 4
nc 5
nop 1
crap 4.1755
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace DMT\Insolvency\Soap\Serializer;
4
5
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
6
use JMS\Serializer\EventDispatcher\PreDeserializeEvent;
7
use JMS\Serializer\Exception\RuntimeException;
8
9
/**
10
 * Class SoapDateTimeSanitizerEventSubscriber
11
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
12
class SoapDateTimeSanitizerEventSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
17 12
    public static function getSubscribedEvents()
18
    {
19
        return [
20
            [
21 12
                'event' => 'serializer.pre_deserialize',
22
                'method' => 'sanitizeDateTime',
23
                'format' => 'soap',
24
            ]
25
        ];
26
    }
27
28
    /**
29
     * Fix the datetime format, before it is deserialized.
30
     *
31
     * @param PreDeserializeEvent $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @throws RuntimeException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
33
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
34 13
    public function sanitizeDateTime(PreDeserializeEvent $event)
35
    {
36 13
        if ($event->getType()['name'] !== 'DateTime' || empty($event->getData())) {
37 11
            return;
38
        }
39
40
        try {
41 13
            $dateTime = new \DateTime($event->getData()[0]);
42
43 13
            $element = simplexml_load_string(
44 13
                sprintf('<x attr="%s"/>', $dateTime->format($event->getType()['params'][0]))
45
            );
46
47 13
            $event->setData($element->attributes()['attr']);
48
        } catch (\Exception $exception) {
49
            throw new RuntimeException(sprintf('"%s" is not a datetime', $event->getData()[0]));
50
        }
51
    }
52
}