Passed
Push — master ( 17481c...d9f781 )
by Reyo
02:57
created

ProjectSubscriptionNormalizer   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 78
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
C denormalize() 0 32 10
A supportsDenormalization() 0 3 1
C normalize() 0 29 9
A supportsNormalization() 0 3 2
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\Exception\InvalidArgumentException;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
20
class ProjectSubscriptionNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
21
{
22
    use DenormalizerAwareTrait;
23
    use NormalizerAwareTrait;
24
25
    public function supportsDenormalization($data, $type, $format = null)
26
    {
27
        return $type === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectSubscription';
28
    }
29
30
    public function supportsNormalization($data, $format = null)
31
    {
32
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectSubscription';
33
    }
34
35
    public function denormalize($data, $class, $format = null, array $context = [])
36
    {
37
        if (!is_object($data)) {
38
            throw new InvalidArgumentException(sprintf('Given $data is not an object (%s given). We need an object in order to continue denormalize method.', gettype($data)));
39
        }
40
        $object = new \ConnectHolland\TimechimpBundle\Api\Model\ProjectSubscription();
41
        if (property_exists($data, 'id')) {
42
            $object->setId($data->{'id'});
43
        }
44
        if (property_exists($data, 'frequency')) {
45
            $object->setFrequency($data->{'frequency'});
46
        }
47
        if (property_exists($data, 'startDate')) {
48
            $object->setStartDate(\DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'startDate'}));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromForm...P', $data->'startDate') can also be of type false; however, parameter $startDate of ConnectHolland\Timechimp...ription::setStartDate() does only seem to accept DateTime, 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

48
            $object->setStartDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'startDate'}));
Loading history...
49
        }
50
        if (property_exists($data, 'endDate')) {
51
            $object->setEndDate(\DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'endDate'}));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromForm...:sP', $data->'endDate') can also be of type false; however, parameter $endDate of ConnectHolland\Timechimp...scription::setEndDate() does only seem to accept DateTime, 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->setEndDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data->{'endDate'}));
Loading history...
52
        }
53
        if (property_exists($data, 'description')) {
54
            $object->setDescription($data->{'description'});
55
        }
56
        if (property_exists($data, 'code')) {
57
            $object->setCode($data->{'code'});
58
        }
59
        if (property_exists($data, 'amount')) {
60
            $object->setAmount($data->{'amount'});
61
        }
62
        if (property_exists($data, 'rate')) {
63
            $object->setRate($data->{'rate'});
64
        }
65
66
        return $object;
67
    }
68
69
    public function normalize($object, $format = null, array $context = [])
70
    {
71
        $data = new \stdClass();
72
        if (null !== $object->getId()) {
73
            $data->{'id'} = $object->getId();
74
        }
75
        if (null !== $object->getFrequency()) {
76
            $data->{'frequency'} = $object->getFrequency();
77
        }
78
        if (null !== $object->getStartDate()) {
79
            $data->{'startDate'} = $object->getStartDate()->format('Y-m-d\\TH:i:sP');
80
        }
81
        if (null !== $object->getEndDate()) {
82
            $data->{'endDate'} = $object->getEndDate()->format('Y-m-d\\TH:i:sP');
83
        }
84
        if (null !== $object->getDescription()) {
85
            $data->{'description'} = $object->getDescription();
86
        }
87
        if (null !== $object->getCode()) {
88
            $data->{'code'} = $object->getCode();
89
        }
90
        if (null !== $object->getAmount()) {
91
            $data->{'amount'} = $object->getAmount();
92
        }
93
        if (null !== $object->getRate()) {
94
            $data->{'rate'} = $object->getRate();
95
        }
96
97
        return $data;
98
    }
99
}
100