Passed
Push — master ( 9db3d0...354d5b )
by Reyo
02:53
created

ProjectSubscriptionNormalizer::denormalize()   F

Complexity

Conditions 34
Paths 6562

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1190

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 34
eloc 36
c 1
b 0
f 0
nc 6562
nop 4
dl 0
loc 48
ccs 0
cts 47
cp 0
crap 1190
rs 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the timechimp bundle package.
7
 * (c) Connect Holland.
8
 */
9
10
namespace ConnectHolland\TimechimpBundle\Api\Normalizer;
11
12
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
19
class ProjectSubscriptionNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
20
{
21
    use DenormalizerAwareTrait;
22
    use NormalizerAwareTrait;
23
24
    public function supportsDenormalization($data, $type, $format = null)
25
    {
26
        return $type === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectSubscription';
27
    }
28
29
    public function supportsNormalization($data, $format = null)
30
    {
31
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectSubscription';
32
    }
33
34
    public function denormalize($data, $class, $format = null, array $context = [])
35
    {
36
        if (!is_object($data)) {
37
            return null;
38
        }
39
        $object = new \ConnectHolland\TimechimpBundle\Api\Model\ProjectSubscription();
40
        if (property_exists($data, 'id') && $data->{'id'} !== null) {
41
            $object->setId($data->{'id'});
42
        } elseif (property_exists($data, 'id') && $data->{'id'} === null) {
43
            $object->setId(null);
44
        }
45
        if (property_exists($data, 'frequency') && $data->{'frequency'} !== null) {
46
            $object->setFrequency($data->{'frequency'});
47
        } elseif (property_exists($data, 'frequency') && $data->{'frequency'} === null) {
48
            $object->setFrequency(null);
49
        }
50
        if (property_exists($data, 'startDate') && $data->{'startDate'} !== null) {
51
            $object->setStartDate(\DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'startDate'}));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromForm...s', $data->'startDate') can also be of type false; however, parameter $startDate of ConnectHolland\Timechimp...ription::setStartDate() does only seem to accept DateTime|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            $object->setStartDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'startDate'}));
Loading history...
52
        } elseif (property_exists($data, 'startDate') && $data->{'startDate'} === null) {
53
            $object->setStartDate(null);
54
        }
55
        if (property_exists($data, 'endDate') && $data->{'endDate'} !== null) {
56
            $object->setEndDate(\DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'endDate'}));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromForm...i:s', $data->'endDate') can also be of type false; however, parameter $endDate of ConnectHolland\Timechimp...scription::setEndDate() does only seem to accept DateTime|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $object->setEndDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'endDate'}));
Loading history...
57
        } elseif (property_exists($data, 'endDate') && $data->{'endDate'} === null) {
58
            $object->setEndDate(null);
59
        }
60
        if (property_exists($data, 'description') && $data->{'description'} !== null) {
61
            $object->setDescription($data->{'description'});
62
        } elseif (property_exists($data, 'description') && $data->{'description'} === null) {
63
            $object->setDescription(null);
64
        }
65
        if (property_exists($data, 'code') && $data->{'code'} !== null) {
66
            $object->setCode($data->{'code'});
67
        } elseif (property_exists($data, 'code') && $data->{'code'} === null) {
68
            $object->setCode(null);
69
        }
70
        if (property_exists($data, 'amount') && $data->{'amount'} !== null) {
71
            $object->setAmount($data->{'amount'});
72
        } elseif (property_exists($data, 'amount') && $data->{'amount'} === null) {
73
            $object->setAmount(null);
74
        }
75
        if (property_exists($data, 'rate') && $data->{'rate'} !== null) {
76
            $object->setRate($data->{'rate'});
77
        } elseif (property_exists($data, 'rate') && $data->{'rate'} === null) {
78
            $object->setRate(null);
79
        }
80
81
        return $object;
82
    }
83
84
    public function normalize($object, $format = null, array $context = [])
85
    {
86
        $data = new \stdClass();
87
        if (null !== $object->getId()) {
88
            $data->{'id'} = $object->getId();
89
        } else {
90
            $data->{'id'} = null;
91
        }
92
        if (null !== $object->getFrequency()) {
93
            $data->{'frequency'} = $object->getFrequency();
94
        } else {
95
            $data->{'frequency'} = null;
96
        }
97
        if (null !== $object->getStartDate()) {
98
            $data->{'startDate'} = $object->getStartDate()->format('Y-m-d\\TH:i:s');
99
        } else {
100
            $data->{'startDate'} = null;
101
        }
102
        if (null !== $object->getEndDate()) {
103
            $data->{'endDate'} = $object->getEndDate()->format('Y-m-d\\TH:i:s');
104
        } else {
105
            $data->{'endDate'} = null;
106
        }
107
        if (null !== $object->getDescription()) {
108
            $data->{'description'} = $object->getDescription();
109
        } else {
110
            $data->{'description'} = null;
111
        }
112
        if (null !== $object->getCode()) {
113
            $data->{'code'} = $object->getCode();
114
        } else {
115
            $data->{'code'} = null;
116
        }
117
        if (null !== $object->getAmount()) {
118
            $data->{'amount'} = $object->getAmount();
119
        } else {
120
            $data->{'amount'} = null;
121
        }
122
        if (null !== $object->getRate()) {
123
            $data->{'rate'} = $object->getRate();
124
        } else {
125
            $data->{'rate'} = null;
126
        }
127
128
        return $data;
129
    }
130
}
131