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

ProductVariantPutRequestModelNormalizer   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 108
rs 9.1199
wmc 41

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 3 2
B normalize() 0 38 6
F denormalize() 0 57 32
A supportsDenormalization() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ProductVariantPutRequestModelNormalizer 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 ProductVariantPutRequestModelNormalizer, 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 ProductVariantPutRequestModelNormalizer 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\\ProductVariantPutRequestModel';
20
    }
21
    public function supportsNormalization($data, $format = null)
22
    {
23
        return is_object($data) && get_class($data) === 'Starweb\\Api\\Generated\\Model\\ProductVariantPutRequestModel';
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\ProductVariantPutRequestModel();
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
        return $object;
82
    }
83
    public function normalize($object, $format = null, array $context = array())
84
    {
85
        $data = new \stdClass();
86
        if (null !== $object->getSku()) {
87
            $data->{'sku'} = $object->getSku();
88
        }
89
        $data->{'externalId'} = $object->getExternalId();
90
        $data->{'externalIdType'} = $object->getExternalIdType();
91
        $data->{'isActive'} = $object->getIsActive();
92
        $data->{'sortIndex'} = $object->getSortIndex();
93
        $data->{'stockStatusId'} = $object->getStockStatusId();
94
        $data->{'stockQuantity'} = $object->getStockQuantity();
95
        $data->{'weightInKg'} = $object->getWeightInKg();
96
        $data->{'costPrice'} = $object->getCostPrice();
97
        $data->{'ean'} = $object->getEan();
98
        $data->{'mpn'} = $object->getMpn();
99
        $data->{'imageFileId'} = $object->getImageFileId();
100
        if (null !== $object->getAttributeValueLinks()) {
101
            $values = array();
102
            foreach ($object->getAttributeValueLinks() as $value) {
103
                $values[] = $value;
104
            }
105
            $data->{'attributeValueLinks'} = $values;
106
        }
107
        else {
108
            $data->{'attributeValueLinks'} = null;
109
        }
110
        if (null !== $object->getVolumePricingInheritancePricelistIds()) {
111
            $values_1 = array();
112
            foreach ($object->getVolumePricingInheritancePricelistIds() as $value_1) {
113
                $values_1[] = $value_1;
114
            }
115
            $data->{'volumePricingInheritancePricelistIds'} = $values_1;
116
        }
117
        else {
118
            $data->{'volumePricingInheritancePricelistIds'} = null;
119
        }
120
        return $data;
121
    }
122
}