ListItemContactNormalizer   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsClass() 0 3 1
C denormalize() 0 41 12
A supportsDenormalization() 0 3 1
A supportsNormalization() 0 3 1
A normalize() 0 17 1
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