Passed
Push — master ( 63969d...b1d9d8 )
by Daniel
17:15
created

UserNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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\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