MailChimpResponseNormalizer::normalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 11
rs 10
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\MailChimpResponse;
9
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpWebHook;
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 MailChimpResponseNormalizer 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 MailChimpResponse $item */
25
        $item = &$object;
26
27
        $data = [
28
            'lists' => $this->serializer->normalize($item->getLists(), '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

28
            'lists' => $this->serializer->/** @scrutinizer ignore-call */ normalize($item->getLists(), 'json'),
Loading history...
29
            'webhooks' => $this->serializer->normalize($item->getWebhooks(), 'json'),
30
        ];
31
32
        return $data;
33
    }
34
35
    public function denormalize($data, $class, $format = null, array $context = [])
36
    {
37
        $data = json_decode($data, true);
38
39
        /** @var MailChimpResponse $item */
40
        $item = new $class();
41
42
        if ($data) {
43
            foreach ($data as $fieldName => $fieldValue) {
44
                switch ($fieldName) {
45
                    case 'lists':
46
                        foreach ($fieldValue as $value) {
47
                            /** @var ListItem $listItem */
48
                            $listItem = $this->serializer->denormalize($value, ListItem::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

48
                            /** @scrutinizer ignore-call */ 
49
                            $listItem = $this->serializer->denormalize($value, ListItem::class, 'json', []);
Loading history...
49
50
                            $item->addList($listItem);
51
                        }
52
                        break;
53
                    case 'webhooks':
54
                        foreach ($fieldValue as $value) {
55
                            /** @var MailChimpWebHook $listItem */
56
                            $listItem = $this->serializer->denormalize($value, MailChimpWebHook::class, 'json', []);
57
58
                            $item->addWebhook($listItem);
59
                        }
60
                        break;
61
                }
62
            }
63
        }
64
65
        return $item;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function supportsDenormalization($data, $type, $format = null)
72
    {
73
        return MailChimpResponse::class == $type;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function supportsNormalization($data, $format = null)
80
    {
81
        return $this->supportsClass($data);
82
    }
83
84
    private function supportsClass($data)
85
    {
86
        return $data instanceof MailChimpResponse;
87
    }
88
}
89