Completed
Pull Request — master (#366)
by Beñat
04:43 queued 26s
created

SymfonyUserSignedUpDenormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A denormalize() 0 16 1
A supportsDenormalization() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\Notifier\Infrastructure\Serialization\Inbox;
16
17
use Kreta\Notifier\Domain\Model\Inbox\UserId;
18
use Kreta\Notifier\Domain\Model\Inbox\UserSignedUp;
19
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21
22
final class SymfonyUserSignedUpDenormalizer implements DenormalizerInterface
23
{
24
    private $nameConverter;
25
26
    public function __construct(NameConverterInterface $nameConverter)
27
    {
28
        $this->nameConverter = $nameConverter;
29
    }
30
31
    public function denormalize($data, $class, $format = null, array $context = []) : UserSignedUp
32
    {
33
        $userSignedUp = new UserSignedUp(
34
            UserId::generate($data['payload']['user_id'])
35
        );
36
37
        $reflectionClass = new \ReflectionClass($userSignedUp);
38
        $reflectionProperty = $reflectionClass->getProperty('occurredOn');
39
        $reflectionProperty->setAccessible(true);
40
        $reflectionProperty->setValue(
41
            $userSignedUp,
42
            \DateTimeImmutable::createFromFormat('U', (string) $data['occurred_on'])
43
        );
44
45
        return $userSignedUp;
46
    }
47
48
    public function supportsDenormalization($data, $type, $format = null) : bool
49
    {
50
        return isset($data['type']) && $this->nameConverter->denormalize($data['type']) === UserSignedUp::class;
51
    }
52
}
53