UserDenormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 2
A denormalize() 0 16 2
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\User\Serializers;
4
5
use Ramsey\Uuid\Uuid;
6
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7
use VSV\GVQ_API\Common\ValueObjects\Language;
8
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;
9
use VSV\GVQ_API\User\Models\User;
10
use VSV\GVQ_API\User\ValueObjects\Email;
11
use VSV\GVQ_API\User\ValueObjects\Password;
12
use VSV\GVQ_API\User\ValueObjects\Role;
13
14
class UserDenormalizer implements DenormalizerInterface
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function denormalize($data, $class, $format = null, array $context = array()): User
20
    {
21
        $user = new User(
22
            Uuid::fromString($data['id']),
23
            new Email($data['email']),
24
            new NotEmptyString($data['lastName']),
25
            new NotEmptyString($data['firstName']),
26
            new Role($data['role']),
27
            new Language($data['language'])
28
        );
29
30
        if (isset($data['password'])) {
31
            $user = $user->withPassword(Password::fromPlainText($data['password']));
32
        }
33
34
        return $user;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function supportsDenormalization($data, $type, $format = null): bool
41
    {
42
        return ($type === User::class) && ($format === 'json');
43
    }
44
}
45