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

UserNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 48
ccs 0
cts 18
cp 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCacheableSupportsMethod() 0 3 1
A __construct() 0 3 1
A supportsDenormalization() 0 3 2
A supportsNormalization() 0 3 2
A normalize() 0 5 1
A denormalize() 0 12 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 Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentsBundle\Helper\User\UserDataProcessor;
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 UserDataProcessor $userDataProcessor;
38
39
    public function __construct(UserDataProcessor $userDataProcessor)
40
    {
41
        $this->userDataProcessor = $userDataProcessor;
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
        /** @var AbstractUser $object */
59
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
60
        /** @var AbstractUser $oldObject */
61
        $oldObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null;
62
63
        $this->userDataProcessor->processChanges($object, $oldObject);
64
65
        return $object;
66
    }
67
68
    public function supportsNormalization($data, $format = null, array $context = []): bool
69
    {
70
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser;
71
    }
72
73
    public function normalize($object, $format = null, array $context = [])
74
    {
75
        $context[self::ALREADY_CALLED] = true;
76
77
        return $this->normalizer->normalize($object, $format, $context);
78
    }
79
}
80