ListItemCampaignDefaultsNormalizer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 70
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 1
A supportsNormalization() 0 3 1
A normalize() 0 13 1
A supportsClass() 0 3 1
B denormalize() 0 29 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlexCk\MailchimpBundle\Normalize\MailChimp;
6
7
use AlexCk\MailchimpBundle\Model\MailChimp\ListItemCampaignDefaults;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
11
class ListItemCampaignDefaultsNormalizer implements NormalizerInterface, DenormalizerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function normalize($object, $format = null, array $context = [])
17
    {
18
        /** @var ListItemCampaignDefaults $item */
19
        $item = &$object;
20
21
        $data = [
22
            'from_name' => $item->getFromName(),
23
            'from_email' => $item->getFromEmail(),
24
            'subject' => $item->getSubject(),
25
            'language' => $item->getLanguage(),
26
        ];
27
28
        return $data;
29
    }
30
31
    public function denormalize($data, $class, $format = null, array $context = [])
32
    {
33
        if (is_string($data)) {
34
            $data = json_decode($data, true);
35
        }
36
37
        /** @var ListItemCampaignDefaults $item */
38
        $item = new $class();
39
40
        if ($data) {
41
            foreach ($data as $fieldName => $fieldValue) {
42
                switch ($fieldName) {
43
                    case 'from_name':
44
                        $item->setFromName($fieldValue);
45
                        break;
46
                    case 'from_email':
47
                        $item->setFromEmail($fieldValue);
48
                        break;
49
                    case 'subject':
50
                        $item->setSubject($fieldValue);
51
                        break;
52
                    case 'language':
53
                        $item->setLanguage($fieldValue);
54
                        break;
55
                }
56
            }
57
        }
58
59
        return $item;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supportsDenormalization($data, $type, $format = null)
66
    {
67
        return ListItemCampaignDefaults::class == $type;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsNormalization($data, $format = null)
74
    {
75
        return $this->supportsClass($data);
76
    }
77
78
    private function supportsClass($data)
79
    {
80
        return $data instanceof ListItemCampaignDefaults;
81
    }
82
}
83