Passed
Pull Request — master (#32)
by
unknown
18:56
created

ProductVariantResponseModelNormalizer   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 92
c 2
b 0
f 0
dl 0
loc 136
rs 6.96
wmc 53

4 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 49 10
A supportsNormalization() 0 3 2
F denormalize() 0 74 40
A supportsDenormalization() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ProductVariantResponseModelNormalizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ProductVariantResponseModelNormalizer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Starweb\Api\Generated\Normalizer;
4
5
use Jane\JsonSchemaRuntime\Reference;
6
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
7
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
9
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
12
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
13
class ProductVariantResponseModelNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
14
{
15
    use DenormalizerAwareTrait;
16
    use NormalizerAwareTrait;
17
    public function supportsDenormalization($data, $type, $format = null)
18
    {
19
        return $type === 'Starweb\\Api\\Generated\\Model\\ProductVariantResponseModel';
20
    }
21
    public function supportsNormalization($data, $format = null)
22
    {
23
        return is_object($data) && get_class($data) === 'Starweb\\Api\\Generated\\Model\\ProductVariantResponseModel';
24
    }
25
    public function denormalize($data, $class, $format = null, array $context = array())
26
    {
27
        if (!is_object($data)) {
28
            return null;
29
        }
30
        $object = new \Starweb\Api\Generated\Model\ProductVariantResponseModel();
31
        if (property_exists($data, 'sku') && $data->{'sku'} !== null) {
32
            $object->setSku($data->{'sku'});
33
        }
34
        if (property_exists($data, 'externalId') && $data->{'externalId'} !== null) {
35
            $object->setExternalId($data->{'externalId'});
36
        }
37
        if (property_exists($data, 'externalIdType') && $data->{'externalIdType'} !== null) {
38
            $object->setExternalIdType($data->{'externalIdType'});
39
        }
40
        if (property_exists($data, 'isActive') && $data->{'isActive'} !== null) {
41
            $object->setIsActive($data->{'isActive'});
42
        }
43
        if (property_exists($data, 'sortIndex') && $data->{'sortIndex'} !== null) {
44
            $object->setSortIndex($data->{'sortIndex'});
45
        }
46
        if (property_exists($data, 'stockStatusId') && $data->{'stockStatusId'} !== null) {
47
            $object->setStockStatusId($data->{'stockStatusId'});
48
        }
49
        if (property_exists($data, 'stockQuantity') && $data->{'stockQuantity'} !== null) {
50
            $object->setStockQuantity($data->{'stockQuantity'});
51
        }
52
        if (property_exists($data, 'weightInKg') && $data->{'weightInKg'} !== null) {
53
            $object->setWeightInKg($data->{'weightInKg'});
54
        }
55
        if (property_exists($data, 'costPrice') && $data->{'costPrice'} !== null) {
56
            $object->setCostPrice($data->{'costPrice'});
57
        }
58
        if (property_exists($data, 'ean') && $data->{'ean'} !== null) {
59
            $object->setEan($data->{'ean'});
60
        }
61
        if (property_exists($data, 'mpn') && $data->{'mpn'} !== null) {
62
            $object->setMpn($data->{'mpn'});
63
        }
64
        if (property_exists($data, 'imageFileId') && $data->{'imageFileId'} !== null) {
65
            $object->setImageFileId($data->{'imageFileId'});
66
        }
67
        if (property_exists($data, 'attributeValueLinks') && $data->{'attributeValueLinks'} !== null) {
68
            $values = array();
69
            foreach ($data->{'attributeValueLinks'} as $value) {
70
                $values[] = $value;
71
            }
72
            $object->setAttributeValueLinks($values);
73
        }
74
        if (property_exists($data, 'volumePricingInheritancePricelistIds') && $data->{'volumePricingInheritancePricelistIds'} !== null) {
75
            $values_1 = array();
76
            foreach ($data->{'volumePricingInheritancePricelistIds'} as $value_1) {
77
                $values_1[] = $value_1;
78
            }
79
            $object->setVolumePricingInheritancePricelistIds($values_1);
80
        }
81
        if (property_exists($data, 'variantId') && $data->{'variantId'} !== null) {
82
            $object->setVariantId($data->{'variantId'});
83
        }
84
        if (property_exists($data, 'prices') && $data->{'prices'} !== null) {
85
            $values_2 = array();
86
            foreach ($data->{'prices'} as $value_2) {
87
                $values_2[] = $this->denormalizer->denormalize($value_2, 'Starweb\\Api\\Generated\\Model\\ProductVariantPriceModel', 'json', $context);
88
            }
89
            $object->setPrices($values_2);
90
        }
91
        if (property_exists($data, 'attributeValues') && $data->{'attributeValues'} !== null) {
92
            $values_3 = array();
93
            foreach ($data->{'attributeValues'} as $value_3) {
94
                $values_3[] = $this->denormalizer->denormalize($value_3, 'Starweb\\Api\\Generated\\Model\\ProductVariantAttributeValueModel', 'json', $context);
95
            }
96
            $object->setAttributeValues($values_3);
97
        }
98
        return $object;
99
    }
100
    public function normalize($object, $format = null, array $context = array())
101
    {
102
        $data = new \stdClass();
103
        if (null !== $object->getSku()) {
104
            $data->{'sku'} = $object->getSku();
105
        }
106
        $data->{'externalId'} = $object->getExternalId();
107
        $data->{'externalIdType'} = $object->getExternalIdType();
108
        $data->{'isActive'} = $object->getIsActive();
109
        $data->{'sortIndex'} = $object->getSortIndex();
110
        $data->{'stockStatusId'} = $object->getStockStatusId();
111
        $data->{'stockQuantity'} = $object->getStockQuantity();
112
        $data->{'weightInKg'} = $object->getWeightInKg();
113
        $data->{'costPrice'} = $object->getCostPrice();
114
        $data->{'ean'} = $object->getEan();
115
        $data->{'mpn'} = $object->getMpn();
116
        $data->{'imageFileId'} = $object->getImageFileId();
117
        if (null !== $object->getAttributeValueLinks()) {
118
            $values = array();
119
            foreach ($object->getAttributeValueLinks() as $value) {
120
                $values[] = $value;
121
            }
122
            $data->{'attributeValueLinks'} = $values;
123
        }
124
        if (null !== $object->getVolumePricingInheritancePricelistIds()) {
125
            $values_1 = array();
126
            foreach ($object->getVolumePricingInheritancePricelistIds() as $value_1) {
127
                $values_1[] = $value_1;
128
            }
129
            $data->{'volumePricingInheritancePricelistIds'} = $values_1;
130
        }
131
        else {
132
            $data->{'volumePricingInheritancePricelistIds'} = null;
133
        }
134
        if (null !== $object->getPrices()) {
135
            $values_2 = array();
136
            foreach ($object->getPrices() as $value_2) {
137
                $values_2[] = $this->normalizer->normalize($value_2, 'json', $context);
138
            }
139
            $data->{'prices'} = $values_2;
140
        }
141
        if (null !== $object->getAttributeValues()) {
142
            $values_3 = array();
143
            foreach ($object->getAttributeValues() as $value_3) {
144
                $values_3[] = $this->normalizer->normalize($value_3, 'json', $context);
145
            }
146
            $data->{'attributeValues'} = $values_3;
147
        }
148
        return $data;
149
    }
150
}