MailChimpWebhookNormalizer::denormalize()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 26
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlexCk\MailchimpBundle\Normalize\MailChimp;
6
7
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpWebHook;
8
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpWebHooksEvent;
9
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
11
use Symfony\Component\Serializer\SerializerAwareInterface;
12
use Symfony\Component\Serializer\SerializerAwareTrait;
13
14
class MailChimpWebhookNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
15
{
16
    use SerializerAwareTrait;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function normalize($object, $format = null, array $context = [])
22
    {
23
        /** @var MailChimpWebHook $item */
24
        $item = &$object;
25
26
        $data = [
27
            'url' => $item->getUrl(),
28
            'events' => $this->serializer->normalize($item->getEvents(), '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
            'events' => $this->serializer->/** @scrutinizer ignore-call */ normalize($item->getEvents(), 'json', []),
Loading history...
29
            'sources' => $this->serializer->normalize($item->getSources(), 'json', []),
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 MailChimpWebHook $item */
42
        $item = new $class();
43
44
        if ($data) {
45
            foreach ($data as $fieldName => $fieldValue) {
46
                switch ($fieldName) {
47
                    case 'id':
48
                        $item->setId($fieldValue);
49
                        break;
50
                    case 'url':
51
                        $item->setUrl($fieldValue);
52
                        break;
53
                    case 'events':
54
                        $item->setEvents($this->serializer->denormalize($fieldValue, MailChimpWebHooksEvent::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

54
                        $item->setEvents($this->serializer->/** @scrutinizer ignore-call */ denormalize($fieldValue, MailChimpWebHooksEvent::class, 'json', []));
Loading history...
55
                        break;
56
                }
57
            }
58
        }
59
60
        return $item;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supportsDenormalization($data, $type, $format = null)
67
    {
68
        return MailChimpWebHook::class == $type;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function supportsNormalization($data, $format = null)
75
    {
76
        return $this->supportsClass($data);
77
    }
78
79
    private function supportsClass($data)
80
    {
81
        return $data instanceof MailChimpWebHook;
82
    }
83
}
84