SymfonyNotificationPublishedDenormalizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

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