Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

SymfonyNotificationPublishedDenormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B denormalize() 0 25 1
A supportsDenormalization() 0 5 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\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationBody;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationPublished;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationStatus;
21
use Kreta\Notifier\Domain\Model\Inbox\UserId;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
24
25
final class SymfonyNotificationPublishedDenormalizer implements DenormalizerInterface
26
{
27
    private $nameConverter;
28
29
    public function __construct(NameConverterInterface $nameConverter)
30
    {
31
        $this->nameConverter = $nameConverter;
32
    }
33
34
    public function denormalize($data, $class, $format = null, array $context = []) : NotificationPublished
35
    {
36
        $notificationPublished = new NotificationPublished(
37
            NotificationId::generate($data['payload']['id']),
38
            UserId::generate($data['payload']['user_id']),
39
            new NotificationBody($data['payload']['body'])
40
        );
41
42
        $reflectionClass = new \ReflectionClass($notificationPublished);
43
        $reflectionProperty = $reflectionClass->getProperty('occurredOn');
44
        $reflectionProperty->setAccessible(true);
45
        $reflectionProperty->setValue(
46
            $notificationPublished,
47
            \DateTimeImmutable::createFromFormat('U', (string) $data['occurred_on'])
48
        );
49
50
        $reflectionProperty = $reflectionClass->getProperty('status');
51
        $reflectionProperty->setAccessible(true);
52
        $reflectionProperty->setValue(
53
            $notificationPublished,
54
            new NotificationStatus($data['payload']['status'])
55
        );
56
57
        return $notificationPublished;
58
    }
59
60
    public function supportsDenormalization($data, $type, $format = null) : bool
61
    {
62
        return isset($data['type'])
63
            && $this->nameConverter->denormalize($data['type']) === NotificationPublished::class;
64
    }
65
}
66