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