ProductModelNormalizer   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 104
c 1
b 0
f 1
dl 0
loc 160
rs 8.5599
wmc 48

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 1
A supportsNormalization() 0 3 1
F denormalize() 0 80 26
F normalize() 0 67 20

How to fix   Complexity   

Complex Class

Complex classes like ProductModelNormalizer 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 ProductModelNormalizer, 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 ProductModelNormalizer 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\\ProductModel';
20
    }
21
    public function supportsNormalization($data, $format = null)
22
    {
23
        return get_class($data) === 'Starweb\\Api\\Generated\\Model\\ProductModel';
24
    }
25
    public function denormalize($data, $class, $format = null, array $context = array())
26
    {
27
        if (!is_object($data)) {
28
            throw new InvalidArgumentException();
29
        }
30
        $object = new \Starweb\Api\Generated\Model\ProductModel();
31
        if (property_exists($data, 'productId')) {
32
            $object->setProductId($data->{'productId'});
33
        }
34
        if (property_exists($data, 'createdAt')) {
35
            $object->setCreatedAt($data->{'createdAt'});
36
        }
37
        if (property_exists($data, 'defaultVatRate')) {
38
            $object->setDefaultVatRate($data->{'defaultVatRate'});
39
        }
40
        if (property_exists($data, 'visibility')) {
41
            $object->setVisibility($data->{'visibility'});
42
        }
43
        if (property_exists($data, 'visibilityPricelistIds')) {
44
            $values = array();
45
            foreach ($data->{'visibilityPricelistIds'} as $value) {
46
                $values[] = $value;
47
            }
48
            $object->setVisibilityPricelistIds($values);
49
        }
50
        if (property_exists($data, 'moreInfoUrl')) {
51
            $object->setMoreInfoUrl($data->{'moreInfoUrl'});
52
        }
53
        if (property_exists($data, 'manufacturerId')) {
54
            $object->setManufacturerId($data->{'manufacturerId'});
55
        }
56
        if (property_exists($data, 'unitId')) {
57
            $object->setUnitId($data->{'unitId'});
58
        }
59
        if (property_exists($data, 'sortIndex')) {
60
            $object->setSortIndex($data->{'sortIndex'});
61
        }
62
        if (property_exists($data, 'type')) {
63
            $object->setType($data->{'type'});
64
        }
65
        if (property_exists($data, 'isBackInStockWatchable')) {
66
            $object->setIsBackInStockWatchable($data->{'isBackInStockWatchable'});
67
        }
68
        if (property_exists($data, 'bundleUseManualPrice')) {
69
            $object->setBundleUseManualPrice($data->{'bundleUseManualPrice'});
70
        }
71
        if (property_exists($data, 'accounting')) {
72
            $object->setAccounting($data->{'accounting'});
73
        }
74
        if (property_exists($data, 'hasSeveralVariants')) {
75
            $object->setHasSeveralVariants($data->{'hasSeveralVariants'});
76
        }
77
        if (property_exists($data, 'modifiedAt')) {
78
            $object->setModifiedAt($data->{'modifiedAt'});
79
        }
80
        if (property_exists($data, 'variants')) {
81
            $object->setVariants($this->denormalizer->denormalize($data->{'variants'}, 'Starweb\\Api\\Generated\\Model\\ProductVariantModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $variants of Starweb\Api\Generated\Mo...uctModel::setVariants() does only seem to accept Starweb\Api\Generated\Mo...tVariantModelCollection, 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

81
            $object->setVariants(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'variants'}, 'Starweb\\Api\\Generated\\Model\\ProductVariantModelCollection', 'json', $context));
Loading history...
82
        }
83
        if (property_exists($data, 'bundledProducts')) {
84
            $object->setBundledProducts($this->denormalizer->denormalize($data->{'bundledProducts'}, 'Starweb\\Api\\Generated\\Model\\BundledProductsModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $bundledProducts of Starweb\Api\Generated\Mo...l::setBundledProducts() does only seem to accept Starweb\Api\Generated\Mo...ProductsModelCollection, 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

84
            $object->setBundledProducts(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'bundledProducts'}, 'Starweb\\Api\\Generated\\Model\\BundledProductsModelCollection', 'json', $context));
Loading history...
85
        }
86
        if (property_exists($data, 'mediaFiles')) {
87
            $object->setMediaFiles($this->denormalizer->denormalize($data->{'mediaFiles'}, 'Starweb\\Api\\Generated\\Model\\ProductMediaFileLinkModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $mediaFiles of Starweb\Api\Generated\Mo...tModel::setMediaFiles() does only seem to accept Starweb\Api\Generated\Mo...FileLinkModelCollection, 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

87
            $object->setMediaFiles(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'mediaFiles'}, 'Starweb\\Api\\Generated\\Model\\ProductMediaFileLinkModelCollection', 'json', $context));
Loading history...
88
        }
89
        if (property_exists($data, 'languages')) {
90
            $object->setLanguages($this->denormalizer->denormalize($data->{'languages'}, 'Starweb\\Api\\Generated\\Model\\ProductModelLanguages', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ges', 'json', $context) can also be of type array; however, parameter $languages of Starweb\Api\Generated\Mo...ctModel::setLanguages() does only seem to accept Starweb\Api\Generated\Model\ProductModelLanguages, 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

90
            $object->setLanguages(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'languages'}, 'Starweb\\Api\\Generated\\Model\\ProductModelLanguages', 'json', $context));
Loading history...
91
        }
92
        if (property_exists($data, 'vatRates')) {
93
            $object->setVatRates($this->denormalizer->denormalize($data->{'vatRates'}, 'Starweb\\Api\\Generated\\Model\\ProductVatRateModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $vatRates of Starweb\Api\Generated\Mo...uctModel::setVatRates() does only seem to accept Starweb\Api\Generated\Mo...tVatRateModelCollection, 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

93
            $object->setVatRates(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'vatRates'}, 'Starweb\\Api\\Generated\\Model\\ProductVatRateModelCollection', 'json', $context));
Loading history...
94
        }
95
        if (property_exists($data, 'categories')) {
96
            $object->setCategories($this->denormalizer->denormalize($data->{'categories'}, 'Starweb\\Api\\Generated\\Model\\ProductCategoryLinkModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $categories of Starweb\Api\Generated\Mo...tModel::setCategories() does only seem to accept Starweb\Api\Generated\Mo...goryLinkModelCollection, 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

96
            $object->setCategories(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'categories'}, 'Starweb\\Api\\Generated\\Model\\ProductCategoryLinkModelCollection', 'json', $context));
Loading history...
97
        }
98
        if (property_exists($data, 'unit')) {
99
            $object->setUnit($this->denormalizer->denormalize($data->{'unit'}, 'Starweb\\Api\\Generated\\Model\\ProductUnitModelItem', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...tem', 'json', $context) can also be of type array; however, parameter $unit of Starweb\Api\Generated\Mo...ProductModel::setUnit() does only seem to accept Starweb\Api\Generated\Model\ProductUnitModelItem, 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

99
            $object->setUnit(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'unit'}, 'Starweb\\Api\\Generated\\Model\\ProductUnitModelItem', 'json', $context));
Loading history...
100
        }
101
        if (property_exists($data, 'metaData')) {
102
            $object->setMetaData($this->denormalizer->denormalize($data->{'metaData'}, 'Starweb\\Api\\Generated\\Model\\ProductMetaDataModelCollection', 'json', $context));
0 ignored issues
show
Bug introduced by
It seems like $this->denormalizer->den...ion', 'json', $context) can also be of type array; however, parameter $metaData of Starweb\Api\Generated\Mo...uctModel::setMetaData() does only seem to accept Starweb\Api\Generated\Mo...MetaDataModelCollection, 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

102
            $object->setMetaData(/** @scrutinizer ignore-type */ $this->denormalizer->denormalize($data->{'metaData'}, 'Starweb\\Api\\Generated\\Model\\ProductMetaDataModelCollection', 'json', $context));
Loading history...
103
        }
104
        return $object;
105
    }
