ListItemContactNormalizer::denormalize()   C
last analyzed

Complexity

Conditions 12
Paths 4

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 41
rs 6.9666

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
namespace AlexCk\MailchimpBundle\Normalize\MailChimp;
6
7
use AlexCk\MailchimpBundle\Model\MailChimp\ListItemContact;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
11
class ListItemContactNormalizer implements NormalizerInterface, DenormalizerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function normalize($object, $format = null, array $context = [])
17
    {
18
        /** @var ListItemContact $item */
19
        $item = &$object;
20
21
        $data = [
22
            'company' => $item->getCompany(),
23
            'address1' => $item->getAddress1(),
24
            'address2' => $item->getAddress2(),
25
            'city' => $item->getCity(),
26
            'zip' => $item->getZip(),
27
            'country' => $item->getCountry(),
28
            'phone' => $item->getPhone(),
29
            'state' => $item->getState(),
30
        ];
31
32
        return $data;
33
    }
34
35
    public function denormalize($data, $class, $format = null, array $context = [])
36
    {
37
        if (is_string($data)) {
38
            $data = json_decode($data, true);
39
        }
40
41
        /** @var ListItemContact $item */
42
        $item = new $class();
43
44
        if ($data) {
45
            foreach ($data as $fieldName => $fieldValue) {
46
                switch ($fieldName) {
47
                    case 'company':
48
                        $item->setCompany($fieldValue);
49
                        break;
50
                    case 'address1':
51
                        $item->setAddress1($fieldValue);
52
                        break;
53
                    case 'address2':
54
                        $item->setAddress2($fieldValue);
55
                        break;
56
                    case 'city':
57
                        $item->setCity($fieldValue);
58
                        break;
59
                    case 'zip':
60
                        $item->setZip($fieldValue);
61
                        break;
62
                    case 'country':
63
                        $item->setCountry($fieldValue);
64
                        break;
65
                    case 'phone':
66
                        $item->setPhone($fieldValue);
67
                        break;
68
                    case 'state':
69
                        $item->setState($fieldValue);
70
                        break;
71
                }
72
            }
73
        }
74
75
        return $item;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function supportsDenormalization($data, $type, $format = null)
82
    {
83
        return ListItemContact::class == $type;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function supportsNormalization($data, $format = null)
90
    {
91
        return $this->supportsClass($data);
92
    }
93
94
    private function supportsClass($data)
95
    {
96
        return $data instanceof ListItemContact;
97
    }
98
}
99