Completed
Pull Request — dev (#59)
by
unknown
03:42
created

RequestNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\FormRequest;
6
use AppBundle\Exception\JsonHttpException;
7
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
8
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
9
use Symfony\Component\Serializer\Exception\LogicException;
10
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
11
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
12
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
15
16
class RequestNormalizer extends ObjectNormalizer
17
{
18
    /**
19
     * RequestNormalizer constructor.
20
     *
21
     * @param ClassMetadataFactoryInterface|null  $classMDF
22
     * @param NameConverterInterface|null         $nameCv
23
     * @param PropertyAccessorInterface|null      $propAs
24
     * @param PropertyTypeExtractorInterface|null $propTE
25
     */
26 5
    public function __construct($classMDF, $nameCv, $propAs, $propTE)
27
    {
28 5
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
29 5
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 5
    public function supportsNormalization($data, $format = null)
35
    {
36 5
        return $data instanceof FormRequest;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function normalize($object, $format = null, array $context = [])
43
    {
44 1
        if (!$this->serializer instanceof NormalizerInterface) {
45
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
46
        }
47
        /** @var FormRequest $request */
48 1
        $request = &$object;
49
50 1
        return $this->serializer->normalize(new \ArrayObject([
51 1
            'id' => $request->getId(),
52 1
            'type' => $request->getType(),
53 1
            'status' => $request->getStatus(),
54 1
            'reason' => $request->getReason(),
55 1
            'date' => $request->getDate(),
56 1
            'createdAt' => $request->getCreatedAt(),
57 1
            'updatedAt' => $request->getUpdatedAt(),
58
        ]), $format, $context);
59
    }
60
61
    /**
62
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
     *
64
     * @param mixed  $data
65
     * @param string $class
66
     * @param null   $format
67
     * @param array  $context
68
     *
69
     * @return FormRequest|JsonHttpException
70
     */
71
    public function denormalize($data, $class, $format = null, array $context = array())
72
    {
73
        if (!$this->serializer instanceof DenormalizerInterface) {
74
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
75
        }
76
        /** @var FormRequest $request */
77
        $request = $context[ObjectNormalizer::OBJECT_TO_POPULATE];
78
79
        if (array_key_exists('date', $data)) {
80
            $request->setDate($data['date']);
81
        }
82
        if (isset($data['reason'])) {
83
            $request->setReason($data['reason']);
84
        }
85
86
        return $request;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function supportsDenormalization($data, $type, $format = null)
93
    {
94 1
        if ($type != FormRequest::class) {
95 1
            return false;
96
        }
97
98
        return true;
99
    }
100
}
101