Completed
Push — dev ( f2abe4...3f06d3 )
by nonanerz
05:36 queued 05:30
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 Faker\Provider\DateTime;
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 4
    public function __construct($classMDF, $nameCv, $propAs, $propTE)
27
    {
28 4
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
29 4
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 4
    public function supportsNormalization($data, $format = null)
35
    {
36 4
        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
            'date' => $request->getDate(),
55 1
            'createdAt' => $request->getCreatedAt(),
56 1
            'updatedAt' => $request->getUpdatedAt(),
57
        ]), $format, $context);
58
    }
59
60
61
    /**
62
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
     * @param mixed $data
64
     * @param string $class
65
     * @param null $format
66
     * @param array $context
67
     * @return FormRequest
68
     */
69
    public function denormalize($data, $class, $format = null, array $context = array())
70
    {
71
        if (!$this->serializer instanceof DenormalizerInterface) {
72
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
73
        }
74
        /** @var FormRequest $request */
75
        $request = $context[ObjectNormalizer::OBJECT_TO_POPULATE];
76
77
        $request->setDate($data['date']);
78
79
        return $request;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function supportsDenormalization($data, $type, $format = null)
86
    {
87 1
        if ($type != FormRequest::class) {
88 1
            return false;
89
        }
90
91
        return true;
92
    }
93
}
94