BatchResponseNormalizer::supportsDenormalization()   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 3
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\BatchResponse;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
10
class BatchResponseNormalizer implements DenormalizerInterface
11
{
12
    public function denormalize($data, $class, $format = null, array $context = [])
13
    {
14
        if (is_string($data)) {
15
            $data = json_decode($data, true);
16
        }
17
18
        /** @var BatchResponse $item */
19
        $item = new $class();
20
21
        if ($data) {
22
            foreach ($data as $fieldName => $fieldValue) {
23
                switch ($fieldName) {
24
                    case 'id':
25
                        $item->setId($fieldValue);
26
                        break;
27
                    case 'status':
28
                        $item->setStatus($fieldValue);
29
                        break;
30
                    case 'total_operations':
31
                        $item->setTotalOperations($fieldValue);
32
                        break;
33
                    case 'finished_operations':
34
                        $item->setFinishedOperations($fieldValue);
35
                        break;
36
                }
37
            }
38
        }
39
40
        return $item;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function supportsDenormalization($data, $type, $format = null)
47
    {
48
        return BatchResponse::class == $type;
49
    }
50
}
51