MemberNormalizer::normalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlexCk\MailchimpBundle\Normalize\MailChimp;
6
7
use AlexCk\MailchimpBundle\Model\MailChimp\Member;
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 MemberNormalizer 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 Member $item */
23
        $item = &$object;
24
25
        $data = [
26
            'email_address' => $item->getEmail(),
27
            'status' => $item->getStatus(),
28
            'merge_fields' => $this->serializer->normalize($item->getMergeFields(), '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
            'merge_fields' => $this->serializer->/** @scrutinizer ignore-call */ normalize($item->getMergeFields(), 'json', [])
Loading history...
29
        ];
30
31
        return $data;
32
    }
33
34
    public function denormalize($data, $class, $format = null, array $context = [])
35
    {
36
        if (is_string($data)) {
37
            $data = json_decode($data, true);
38
        }
39
40
        /** @var Member $item */
41
        $item = new $class();
42
43
        if ($data) {
44
            foreach ($data as $fieldName => $fieldValue) {
45
                switch ($fieldName) {
46
                    case 'id':
47
                        $item->setId($fieldValue);
48
                        break;
49
                    case 'email_address':
50
                        $item->setEmail($fieldValue);
51
                        break;
52
                    case 'status':
53
                        $item->setStatus($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 Member::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 Member;
81
    }
82
}
83