ListItemNormalizer::denormalize()   B
last analyzed

Complexity

Conditions 10
Paths 4

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 35
rs 7.6666

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\ListItem;
8
use AlexCk\MailchimpBundle\Model\MailChimp\ListItemCampaignDefaults;
9
use AlexCk\MailchimpBundle\Model\MailChimp\ListItemContact;
10
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12
use Symfony\Component\Serializer\SerializerAwareInterface;
13
use Symfony\Component\Serializer\SerializerAwareTrait;
14
15
class ListItemNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
16
{
17
    use SerializerAwareTrait;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function normalize($object, $format = null, array $context = [])
23
    {
24
        /** @var ListItem $item */
25
        $item = &$object;
26
27
        $data = [
28
            'name' => $item->getName(),
29
            'contact' => $this->serializer->normalize($item->getContact(), 'json', []),
0 ignored issues
show
Bug introduced by
The method normalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Serial...rializerCollectionDummy. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            'contact' => $this->serializer->/** @scrutinizer ignore-call */ normalize($item->getContact(), 'json', []),
Loading history...
30
            'campaign_defaults' => $this->serializer->normalize($item->getCampaignDefaults(), 'json', []),
31
            'permission_reminder' => $item->getPermissionReminder(),
32
            'email_type_option' => $item->isEmailTypeOption(),
33
        ];
34
35
        return $data;
36
    }
37
38
    public function denormalize($data, $class, $format = null, array $context = [])
39
    {
40
        if (is_string($data)) {
41
            $data = json_decode($data, true);
42
        }
43
44
        /** @var ListItem $item */
45
        $item = new $class();
46
47
        if ($data) {
48
            foreach ($data as $fieldName => $fieldValue) {
49
                switch ($fieldName) {
50
                    case 'id':
51
                        $item->setId($fieldValue);
52
                        break;
53
                    case 'name':
54
                        $item->setName($fieldValue);
55
                        break;
56
                    case 'permission_reminder':
57
                        $item->setPermissionReminder($fieldValue);
58
                        break;
59
                    case 'email_type_option':
60
                        $item->setEmailTypeOption($fieldValue);
61
                        break;
62
                    case 'contact':
63
                        $item->setContact($this->serializer->denormalize($fieldValue, ListItemContact::class, 'json', []));
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of Symfony\Component\Serializer\SerializerInterface such as Symfony\Component\Serializer\Serializer or Symfony\Component\Serial...rializerCollectionDummy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                        $item->setContact($this->serializer->/** @scrutinizer ignore-call */ denormalize($fieldValue, ListItemContact::class, 'json', []));
Loading history...
64
                        break;
65
                    case 'campaign_defaults':
66
                        $item->setCampaignDefaults($this->serializer->denormalize($fieldValue, ListItemCampaignDefaults::class, 'json', []));
67
                        break;
68
                }
69
            }
70
        }
71
72
        return $item;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function supportsDenormalization($data, $type, $format = null)
79
    {
80
        return ListItem::class == $type;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function supportsNormalization($data, $format = null)
87
    {
88
        return $this->supportsClass($data);
89
    }
90
91
    private function supportsClass($data)
92
    {
93
        return $data instanceof ListItem;
94
    }
95
}
96