MailChimpWebhookEventNormalizer::supportsClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlexCk\MailchimpBundle\Normalize\MailChimp;
6
7
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpWebHooksEvent;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10
use Symfony\Component\Serializer\SerializerAwareInterface;
11
use Symfony\Component\Serializer\SerializerAwareTrait;
12
13
class MailChimpWebhookEventNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
14
{
15
    use SerializerAwareTrait;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function normalize($object, $format = null, array $context = [])
21
    {
22
        /** @var MailChimpWebHooksEvent $item */
23
        $item = &$object;
24
25
        $data = [
26
            'subscribe' => $item->isSubscribe(),
27
            'unsubscribe' => $item->isUnsubscribe(),
28
        ];
29
30
        return $data;
31
    }
32
33
    public function denormalize($data, $class, $format = null, array $context = [])
34
    {
35
        if (is_string($data)) {
36
            $data = json_decode($data, true);
37
        }
38
39
        /** @var MailChimpWebHooksEvent $item */
40
        $item = new $class();
41
42
        if ($data) {
43
            foreach ($data as $fieldName => $fieldValue) {
44
                switch ($fieldName) {
45
                    case 'subscribe':
46
                        $item->setSubscribe($fieldValue);
47
                        break;
48
                    case 'unsubscribe':
49
                        $item->setUnsubscribe($fieldValue);
50
                        break;
51
                }
52
            }
53
        }
54
55
        return $item;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function supportsDenormalization($data, $type, $format = null)
62
    {
63
        return MailChimpWebHooksEvent::class == $type;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function supportsNormalization($data, $format = null)
70
    {
71
        return $this->supportsClass($data);
72
    }
73
74
    private function supportsClass($data)
75
    {
76
        return $data instanceof MailChimpWebHooksEvent;
77
    }
78
}
79