Passed
Push — master ( 705018...c55970 )
by Daniel
12:42 queued 06:12
created

TimestampedNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 38
ccs 5
cts 15
cp 0.3333
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 2
A denormalize() 0 10 1
A hasCacheableSupportsMethod() 0 3 1
A __construct() 0 5 1
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\AnnotationReader\TimestampedAnnotationReader;
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\CacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
24
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
class TimestampedNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface
30
{
31
    use ClassMetadataTrait;
32
33
    use DenormalizerAwareTrait;
34
35
    private const ALREADY_CALLED = 'TIMESTAMPED_NORMALIZER_ALREADY_CALLED';
36
37
    private TimestampedAnnotationReader $annotationReader;
38
    private TimestampedDataPersister $timestampedDataPersister;
39
40 7
    public function __construct(ManagerRegistry $registry, TimestampedAnnotationReader $annotationReader, TimestampedDataPersister $timestampedDataPersister)
41
    {
42 7
        $this->initRegistry($registry);
43 7
        $this->annotationReader = $annotationReader;
44 7
        $this->timestampedDataPersister = $timestampedDataPersister;
45 7
    }
46
47
    public function hasCacheableSupportsMethod(): bool
48
    {
49
        return false;
50
    }
51
52
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
53
    {
54
        return !isset($context[self::ALREADY_CALLED]) && $this->annotationReader->isConfigured($type);
55
    }
56
57
    public function denormalize($data, $type, $format = null, array $context = [])
58
    {
59
        $context[self::ALREADY_CALLED] = true;
60
61
        $isNew = !isset($context[AbstractNormalizer::OBJECT_TO_POPULATE]);
62
63
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
64
        $this->timestampedDataPersister->persistTimestampedFields($object, $isNew);
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type array; however, parameter $timestamped of Silverback\ApiComponents...sistTimestampedFields() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        $this->timestampedDataPersister->persistTimestampedFields(/** @scrutinizer ignore-type */ $object, $isNew);
Loading history...
65
66
        return $object;
67
    }
68
}
69