Passed
Push — v2 ( d6a970...09f656 )
by Daniel
04:52
created

ApiNormalizer::supportsDenormalization()   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
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Serializer;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
use Symfony\Component\Serializer\SerializerAwareInterface;
20
use Symfony\Component\Serializer\SerializerInterface;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class ApiNormalizer
26
{
27
    private NormalizerInterface $decorated;
28
    private EntityManagerInterface $entityManager;
29
30
    public function __construct(NormalizerInterface $decorated, EntityManagerInterface $entityManager)
31
    {
32
        if (!$decorated instanceof DenormalizerInterface) {
33
            throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class));
34
        }
35
36
        $this->decorated = $decorated;
37
        $this->entityManager = $entityManager;
38
    }
39
40
    public function supportsNormalization($data, $format = null): bool
41
    {
42
        dump($data, $format, $this->decorated->supportsNormalization($data, $format));
43
44
        return $this->decorated->supportsNormalization($data, $format);
45
    }
46
47
    public function normalize($object, $format = null, array $context = [])
48
    {
49
        $data = $this->decorated->normalize($object, $format, $context);
50
        $data['__persisted__'] = $this->entityManager->contains($object);
51
        dump($data);
52
53
        return $data;
54
    }
55
56
    public function supportsDenormalization($data, $type, $format = null): bool
57
    {
58
        return $this->decorated->supportsDenormalization($data, $type, $format);
0 ignored issues
show
Bug introduced by
The method supportsDenormalization() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of Symfony\Component\Serial...zer\NormalizerInterface such as Symfony\Component\Serial...alizer\CustomNormalizer or Symfony\Component\Serial...izer\AbstractNormalizer or ApiPlatform\Core\Hal\Serializer\ObjectNormalizer or Symfony\Component\Serial...\DateIntervalNormalizer or Symfony\Component\Serial...lizer\DataUriNormalizer or Symfony\Component\Serial...izer\DateTimeNormalizer or Symfony\Component\Serial...\DateTimeZoneNormalizer or Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

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

58
        return $this->decorated->/** @scrutinizer ignore-call */ supportsDenormalization($data, $type, $format);
Loading history...
59
    }
60
61
    public function denormalize($data, $class, $format = null, array $context = [])
62
    {
63
        return $this->decorated->denormalize($data, $class, $format, $context);
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. Did you maybe mean normalize()? ( Ignorable by Annotation )

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

63
        return $this->decorated->/** @scrutinizer ignore-call */ denormalize($data, $class, $format, $context);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
    }
65
66
    public function setSerializer(SerializerInterface $serializer): void
67
    {
68
        if ($this->decorated instanceof SerializerAwareInterface) {
69
            $this->decorated->setSerializer($serializer);
0 ignored issues
show
Bug introduced by
The method setSerializer() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of Symfony\Component\Serial...zer\NormalizerInterface such as Symfony\Component\Serial...alizer\CustomNormalizer or Symfony\Component\Serial...izer\AbstractNormalizer. ( Ignorable by Annotation )

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

69
            $this->decorated->/** @scrutinizer ignore-call */ 
70
                              setSerializer($serializer);
Loading history...
70
        }
71
    }
72
}
73