Passed
Pull Request — master (#67)
by Daniel
06:16
created

UserNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 3
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 Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentsBundle\Helper\User\UserChangesProcessor;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
20
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
21
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
23
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
25
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
26
27
/**
28
 * @author Daniel West <[email protected]>
29
 */
30
class UserNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
31
{
32
    use DenormalizerAwareTrait;
33
    use NormalizerAwareTrait;
34
35
    private const ALREADY_CALLED = 'USER_NORMALIZER_ALREADY_CALLED';
36
37
    private UserChangesProcessor $userChangesProcessor;
38
39
    public function __construct(UserChangesProcessor $userChangesProcessor)
40
    {
41
        $this->userChangesProcessor = $userChangesProcessor;
42
    }
43
44
    public function hasCacheableSupportsMethod(): bool
45
    {
46
        return false;
47
    }
48
49
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
50
    {
51
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser;
52
    }
53
54
    public function denormalize($data, $type, $format = null, array $context = [])
55
    {
56
        $context[self::ALREADY_CALLED] = true;
57
58
        $isNew = !isset($context[AbstractNormalizer::OBJECT_TO_POPULATE]);
0 ignored issues
show
Unused Code introduced by
The assignment to $isNew is dead and can be removed.
Loading history...
59
        /** @var AbstractUser $object */
60
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
61
        /** @var AbstractUser $oldObject */
62
        $oldObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE];
63
64
        $this->userChangesProcessor->processChanges($object, $oldObject);
65
66
        return $object;
67
    }
68
69
    public function supportsNormalization($data, $format = null, array $context = []): bool
70
    {
71
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser;
72
    }
73
74
    public function normalize($object, $format = null, array $context = [])
75
    {
76
        $context[self::ALREADY_CALLED] = true;
77
78
        return $this->normalizer->normalize($object, $format, $context);
79
    }
80
}
81