106
    public function normalize($object, $format = null, array $context = array())
107
    {
108
        $data = new \stdClass();
109
        if (null !== $object->getProductId()) {
110
            $data->{'productId'} = $object->getProductId();
111
        }
112
        if (null !== $object->getCreatedAt()) {
113
            $data->{'createdAt'} = $object->getCreatedAt();
114
        }
115
        $data->{'defaultVatRate'} = $object->getDefaultVatRate();
116
        if (null !== $object->getVisibility()) {
117
            $data->{'visibility'} = $object->getVisibility();
118
        }
119
        if (null !== $object->getVisibilityPricelistIds()) {
120
            $values = array();
121
            foreach ($object->getVisibilityPricelistIds() as $value) {
122
                $values[] = $value;
123
            }
124
            $data->{'visibilityPricelistIds'} = $values;
125
        }
126
        if (null !== $object->getMoreInfoUrl()) {
127
            $data->{'moreInfoUrl'} = $object->getMoreInfoUrl();
128
        }
129
        $data->{'manufacturerId'} = $object->getManufacturerId();
130
        $data->{'unitId'} = $object->getUnitId();
131
        $data->{'sortIndex'} = $object->getSortIndex();
132
        if (null !== $object->getType()) {
133
            $data->{'type'} = $object->getType();
134
        }
135
        if (null !== $object->getIsBackInStockWatchable()) {
136
            $data->{'isBackInStockWatchable'} = $object->getIsBackInStockWatchable();
137
        }
138
        if (null !== $object->getBundleUseManualPrice()) {
139
            $data->{'bundleUseManualPrice'} = $object->getBundleUseManualPrice();
140
        }
141
        $data->{'accounting'} = $object->getAccounting();
142
        if (null !== $object->getHasSeveralVariants()) {
143
            $data->{'hasSeveralVariants'} = $object->getHasSeveralVariants();
144
        }
145
        if (null !== $object->getModifiedAt()) {
146
            $data->{'modifiedAt'} = $object->getModifiedAt();
147
        }
148
        if (null !== $object->getVariants()) {
149
            $data->{'variants'} = $this->normalizer->normalize($object->getVariants(), 'json', $context);
150
        }
151
        if (null !== $object->getBundledProducts()) {
152
            $data->{'bundledProducts'} = $this->normalizer->normalize($object->getBundledProducts(), 'json', $context);
153
        }
154
        if (null !== $object->getMediaFiles()) {
155
            $data->{'mediaFiles'} = $this->normalizer->normalize($object->getMediaFiles(), 'json', $context);
156
        }
157
        if (null !== $object->getLanguages()) {
158
            $data->{'languages'} = $this->normalizer->normalize($object->getLanguages(), 'json', $context);
159
        }
160
        if (null !== $object->getVatRates()) {
161
            $data->{'vatRates'} = $this->normalizer->normalize($object->getVatRates(), 'json', $context);
162
        }
163
        if (null !== $object->getCategories()) {
164
            $data->{'categories'} = $this->normalizer->normalize($object->getCategories(), 'json', $context);
165
        }
166
        if (null !== $object->getUnit()) {
167
            $data->{'unit'} = $this->normalizer->normalize($object->getUnit(), 'json', $context);
168
        }
169
        if (null !== $object->getMetaData()) {
170
            $data->{'metaData'} = $this->normalizer->normalize($object->getMetaData(), 'json', $context);
171
        }
172
        return $data;
173
    }
174
}