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

UserNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 17.39%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 61
ccs 4
cts 23
cp 0.1739
rs 10
c 2
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasCacheableSupportsMethod() 0 3 1
A supportsNormalization() 0 3 2
A normalize() 0 8 1
A supportsDenormalization() 0 3 2
A denormalize() 0 16 2
A __construct() 0 4 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\Security\Core\Role\RoleHierarchy;
19
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
24
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
25
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
26
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
27
28
/**
29
 * @author Daniel West <[email protected]>
30
 */
31
class UserNormalizer implements CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
32
{
33
    use DenormalizerAwareTrait;
34
    use NormalizerAwareTrait;
35
36
    private const ALREADY_CALLED = 'USER_NORMALIZER_ALREADY_CALLED';
37
38
    private UserDataProcessor $userDataProcessor;
39
    private RoleHierarchy $roleHierarchy;
40
41 7
    public function __construct(UserDataProcessor $userDataProcessor, RoleHierarchy $roleHierarchy)
42
    {
43 7
        $this->userDataProcessor = $userDataProcessor;
44 7
        $this->roleHierarchy = $roleHierarchy;
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]) && is_subclass_of($type, AbstractUser::class);
55
    }
56
57
    public function denormalize($data, $type, $format = null, array $context = [])
58
    {
59
        $context[self::ALREADY_CALLED] = true;
60
61
        /** @var AbstractUser $oldObject */
62
        $oldObject = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null;
63
        if ($oldObject) {
0 ignored issues
show
introduced by
$oldObject is of type Silverback\ApiComponents...ntity\User\AbstractUser, thus it always evaluated to true.
Loading history...
64
            $oldObject = clone $oldObject;
65
        }
66
67
        /** @var AbstractUser $object */
68
        $object = $this->denormalizer->denormalize($data, $type, $format, $context);
69
70
        $this->userDataProcessor->processChanges($object, $oldObject);
71
72
        return $object;
73
    }
74
75
    public function supportsNormalization($data, $format = null, array $context = []): bool
76
    {
77
        return !isset($context[self::ALREADY_CALLED]) && $data instanceof AbstractUser;
78
    }
79
80
    /**
81
     * @param AbstractUser $object
82
     * @param mixed|null   $format
83
     */
84
    public function normalize($object, $format = null, array $context = [])
85
    {
86
        $context[self::ALREADY_CALLED] = true;
87
88
        $rolesAsEntities = $object->getRoles();
89
        $object->setRoles($this->roleHierarchy->getReachableRoleNames($rolesAsEntities));
90
91
        return $this->normalizer->normalize($object, $format, $context);
92
    }
93
}
94