Passed
Push — main ( b2d943...872826 )
by Daniel
05:35
created

d()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Serializer\Normalizer;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Silverback\ApiComponentsBundle\AttributeReader\TimestampedAttributeReader;
18
use Silverback\ApiComponentsBundle\Helper\Timestamped\TimestampedDataPersister;
19
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
20
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
21
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 */
28
class TimestampedNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
29
{
30
    use ClassMetadataTrait;
31
32
    use DenormalizerAwareTrait;
33
34
    private const ALREADY_CALLED = 'TIMESTAMPED_NORMALIZER_ALREADY_CALLED';
35
36
    private TimestampedAttributeReader $annotationReader;
37
    private TimestampedDataPersister $timestampedDataPersister;
38
39
    public function __construct(ManagerRegistry $registry, TimestampedAttributeReader $annotationReader, TimestampedDataPersister $timestampedDataPersister)
40
    {
41
        $this->initRegistry($registry);
42
        $this->annotationReader = $annotationReader;
43
        $this->timestampedDataPersister = $timestampedDataPersister;
44
    }
45
46
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
47
    {
48
        if (!isset($context[self::ALREADY_CALLED])) {
49
            $context[self::ALREADY_CALLED] = [];
50
        }
51
        $id = $type;
52
53
        return !\in_array($id, $context[self::ALREADY_CALLED], true) && $this->annotationReader->isConfigured($type);
54
    }
55
56
    public function denormalize($data, $type, $format = null, array $context = []): mixed
57
    {
58
        $context[self::ALREADY_CALLED][] = $type;
59
60
        $isNew = !isset($context[AbstractNormalizer::OBJECT_TO_POPULATE]);
61
62
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
63
        $this->timestampedDataPersister->persistTimestampedFields($object, $isNew);
64
65
        return $object;
66
    }
67
68
    // TODO: is this only for objects?
69
    public function getSupportedTypes(?string $format): array
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

69
    public function getSupportedTypes(/** @scrutinizer ignore-unused */ ?string $format): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71
        return ['*' => false];
72
    }
73
}
74